我试图遍历此容器,该容器在一页上重复多次,并在span标签内打印时间:
<div testid="Item-Content">
<div></div>
<div>
<div>
<div></div>
<div>
<div>
<span>5:00pm</span>
</div>
</div>
</div>
</div>
这是我到目前为止所拥有的:
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)
selenium.common.exceptions.NoSuchElementException错误
答案 0 :(得分:1)
尝试下面的代码。它应该打印出值。
Order =driver.find_elements_by_xpath("//div[@testid='Item-Content']")
for times in Order:
print(times.find_element_by_xpath("//span").text)
或者您也可以使用WebdriverWait来处理相同的事情。
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)