1

I'm trying to loop through this container that is repeated multiple times on one page and print the time inside the span tag:

<div testid="Item-Content">
<div></div>
<div>
    <div>
        <div></div>
        <div>
            <div>
                <span>5:00pm</span>
            </div>
        </div>
    </div>
</div>

This is what I have so far:

Order = driver.find_elements_by_xpath('//*[@testid="Item-Content"]')
for Times in Order:
    Time = Times.find_element_by_xpath('./div[2]/div/div[2]/div/span')

    print(Time.text)

Errors with selenium.common.exceptions.NoSuchElementException

1
  • the structure of other times are same ? I mean all those divs and span. Commented May 5, 2019 at 2:13

1 Answer 1

2

Try this below code.It should print the value.

Order =driver.find_elements_by_xpath("//div[@testid='Item-Content']")
for times in Order:
    print(times.find_element_by_xpath("//span").text)

OR you can also use WebdriverWait to handle the same.

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

Order=WebDriverWait(driver,30).until(EC.visibility_of_all_elements_located((By.XPATH,"//div[@testid='Item-Content']")))
for times in Order:
   print(times.find_element_by_xpath("//span").text)
Sign up to request clarification or add additional context in comments.

8 Comments

Thanks KunduK. It just printed several blank lines in a row when I ran both of those.
based on your div tag it is printing value here
can i have your full html ?
It's going to very hard to send that over in a comment. It's tons of nested divs with dynamic classes and IDs.
Is your url public?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.