当我访问chrome://settings/help网站,并试图通过XPath或其他渠道访问最新版本的Google时,我无法访问它.

我只是想得到最新版本的Chrome的字符串.

是否存在无法识别XPath的原因或特定情况?

错误:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="main"]//settings-about-page//settings-section[1]/div[2]/div[2]/div[2]"} (Session info: chrome=114.0.5735.199);

请.帮助.

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
import time

service = Service(executable_path="chromedriver")
options = webdriver.ChromeOptions()
options.add_argument('start-maximized')
driver = webdriver.Chrome(service=service, options=options)

driver.get('chrome://settings/help')
time.sleep(5)
update_check = driver.find_element(By. XPATH, '//*[@id="main"]//settings-about-page//settings-section[1]/div[2]/div[2]/div[2]').text
print(update_check)

推荐答案

考虑到您的XPath,我想您正在try 获取机器上已安装的Chrome版本的文本.

因为页面高度嵌入了许多shadow-root个元素,这使得使用XAPTH、CSS Select 器、ID等常用的定位器策略来定位嵌入在shadow-root中的元素是不可能的.

以下是你如何做到这一点:

import time
from selenium import webdriver

options = webdriver.ChromeOptions()
options.add_argument('start-maximized')
driver = webdriver.Chrome(options=options)

driver.get('chrome://settings/help')

time.sleep(2)
update_check = driver.execute_script("return document.querySelector('settings-ui').shadowRoot.querySelector('settings-main').shadowRoot.querySelector('settings-about-page').shadowRoot.querySelectorAll('settings-section')[0].querySelector('div.secondary').getInnerHTML();")
print(update_check)

输出:

Version 114.0.5735.199 (Official Build)  (64-bit)

参考资料:

  1. Selenium: how to get element in shadow root of html page code?
  2. How to click the button on Visa exchange rate calculator using Selenium?
  3. Scraping elements inside the #shadow-root web element

Python相关问答推荐

Chatgpt API不断返回错误:404未能从API获取响应

通过优化空间在Python中的饼图中添加标签

连接两个具有不同标题的收件箱

删除任何仅包含字符(或不包含其他数字值的邮政编码)的观察

. str.替换pandas.series的方法未按预期工作

运行终端命令时出现问题:pip start anonymous"

pandas滚动和窗口中有效观察的最大数量

用Python解密Java加密文件

如何在表中添加重复的列?

将pandas导出到CSV数据,但在此之前,将日期按最小到最大排序

通过ManyToMany字段与Through在Django Admin中过滤

解决调用嵌入式函数的XSLT中表达式的语法移位/归约冲突

可以bcrypts AES—256 GCM加密损坏ZIP文件吗?

pandas fill和bfill基于另一列中的条件

mdates定位器在图表中显示不存在的时间间隔

如何重新组织我的Pandas DataFrame,使列名成为列值?

浏览超过10k页获取数据,解析:欧洲搜索服务:从欧盟站点收集机会的微小刮刀&

我怎么才能用拉夫分拣呢?

我怎样才能让深度测试在OpenGL中使用Python和PyGame呢?

Python:在cmd中添加参数时的语法