Python脚本在Chrome更新至113.0.5672.127后不起作用。

huangapple go评论59阅读模式
英文:

Pythone scripts not working after Chrome update to 113.0.5672.127

问题

已将Chrome升级至113.0.5672.127,下载了ChromeDriver 113.0.5672.63,但脚本停止工作了。看起来脚本的第一行,在我尝试操纵页面元素的地方,现在无论如何都不起作用。

错误文本示例:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//iframe"}
  (Session info: headless chrome=113.0.5672.127)

和:

WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[starts-with(@ng-click,'clickEdit')]"))).click()

这确实是第一行,它们已经运行了好几个月了。有人能帮忙理解问题在哪里吗?

尝试使用其他版本的ChromDriver,但没有帮助。

编辑:

所以在一些研究后,我发现页面出现了错误,这个错误导致了我的问题,但看起来这个错误实际上是由我的脚本在更新后首先引起的。现在由于这个错误,我无法访问页面,但我有其他类似的页面,并可以重现这个过程。

看起来如果我运行带有"chrome_options.add_argument("--headless")"(我希望Chrome隐藏)的脚本,我会在cmd窗口中收到以下消息:
DevTools listening on ws://127.0.0.1:52759/devtools/browser/05d5b34e-667c-4beb-a9a8-d87a52a3dd82
[0517/124106.941:INFO:CONSOLE(0)] "Autofocus processing was blocked because a document already has a focused element.", "link".

之后页面上会出现错误,然后对我来说页面不再可用。
但是,如果我在没有错误的页面上运行没有"chrome_options.add_argument("--headless")"的脚本 - 一切正常。
一些奇怪的事情正在发生,有人能帮忙理解到底发生了什么吗?

英文:

Updated Chrome to 113.0.5672.127, downloaded ChromeDriver 113.0.5672.63 but scripts stopped working. Looks like first line of script where I try to manipulate elements on page are not working now no matter what.

Examples of error text:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//iframe"}
  (Session info: headless chrome=113.0.5672.127)

and:

WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[starts-with(@ng-click,'clickEdit')]"))).click()

It's literally first lines and they worked for months. Can someone help to understand what's the problem?

Tried to use other versions of ChromDriver but it didn't help.

Edit:

So after some reserch I found out that page had an error and this error is cause of my problems but looks like error was caused by my script after update in there first place. Now page is unavailable to me because of this error but I have over similar pages and can recreate the process.

Looks like if I run scrips with "chrome_options.add_argument("--headless")"(I want chrome to be hidden) I get this message in cmd window:
DevTools listening on ws://127.0.0.1:52759/devtools/browser/05d5b34e-667c-4beb-a9a8-d87a52a3dd82
[0517/124106.941:INFO:CONSOLE(0)] "Autofocus processing was blocked because a document already has a focused element.", "link".

After that there is a error on this page and page is unavailable after that for me.
But if I run script without "chrome_options.add_argument("--headless")" on page where is no error yet - all is good.
Some crazy things is happening, can someone help to understand what is going on?

答案1

得分: 1

需要编辑代码中的部分:

从:

chrome_options.add_argument("--headless")

改为:

chrome_options.add_argument("--headless=new")

更多相关信息请查看此链接:
https://stackoverflow.com/questions/45631715/downloading-with-chrome-headless-and-selenium/73840130#73840130

英文:

Got answer in russian stack overflow. Need to edit from

chrome_options.add_argument("--headless")

to

chrome_options.add_argument("--headless=new")

more on the topic:
https://stackoverflow.com/questions/45631715/downloading-with-chrome-headless-and-selenium/73840130#73840130

答案2

得分: 0

以下是翻译好的代码部分:

# 尝试使用[Python的Webdriver Manager][1]似乎有效(尽管我在Mac上,所以我的Chrome版本是113.0.5672.126,而不是Windows上的127版本),这里是一个简单的示例:

import time
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager

# 设置Chrome选项
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--remote-debugging-port=9222")
# 设置WebDriver
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=chrome_options)
# 导航到google.com
driver.get("https://google.com")
# 10秒后关闭驱动程序
time.sleep(10)
driver.quit()

[1]: https://pypi.org/project/webdriver-manager/
英文:

Try using Webdriver Manager for Python seems to work (albeit I am on a Mac so my version of Chrome is 113.0.5672.126 instead of 127 which is for Windows), here is a minimal example:

import time
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager

# Setup Chrome options
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--remote-debugging-port=9222")
# Setup WebDriver
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=chrome_options)
# Navigate to google.com
driver.get("https://google.com")
# Close the driver after 10 seconds
time.sleep(10)
driver.quit()

huangapple
  • 本文由 发表于 2023年5月17日 16:55:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76270258.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定