AWS EC2 Ubuntu无头硒send_key不起作用

发布于 2025-01-21 12:37:35 字数 769 浏览 2 评论 0原文

我用硒(Python)编写了一个小机器人,该机器人在我的笔记本电脑(Ubuntu)中完美工作,无论是无头还是GUI。尽管如此,当我尝试使用Ubuntu在AWS EC2计算机中执行相同的代码时,代码不起作用。当我使用函数发送send_keys然后显示文本时,它仍然是

driver.find_element_by_name('email').send_keys('[email protected]')
driver.find_element_by_name('email').text

我在服务器中使用的选项为:

options = Options()
options.add_argument("--headless")
options.add_argument("--window-size=1920,1080")
options.add_argument("--disable-gpu")
options.add_argument("user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36")

我在笔记本电脑中使用完全相同的代码,并且可以使用,相同的软件包等

I wrote a small bot using selenium (python) that is working perfectly in my laptop (ubuntu), both headless and with GUI. Nonetheless, when I try to execute the same code in an AWS EC2 machine with Ubuntu the code does not work. When I use the function to send_keys and then display the text, it is still empty

driver.find_element_by_name('email').send_keys('[email protected]')
driver.find_element_by_name('email').text

the options that I am using in the server are:

options = Options()
options.add_argument("--headless")
options.add_argument("--window-size=1920,1080")
options.add_argument("--disable-gpu")
options.add_argument("user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36")

I use the exact same code in my laptop and it works, same packages, etc

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

万人眼中万个我 2025-01-28 12:37:35

也许只需要明确的等待?

wait = WebDriverWait(driver, 30)
wait.until(EC.visibility_of_element_located((By.NAME, "email"))).send_keys('[email protected]')

或者您甚至也可以尝试JS:

wait = WebDriverWait(driver, 30)
name = wait.until(EC.presence_of_element_located((By.NAME, "email")))
driver.execute_script("arguments[0].setAttribute('value', '[email protected]')", name)

进口:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

Maybe it just requires explicit wait?

wait = WebDriverWait(driver, 30)
wait.until(EC.visibility_of_element_located((By.NAME, "email"))).send_keys('[email protected]')

or You can even try with JS as well:

wait = WebDriverWait(driver, 30)
name = wait.until(EC.presence_of_element_located((By.NAME, "email")))
driver.execute_script("arguments[0].setAttribute('value', '[email protected]')", name)

Imports:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文