gpt4 book ai didi

python - 即使在 headless 模式下,是否可以在带有 Chrome 的 Selenium 上使用 `element.click()`?

转载 作者:行者123 更新时间:2023-12-03 22:55:31 29 4
gpt4 key购买 nike

我正在编写一个脚本来使用 Python 中的 Selenium 及其 Chrome 驱动程序在网站上下载一组文件,但我发现每当程序打开新标签页或点击下载时,看到浏览器打开并获得焦点是令人厌恶的按钮。

所以我宁愿使用 headless 模式。但是,该元素总是让我出现以下错误:

WebDriverException: Message: unknown error: Element ... is not clickable at point (435, 575). Other element would receive the click:

...

(Session info: headless chrome=66.0.3359.139) (Driver info: chromedriver=2.37.544337 (8c0344a12e552148c185f7d5117db1f28d6c9e85),platform=Mac OS X 10.13.4 x86_64)



这个错误曾经发生在我的非 headless 模式下,但是因为我通过 driver.maximize_window() 改变了浏览器的大小,错误消失了。这可以清楚地表明按钮必须在屏幕上可见,才能在 Chrome 上推送。

但是在 headless 模式下,按钮被隐藏可能是真的,这就是元素始终无法点击的方式,从而导致错误。那么有没有办法让按钮即使在 headless 模式下也可以点击?

相关代码如下,最后一行导致报错:
def login(driver, url, username, password):
driver.get(url)
uname = driver.find_element_by_name("login")
uname.send_keys(username)

passw = driver.find_element_by_name("password")
passw.send_keys(password)
submit_button = driver.find_element_by_class_name("button")
action = webdriver.ActionChains(driver)
action.move_to_element(submit_button)
submit_button.click()

最佳答案

but since I changed the size of the browser by driver.maximize_window()



我在使用 headless Chrome 时遇到过这个问题。在 Jenkins 。问题是浏览器在 headless 模式下没有以全分辨率打开。所以我添加了一行以使用特定分辨率打开浏览器
driver.set_window_size(1440, 900)

然后传递 url 并获取所有内容。您可以尝试这种方法一次,看看这是否是问题所在。

关于python - 即使在 headless 模式下,是否可以在带有 Chrome 的 Selenium 上使用 `element.click()`?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50327432/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com