【问题标题】:StaleElementReferenceException on Python Selenium while using for loop使用 for 循环时 Python Selenium 上的 StaleElementReferenceException
【发布时间】:2021-10-26 09:37:58
【问题描述】:

我的流程是有一个餐厅列表,我想点击它并做一些事情,然后使用循环返回列表中的下一个餐厅,如下图所示 我关注了this link,但没有得到澄清

代码

import time
from selenium import webdriver
from selenium.webdriver.common.by import By


driver = webdriver.Chrome(executable_path='../chromedriver.exe')
driver.get("https://www.zomato.com/kolkata/dine-out?dishv2_id=76487754bd59c594cd5218d3427e68e0_2&rating_range=4.0-5.0")
screen_height = driver.execute_script("return window.screen.height;")  # get the screen height of the web
i = 1
count = 0
scroll_pause_time = 1

while True:
    # scroll one screen height each time
    driver.execute_script("window.scrollTo(0, {screen_height}*{i});".format(screen_height=screen_height, i=i))
    i += 1
    time.sleep(scroll_pause_time)   
    # update scroll height each time after scrolled, as the scroll height can change after we scrolled the page
    scroll_height = driver.execute_script("return document.body.scrollHeight;")
    # Break the loop when the height we need to scroll to is larger than the total scroll height
    if (screen_height) * i > scroll_height:
        break

driver.execute_script("window.scrollTo(0, 0);")
res_list = []
titles = driver.find_elements_by_xpath('//a[@class="sc-dBAPYN kcrxQo"]')

#this is block of code which is error prone

for i in titles:
    time.sleep(5)
    driver.execute_script("window.scrollTo(0, 0);")
    element = i.find_element_by_xpath('./div/h4')
    driver.execute_script("arguments[0].click();", element)
    time.sleep(3)
    name_of_rests = driver.find_element_by_css_selector('#root > div > main > div > section.sc-kxynE.jzTfFZ > section > section > div > div > div > h1').text
    res_list.append(name_of_rests)

    driver.back()

print(res_list)
driver.close()

【问题讨论】:

  • 请提供更多信息 - 对于哪一行代码/元素,您获得了陈旧的 ref 异常> 一般来说,为了避免陈旧的 ref - 您应该尽快尝试获取元素 ref(通过其适当的定位器)准备对其执行操作。

标签: python selenium web-scraping


【解决方案1】:

你必须定义这个

 driver.find_elements_by_xpath('//a[@class="sc-dBAPYN kcrxQo"]')

再次循环。

代码:

number_of_titles = len(driver.find_elements_by_xpath('//a[@class="sc-dBAPYN kcrxQo"]'))

#this is block of code which is error prone
j = 0 
for i in range(number_of_titles):
    time.sleep(5)
    titles = driver.find_elements_by_xpath('//a[@class="sc-dBAPYN kcrxQo"]')
    driver.execute_script("window.scrollTo(0, 0);")
    element = titles[j].find_element_by_xpath('.//div//h4')
    driver.execute_script("arguments[0].click();", element)
    time.sleep(3)
    j = j +1
    name_of_rests = driver.find_element_by_css_selector('#root > div > main > div > section.sc-kxynE.jzTfFZ > section > section > div > div > div > h1').text
    res_list.append(name_of_rests)

    driver.back()

【讨论】:

  • 我在我的代码中使用了这段代码然后我得到这个错误 element =titles[j].find_element_by_xpath('.//div//h4') IndexError: list index out of range
  • 在这种情况下您必须修复 for 循环,尝试在前后打印 j 的值以清楚地了解循环是如何执行的
  • 请检查您的邮件一次
  • 代码很多。暂时做不到,过段时间我看看。
猜你喜欢
  • 2015-01-16
  • 2016-11-16
  • 2018-09-14
  • 1970-01-01
  • 1970-01-01
  • 2014-11-15
  • 1970-01-01
  • 1970-01-01
  • 2021-02-12
相关资源
最近更新 更多