我正在编写一个python selenium脚本来从网站上抓取数据。该脚本从CSV文件中读取员工身份号码、姓名和出生日期,并将其复制到Dictionary&然后将每一行的数据输入到表单中,然后单击提交按钮。出现的下一个页面询问雇佣历史的年份,默认为一年,这对我的用例来说很好,所以脚本在这个页面上唯一做的就是单击搜索按钮。该脚本始终适用于大约20至35名员工。但在某个时刻,脚本将失败,出现以下陈旧的元素引用异常:
消息:的元素引用已过时;元素不再附加到DOM,它不在当前框架上下文中,或者文档已刷新
搜索按钮的相关HTML是:
<div data-container="" class="btn-label OSInline">Search</div>
</button>
</div>
</div>
</div><div data-container="" style="text-align: center;" id="b8-ResetFilters2"><a data-link="" href="#" style="width: auto;"><span data-expression="" style="width: auto;">Reset Search</span></a>
</div>
</div>
</div>
<div
相关的Python Selenium代码是:
# Open MOCemployees.csv as readable file & then convert to a Python Dictionary
with open('MOCemployees.csv', 'r') as MOCemployees.:
MOCemployees.Dict = csv.DictReader(MOCemployees.)
for line in MCpatientsDict:
MOCEmployeeNumber = (line['MCID'])
LastName = (line['LastName'])
FirstName = (line['FirstName'])
DOB = (line['DOB'])
# Explicit Wait for MOCEmployeeNumberInput then click on it and enter MOCEmployeeNumber
MOCEmployeeNumberInput = WebDriverWait(driver, 15).until(EC.element_to_be_clickable((By.ID, 'b7-MOCEmployeeNumberInput')))
# Locate LastNameInput and click on it and enter LastName
LastNameInput = driver.find_element(By.ID, 'b7-LastNameInput')
LastNameInput.click()
LastNameInput.send_keys(LastName)
# Locate FirstNameInput and click on it and enter FirstNameI
FirstNameInput = driver.find_element(By.ID, 'b7-FirstNameInput')
FirstNameInput.click()
FirstNameInput.send_keys(FirstName)
# Locate DOBInput and click on it and enter DOB
DOBInput = driver.find_element(By.ID, 'b7-DOBInput')
DOBInput.click()
#DOBInput.send_keys('YYYY-MM-DD')
DOBInput.send_keys(DOB)
# Explicit Wait for Submit Button & click on it
SubmitButton = WebDriverWait(driver, 15).until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'button.margin-top-m')))
SubmitButton.click()
# *****Explicit Wait for Search Button & click on it to accept default 1 year of data *****THIS BUTTON IS NOT CLICKED INTERMITTENTLY AND THE STALE ELEMENT REFERENCE EXCEPTION OCCURS AT THIS POINT*****
SearchButton = WebDriverWait(driver, 15).until(EC.element_to_be_clickable((By.CSS_SELECTOR, '.btn-label')))
SearchButton.click()
#Wait
time.sleep(3)
# print MOCID, NAME & then the value of the value attribute
RemainDeductible = (WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "b8-b36-Input_RemainAmtYr1"))).get_attribute("value"))
#Save MOCID, Name & Remaining Due to a CSV file
POutput = ','.join((MOCEmployeeNumber, LastName, FirstName, DOB, RemainDeductible)) + 'n'
#Locate Change Employee Link & click on it
SelectButton = driver.find_element(By.CSS_SELECTOR, '#b7-b3-Column4 > div:nth-child(1) > a:nth-child(1) > span:nth-child(1)')
SelectButton.click()
如何防止这种过时的元素引用异常?
可能在HTML DOM中第一次尝试时无法访问该元素。
你可以尝试在这样的循环中再次执行点击:
代码:
# *****Explicit Wait for Search Button & click on it to accept default 1 year of data *****THIS BUTTON IS NOT CLICKED INTERMITTENTLY AND THE STALE ELEMENT REFERENCE EXCEPTION OCCURS AT THIS POINT*****
attempts = 0
while attempts < 2:
try:
SearchButton = WebDriverWait(driver, 15).until(EC.element_to_be_clickable((By.CSS_SELECTOR, '.btn-label')))
SearchButton.click()
break
except StaleElementReferenceException as e:
print('')
# print('Could not click', e.msg)
attempts = attempts + 1
您可能可以在attempts < 5:
时尝试