1

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?

1 Answer 1

1

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)
    
Sign up to request clarification or add additional context in comments.

3 Comments

Awesome, appreciate the advice!
Just one more quick question, and I'll be a little more specific this time: I need to access elements td in a row tr in a body tbody. Your method iterates through each element but has no way of distinguishing which row it came from. I need to do something like: for each row in body, create new array, for each element in row, add element to array. And unless I'm not properly understanding your solution, I don't think that's possible with the code you provided. How would I distinguish which elements are part of which row, so I could add them to the appropriate array.
@MatthewKaplan I'm afraid. Sounds like a different question all together. Feel free to raise a new question with your new requirement.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.