[英]Selenium - Python How to click row element one by one inside <table><tr> loop
我想查詢 html 表。 下面是我的代碼,
browser.get("url.html")
# Wait 20 seconds for page to load
# have EC.visibility_of_element_located here
table_rows = browser.find_elements_by_xpath("//*[@id='table_id']/tbody/tr")
for row in table_rows:
print(row.find_element_by_xpath('./td[2]').text)
table_link = row.find_element_by_xpath('./td[1]/a')
table_link.click()
錯誤:
selenium.common.exceptions.StaleElementReferenceException:
我相信我迷失在頁面上下文中。 如何使此功能正常工作。
selenium.common.exceptions.StaleElementReferenceException:
當元素未附加到頁面時出現。單擊鏈接時頁面正在刷新並且元素不再附加到頁面並且變得stale
。
為了克服這個問題,您需要重新分配元素。 誘導WebDriverWait
()並等待visibility_of_all_elements_located
()
table_rows = WebDriverWait(browser,20).until(EC.visibility_of_all_elements_located((By.XPATH, "//*[@id='table_id']/tbody/tr")))
for row in range(len(table_rows)):
# re-assigned variable here
table_rows = WebDriverWait(browser,20).until(EC.visibility_of_all_elements_located((By.XPATH, "//*[@id='table_id']/tbody/tr")))
print(table_rows[row].find_element_by_xpath('./td[2]').text)
table_link = table_rows[row].find_element_by_xpath('./td[1]/a')
table_link.click()
#Perform details for the page
#
browser.back()
您需要導入以下庫。
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.