Selenium webdriver 访问Google chrome 的设置页面无法定位(shadow root)问题
如题,在Google chrome 的设置页面 ,使用 Selenium webdriver
想使用 driver.find_element_by_xpath(“xxxx”).click( ) 对其中的元素进行定位
获取到的XPATH为:
//*[@id=“changeDownloadsPath”]//cr-button
提示错误:找不到元素:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {“method”:“xpath”,“selector”:"//*[@id=‘cnblogs_post_body’]/table[2]/tbody/tr[3]/td[2]/font"}
(Session info: chrome=79.0.3945.130)
错误原因:
shadown dom 的存在
具体可以参考:
https://www.cnblogs.com/yangguoe/p/8486046.html

解决方案:
(1)stack overflow上有大佬的各种方法,俺没有试(懒懒懒)
(2)CSDN有位大佬也提供了方法,俺也没试(懒懒懒,附链接)
https://blog.csdn.net/LeonLee85/article/details/101566770
(3)小白要求没那么高,想要简单粗暴解决,直接调用 pyautogui ,在设置页面狂点 tab 45次 再点一次 enter 就会出现奇迹!(由于本人的项目中其他地方也要用到pyautogui,所以没有感觉增加很多额外力气)具体操作如下:
driver = webdriver.Chrome(executable_path=config['driver_path'], options=opt) # 创建浏览器对象
driver.maximize_window() # 最大化窗口
driver.get("chrome://settings/downloads")
time.sleep(2)
for i in range(5):
pyautogui.press(['tab', 'tab', 'tab', 'tab', 'tab', 'tab', 'tab', 'tab', 'tab']) # 按下 5 * 9 = 45 次正好到更改
pyautogui.press('enter') # 点击“更改按钮”
关于pyautogui的使用,个人觉着这个还可以:
https://www.jianshu.com/p/e4f4ca3f6b52
这样做不一定能解决你的问题,但是解决了我的问题hhh~
我是什么问题这样粗暴操作就能解决?
阔以看看这个迷惑行为:
(此处应该有链接,但是俺还在写hhh~)
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐


所有评论(0)