What should be changed about this Xpath string to find the last child element?Ask Question

问题:

A list of all child div elements are found when using the following line:

driver.find_element_by_css_selector('div[id="panel2LocListShell"]').find_elements_by_tag_name('div')

However, when using Xpath to find the last child div element, there is a timeout error:

For example:

WebDriverWait(driver,20).until(EC.presence_of_element_located((By.XPATH, './/div[@id = panel2LocListShell]/div[last()]')))

What, if anything, should be changed about this Xpath line?: './/div[@id = panel2LocListShell]/div[last()]'

回答1:


driver.find_element_by_css_selector('div[id="panel2LocListShell"]').find_elements_by_tag_name('div')

Here, selenium would locate all div child elements at any depth level inside the div element with id="panel2LocListShell".

.//div[@id = panel2LocListShell]/div[last()]

In this case, you are looking for div elements located directly under the div element with id="panel2LocListShell". Plus, the panel2LocListShell should be put into quotes.

Fixed expression:

.//div[@id = "panel2LocListShell"]//div[last()]
标签: selenium xpath
© 2014 TuiCode, Inc.