Using Selenium WebDriver with Python 3.4.
I'm writing a scraper, and locating elements using XPaths relative to some non-root ancestor element, such as below:
ancestor_element = driver.find_element_by_xpath(ancestor_xpath)
child_element = ancestor_element.find_element_by_xpath(child_xpath)
This works as expected. However, I am unsure how to do this relative location with an explicit wait call, as the examples I have seen use this syntax:
child_element = WebDriverWait(driver,10).until(
EC.presence_of_element_located((By.XPATH, child_xpath))
)
which appears to evaluate the XPath against the page root, and throws an error complaining about the ".//" beginning of the XPath string.
Any advice on this?
waitdocumentation includes an example that appears to use a parent element to find its child as opposed to searching from page root.element = WebDriverWait(driver, 10).until(lambda x: x.find_element_by_id(“someId”)). I have not tried it, but merely knew it existed.You should be able to change the query to search by xpath.//a/band child xpath is.//c/d, then it will be same as//a/b//c/dclass child_is_present def __init__(self, ancestor_xpath, child_xpath)... def __call__(self, driver): ancestor_element = driver.find_element_by_xpath(*self.ancestor_xpath) child_element = ancestor_element.find_element_by_xpath(*self.child_xpath) if(child_element) return true