Reputation: 15
I'm trying to figure out how to access HTML elements in a nested fashion through Selenium and Python.
For example I have:
box = driver.find_element_by_tag_name('tbody')
which represents the body of the data I'd like to mine. I'd like to iterate through each row in this body (each row characterized by a <tr>
tag) using something like:
for driver.find_element_by_tag_name('tr') in box:
But obviously that's not possible because box is a Selenium object and is non-iterable.
What's the best way to do something like this?
Upvotes: 1
Views: 68
Reputation: 193078
An optimum approach would be to construct locator strategies which would traverse from the parent till the descendants as follows:
Using CSS_SELECTOR:
for element in driver.find_elements(By.CSS_SELECTOR, "tbody tr"):
print(element.text)
Using XPATH:
for element in driver.find_elements(By.XPATH, "//tbody//tr"):
print(element.text)
Upvotes: 1