How to find element by XPATH using variable Selenium

Question:

I’m trying to parse some elements using XPATH

This is my python code:

driver.get("http://watir.com/examples/shadow_dom.html")
shadow_host = driver.find_element(By.XPATH, '//*[@id="shadow_host"]')
shadow_root1 = shadow_host.shadow_root
shadow_host1 = shadow_root1.find_element(By.XPATH, '/span')

selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: invalid locator

I know that I can use CSS Selectors, but I want to use it in case there aren’t any CSS Selectors. Can you help me, how to write shadow_host1 correctly using XPATH? Thanks

Asked By: KraKoff_

||

Answers:

You cannot use XPATH locators with shadow root. You can use CSS LOCATORS instead (when on a Chrome/chromedriver setup), or alternatively (when using a Firefox/geckodriver setup) use javascript selectors (querySelector).

Answered By: Barry the Platipus