[英]How to locate element not found in shadow-root
I am newbie to python automation.我是 python 自动化的新手。 So what i want to do is that I am trying to automate flight booking on wego.co.in but when I am searching xpath for the same, the xpath doesn't get highlighted even I think I am getting the right xpath.
所以我想做的是我试图在 wego.co.in 上自动进行航班预订,但是当我搜索 xpath 时,xpath 并没有突出显示,即使我认为我得到了正确的 Z3D788FA62D7C1805A1ZEE4。
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from time import sleep
driver = webdriver.Chrome()
driver.maximize_window()
driver.get("https://www.wego.co.in/")
fly_from = driver.find_element_by_xpath('//input[@placeholder="From"]').click()
fly_from.send_keys("New Delhi, India")
sleep(0.1)
fly_to = driver.find_element_by_xpath('//input[@placeholder="to"]').click()
fly_to.send_keys("Goa, India")
I am getting "element not found" error我收到“找不到元素”错误
The element is inside several nested shadow-root
sections.该元素位于几个嵌套
shadow-root
部分中。 You need to switch to each section one at a time much like <ifram>
s, however Selenium doesn't have builtin support for shadow-root
.您需要像
<ifram>
一样一次切换到每个部分,但是 Selenium 没有对shadow-root
的内置支持。
A workaround would be to get the shadow-root
section as WebElement using JavaScript and use this element to locate elements inside the section一种解决方法是使用 JavaScript 将
shadow-root
部分作为 WebElement 获取,并使用此元素定位该部分内的元素
driver.get("https://www.wego.co.in/")
element = driver.find_element_by_id('app')
shadow_root = self.get_shadow_root(driver, element)
element = shadow_root.find_element_by_css_selector('[id="base"] > wego-search-form')
shadow_root = self.get_shadow_root(driver, element)
element = shadow_root.find_element_by_class_name('flightSearchForm')
search_bar_shadow_root = self.get_shadow_root(driver, element)
element = search_bar_shadow_root.find_element_by_id('dep')
shadow_root = self.get_shadow_root(driver, element)
fly_from = shadow_root.find_element_by_css_selector('[placeholder="From"]')
fly_from.click()
fly_from.send_keys("New Delhi, India")
element = search_bar_shadow_root.find_element_by_id('arr')
shadow_root = self.get_shadow_root(driver, element)
fly_to = shadow_root.find_element_by_css_selector('[placeholder="to"]')
fly_to.click()
fly_to.send_keys("Goa, India")
def get_shadow_root(self, driver, element):
return driver.execute_script('return arguments[0].shadowRoot', element)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.