简体   繁体   中英

How to locate element not found in shadow-root

I am newbie to python automation. 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.

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. 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 .

A workaround would be to get the shadow-root section as WebElement using JavaScript and use this element to locate elements inside the section

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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