简体   繁体   English

如何定位在 shadow-root 中找不到的元素

[英]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.

相关问题 如何使用 shadow-root 访问网站中的产品元素? - How do I acces the products element in a website with shadow-root? python selenium:无法定位元素,shadow-root内的svg-icon按钮 - python selenium: unable to locate element, svg-icon button inside shadow-root 无法使用 Selenium 和 Python 在 #shadow-root(打开)中找到登录元素 - Unable to locate the Sign In element within #shadow-root (open) using Selenium and Python Python Selenium - 如何在#shadow-root(打开)下的textarea中定位和添加数据 - Python Selenium - How to locate and add data in textarea which is under #shadow-root (open) 网页抓取#shadow-root - Webscraping #shadow-root 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 如何阅读#shadow-root(用户代理)下的文本 - How to read text that is under #shadow-root (user-agent) 无法使用 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 将 Google Chrome 升级到 96 版后,shadow-root 元素搜索不起作用 - After upgrading to Google Chrome to version 96, shadow-root element search does not work
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM