python - Selenium - Python如何在循环内一一单击行元素我想查询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("//*[
问题描述
我想查询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:
我相信我迷失在页面上下文中。如何使此功能正常工作。
- 我的意思是转到一页。
- 对于每个表 tr
- 读td
- 点击第一个 td
- 从第 (4) 页抓取一些信息
- 回去
- 再次循环继续
您将 baseURL 与 axios Post 附加到哪里?我想你忘了附加那个?
解决方案
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
推荐阅读
- android-studio - Huawei Toolkit 无法分析我的项目
- amazon-web-services - quicksight 数据集无法从数据源导入数据以及使用 createDataSet api 创建摄取问题数据集的问题
- c# - 设置窗口大小以适应内容,但阻止窗口宽度超出屏幕宽度
- python - Pandas 系列除以另一个系列,除法前四舍五入到小数点后两位
- javascript - Pub-Sub 模式和消息代理,如何确保所有订阅者完成对事件的处理
- kotlin - Kotlin 中的“val XXX = Class { ...”是什么意思?
- windows - 什么是信托提供者?
- python - 迭代数据框列
- node.js - 合适的加载器来处理这个文件类型,目前没有配置加载器来处理这个文件。构建期间的下一个 js 配置错误
- java - HttpURLConnection Locally 适用于所有 url,但它不在服务器上
我想查询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("//*[
问题描述
我想查询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:
我相信我迷失在页面上下文中。如何使此功能正常工作。
- 我的意思是转到一页。
- 对于每个表 tr
- 读td
- 点击第一个 td
- 从第 (4) 页抓取一些信息
- 回去
- 再次循环继续
您将 baseURL 与 axios Post 附加到哪里?我想你忘了附加那个?
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