简体   繁体   English

python selenium,无法从page_source中找到元素,而可以从浏览器中找到

[英]python selenium, can't find elements from page_source while can find from browser

I try find target element by xpath so that I can click on it.我尝试通过 xpath 查找目标元素,以便我可以单击它。 But can't find it when run code, although can find it by right-click option manually on chrome browser.但是在运行代码时找不到它,虽然可以在chrome浏览器上通过右键单击选项手动找到它。

detail: I am using详细信息:我正在使用

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

to get history pop-up from chrome, then wait element by selenium, and next action I try to click it by:从 chrome 获取历史弹出窗口,然后通过 selenium 等待元素,然后我尝试通过以下方式单击它:

driver.find_element_by_css_selector('* /deep/ #clearBrowsingDataConfirm').click()

or by:或通过:

driver.find_element_by_xpath(r'//paper-button[@id="clearBrowsingDataConfirm"]').click()

both does not work两者都不起作用

Could you tell solution by xpath if possible because I am more familiar with it.如果可能,您能否通过 xpath 告诉解决方案,因为我更熟悉它。 Or any other way to clear history on chrome, thank或任何其他方式来清除 chrome 上的历史记录,谢谢

Looking into Chrome Settings page source it looks like the button, you're looking for is hidden in the ShadowDOM查看 Chrome 设置页面源,它​​看起来像按钮,你正在寻找隐藏在ShadowDOM 中

So you need to iterate down several levels of ShadowRoot所以需要向下迭代几层ShadowRoot

在此处输入图片说明

So the algorithm looks like:所以算法看起来像:

  1. Locate parent WebElement定位父WebElement
  2. Locate its shadow-root and cast it to the WebElement找到它的shadow-root并将其投射到 WebElement
  3. Use WebElement.find_element() function to locate the next WebElement which is the parent for the ShadowRoot使用WebElement.find_element()函数定位下一个 WebElement,它是 ShadowRoot 的父级
  4. Repeat steps 1-3 until you're in the same context with the element you want to interact with重复步骤 1-3,直到您与要与之交互的元素处于相同的上下文中

Example code:示例代码:

from selenium import webdriver

def expand_root_element(element):
    shadow_root = driver.execute_script('return arguments[0].shadowRoot', element)
    return shadow_root


driver = webdriver.Chrome("c:\\apps\\webdriver\\chromedriver.exe")
driver.maximize_window()
driver.get("chrome://settings/clearBrowserData")

settingsUi = driver.find_element_by_tag_name("settings-ui")
settingsUiShadowRoot = expand_root_element(settingsUi)

settingsMain = settingsUiShadowRoot.find_element_by_tag_name("settings-main")
settingsShadowRoot = expand_root_element(settingsMain)
settingsBasicPage = settingsShadowRoot.find_element_by_tag_name("settings-basic-page")
settingsBasicPageShadowroot = expand_root_element(settingsBasicPage)
settingsPrivacyPage = settingsBasicPageShadowroot.find_element_by_tag_name("settings-privacy-page")
settingsPrivacyShadowRoot = expand_root_element(settingsPrivacyPage)
settingsClearBrowsingDataDialog = settingsPrivacyShadowRoot.find_element_by_tag_name(
    "settings-clear-browsing-data-dialog")
settingsClearBrowsingDataDialogShadowRoot = expand_root_element(settingsClearBrowsingDataDialog)
settingsClearBrowsingDataDialogShadowRoot.find_element_by_id("clearBrowsingDataConfirm").click()

我通过这样做让它工作:

 driver.ExecuteScript("return document.querySelector('body > settings-ui').shadowRoot.querySelector('#main').shadowRoot.querySelector('settings-basic-page').shadowRoot.querySelector('#advancedPage > settings-section:nth-child(1) > settings-privacy-page').shadowRoot.querySelector('settings-clear-browsing-data-dialog').shadowRoot.querySelector('#clearBrowsingDataConfirm').click();");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM