Bronson77
Bronson77

Reputation: 289

Loop through to Find Elements Python

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

Upvotes: 1

Views: 1149

Answers (1)

KunduK
KunduK

Reputation: 33384

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)

Upvotes: 2

Related Questions