本文介紹了無法使用 Selenium 和 Python 在 #shadow-root (open) 中找到登錄元素的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我正在嘗試使用 selenium 自動執行某些操作,但無法在頁面上找到第一個元素 :
從 selenium 導入 webdriver選項 = webdriver.ChromeOptions()options.add_argument(開始最大化")options.add_experimental_option("excludeSwitches", ["enable-automation"])options.add_experimental_option('useAutomationExtension', False)driver = webdriver.Chrome(options=options, executable_path=r'C:WebDriverschromedriver.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()
- 瀏覽器快照:
參考文獻
您可以在以下位置找到一些相關的詳細討論:
- 如何交互在使用 cssSelector 清除 Chrome 瀏覽器的瀏覽數據時使用 #shadow-root (open) 中的元素
- 如何使用 selenium 自動化影子 DOM 元素?
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
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)
Solution
Tto click()
on the desired element you can use shadowRoot.querySelector()
and you can use the following Locator Strategy:
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:WebDriverschromedriver.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:
- How to interact with the elements within #shadow-root (open) while Clearing Browsing Data of Chrome Browser using cssSelector
- How to automate shadow DOM elements using selenium?
這篇關于無法使用 Selenium 和 Python 在 #shadow-root (open) 中找到登錄元素的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!