[英]web crawling a table of links
我正在用python创建一个脚本,该脚本通过一个包含三列的表。 我创建了一个列表,第一列中的每个链接都插入到列表中。 然后我循环通过。 循环时,我单击链接,打印一条语句以确保它实际上已单击链接,然后转到上一页,以便可以单击下一个链接。 我一直得到的错误是我的循环首先通过前两个链接,然后在循环第三次调用links [page] .click()时得到StaleElementReferenceException。 我无法发布HTML,因为该网站是机密的。
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
import traceback
# starting chrome browser
chrome_path = r"C:\Users\guaddavi\Downloads\chromedriver_win32 extract\chromedriver.exe"
browser = webdriver.Chrome(chrome_path)
#linking to page
browser.get('link to page with table ')
#find table of ETL Extracts
table_id = browser.find_element_by_id('sortable_table_id_0')
#print('found table')
#get all the rows of the table containing the links
rows = table_id.find_elements_by_tag_name('tr')
#remove the first row that has the header
del rows[0]
current = 0
links = [] * len(rows)
for row in rows:
col = row.find_elements_by_tag_name('td')[0]
links.append(col)
current +=1
page = 0
while(page <= len(rows)):
links[page].click()
print('clicked link' + " " + str(page))
page += 1
browser.back()
我不确定您是否已经看过Selenium官方文档:
在以下两种情况之一中,将引发陈旧的元素引用异常,第一种情况比第二种情况更为常见:元素已被完全删除。 元素不再附加到DOM。
就您而言,我认为您有第二个问题。 每次单击并返回循环,您的DOM都会发生变化。 请检查一下。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.