python - Selenium - 为什么 NoSuchElementException 在第二次 for 循环迭代中发生?
问题描述
我正在尝试遍历与 div 标签匹配的 Web 元素列表。第一个循环运行良好,但第二个循环抛出一个NoSuchElementException
. 这是我的代码的最小示例:
for div in driver.find_elements_by_xpath("//div[@class='class_name']"):
print(div.text)
print(f"Current url 1: {driver.current_url}") # url
new_url = url + "/page/"
time.sleep(2)
driver.get(new_url)
print(f"Current url 2: {driver.current_url}") # new_url
time.sleep(2)
# Then get info from the new url
# Go back
# driver.execute_script("window.history.go(-1)")
driver.back()
print(f"Current url 3: {driver.current_url}") # url
print("Sleeping for 3 seconds from now...")
time.sleep(3)
谢谢!
解决方案
您得到StaleElementReferenceException
的原因是对您尝试使用的 Web 元素的引用不再是有效的 AKA 陈旧。
请参阅此处或有关过时元素引用异常的任何其他资源。
由于您访问了其他网页,即使您回到初始网页,您获得的所有网页元素都会变成陈旧的元素。
要克服这个问题,您必须再次获得这些元素。
因此,我建议您使用以下代码,而不是您当前的代码:
divs = driver.find_elements_by_xpath("//div[@class='class_name']")
for i in range(len(divs)):
divs = driver.find_elements_by_xpath("//div[@class='class_name']")
div = divs[i]
print(div.text)
print(f"Current url 1: {driver.current_url}") # url
new_url = url + "/page/"
time.sleep(2)
driver.get(new_url)
print(f"Current url 2: {driver.current_url}") # new_url
time.sleep(2)
# Then get info from the new url
# Go back
# driver.execute_script("window.history.go(-1)")
driver.back()
print(f"Current url 3: {driver.current_url}") # url
print("Sleeping for 3 seconds from now...")
time.sleep(3)
您可以尝试在循环中获取特定的 div,如下所示:
divs = driver.find_elements_by_xpath("//div[@class='class_name']")
for i in range(len(divs)):
div = driver.find_element_by_xpath("(//div[@class='class_name'])[" + (str)i + "]")
print(div.text)
print(f"Current url 1: {driver.current_url}") # url
new_url = url + "/page/"
time.sleep(2)
driver.get(new_url)
print(f"Current url 2: {driver.current_url}") # new_url
time.sleep(2)
# Then get info from the new url
# Go back
# driver.execute_script("window.history.go(-1)")
driver.back()
print(f"Current url 3: {driver.current_url}") # url
print("Sleeping for 3 seconds from now...")
time.sleep(3)
推荐阅读
- python - ctypes-defined PROCESSENTRY32 在使用 ctypes.sizeof 时会产生不正确的大小
- kubernetes - Kubernetes's http liveness probe failed when pod under heavy load
- javascript - 如何在 JavaScript 中动态添加 div?
- javascript - 你如何在 Javascript 中拆分代码?(进出口)
- bash - 为什么这个两行 bash 提示符会弄乱命令历史
- javascript - JavaScript 中 Reduce 方法的平均值
- php - 如何在序列中的同一插入上插入序列号?
- bash - Bash单行嵌套for循环将比较变量作为命令
- python - Python pandas数据框'SUMIF' - 标准范围与总和范围不同
- c++ - 在OpenGL的角落显示文本