简体   繁体   English

无法使用 Selenium 和 Python 在 #shadow-root(打开)中找到登录元素

[英]Unable to locate the Sign In element within #shadow-root (open) using Selenium and Python

I'm trying to use selenium to automate some actions but am unable to find the first element on the page https://developer.servicenow.com/dev.do and so cannot login我正在尝试使用 selenium 来自动执行某些操作,但无法在页面https://developer.servicenow.com/dev.do上找到第一个元素,因此无法登录

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

driver_path = "../bin/chromedriver.exe"
driver = webdriver.Chrome(driver_path)

driver.get("https://developer.servicenow.com/dev.do")

driver.find_element_by_xpath("/html/body/dps-app//div/header/dps-navigation-header//header/div/div[2]/ul/li[3]/dps-login//div/dps-button//button/span")

I get the error我得到错误

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/dps-app//div/header/dps-navigation-header//header/div/div[2]/ul/li[3]/dps-login//div/dps-button//button/span"}

To Sign In button is deep within multiple #shadow-root (open)登录按钮位于多个#shadow-root (open)的深处

shadow-root-open-multiple


Solution解决方案

Tto click() on the desired element you can use shadowRoot.querySelector() and you can use the following Locator Strategy :要在所需元素上click() ,您可以使用shadowRoot.querySelector()并且可以使用以下定位器策略

from selenium import webdriver

options = webdriver.ChromeOptions() 
options.add_argument("start-maximized")
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)
driver = webdriver.Chrome(options=options, executable_path=r'C:\WebDrivers\chromedriver.exe')
driver.get('https://developer.servicenow.com/dev.do')
SignInButton = driver.execute_script("return document.querySelector('dps-app').shadowRoot.querySelector('dps-navigation-header').shadowRoot.querySelector('header.dps-navigation-header dps-login').shadowRoot.querySelector('dps-button')")
SignInButton.click()
        
  • Browser Snapshot:浏览器快照:

影子根打开


References参考

You can find a couple of relevant detailed discussions in:您可以在以下位置找到一些相关的详细讨论:

You could look at the Automatepro app on the servicenow store.您可以查看 servicenow 商店中的 Automatepro 应用程序。 It uses selenium but they have pre-written the selenium behind the scenes to interact with all the servicenow UI components, including those using shadow DOM.它使用 selenium,但他们在幕后预先编写了 selenium 以与所有 servicenow UI 组件进行交互,包括使用影子 DOM 的组件。 You just pick the UI element and supply the data for your test.您只需选择 UI 元素并为您的测试提供数据。 We found it saves a lot of time writing and maintaining the selenium code ourselves..我们发现它可以节省大量时间编写和维护 selenium 代码。

This solution was what I was looking for to log into my instance using Powershell.这个解决方案是我使用 Powershell 登录我的实例所寻找的。

driver.executescript("return document.querySelector('dps-app').shadowRoot.querySelector('dps-navigation-header').shadowRoot.querySelector('header.dps-navigation-header dps-login').shadowRoot.querySelector('dps-button')").Click()

I think its because you are trying to access the "Sign in" before the website has time to load all of the elements.我认为这是因为您试图在网站有时间加载所有元素之前访问“登录”。

I suggest you use我建议你使用

driver.set_page_load_timeout(120)

so the selenium will look for the button after everything is loaded on the website.所以 selenium 将在网站上加载所有内容后查找按钮。

暂无
暂无

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

相关问题 无法使用 Python Selenium 在 shadow-root(打开)中定位元素 - Can't locate elments within shadow-root (open) using Python Selenium Python Selenium can't find element by xpath within #shadow-root (open) using Selenium and Python - Python Selenium can't find element by xpath within #shadow-root (open) using Selenium and Python How to locate the First name field within shadow-root (open) within the website https://www.virustotal.com using Selenium and Python - How to locate the First name field within shadow-root (open) within the website https://www.virustotal.com using Selenium and Python python selenium:无法定位元素,shadow-root内的svg-icon按钮 - python selenium: unable to locate element, svg-icon button inside shadow-root 如何使用 Selenium Python 在#shadow-root(打开)中提取信息? - How to extract info within a #shadow-root (open) using Selenium Python? 无法使用 Python Selenium 从 shadow-root 内的元素中提取文本 - Unable to pull text from elements within shadow-root using Python Selenium Python Selenium - 如何在#shadow-root(打开)下的textarea中定位和添加数据 - Python Selenium - How to locate and add data in textarea which is under #shadow-root (open) 通过 Python 和 Selenium 在#shadow-root(打开)中单击按钮的最佳方式 - Best way to click a button within #shadow-root (open) via Python and Selenium 如何定位在 shadow-root 中找不到的元素 - How to locate element not found in shadow-root 如何使用 Selenium 在#shadow-root(打开)中切换到子框架 - How to switch to the child frame within #shadow-root (open) using Selenium
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM