1

我在 Python 上使用 Selenium 并尝试移动光标并单击特定元素。这适用于第一个链接,并且 HTML 的结构对于下一个链接是相同的,但是当通过相同的 webdriver 访问第二个链接时,我得到了第二个链接的 StaleElementReferenceException。为什么会发生这种情况,我该如何解决?下面是我正在运行的代码。太感谢了!

def getZest(url):
    zestlist = []
    yearlist = []
    driver.get(url)
    time.sleep(5)
    
    
    result = False;
    attempts = 0;
    while(attempts < 5):
        try:
            Home_Value = wait.until(EC.presence_of_element_located((By.XPATH, "//a[text()='Home value']")))
            action.move_to_element(Home_Value).click().perform()
    
            zestimate = driver.find_element_by_xpath('//*[@id="ds-home-values"]/div/div[3]/button')
            action.move_to_element(zestimate).perform()
            result = True
            break
        except exceptions.StaleElementReferenceException as e:
            print(e)
        attempts = attempts + 1
fivenums = ["https://www.zillow.com/homedetails/212-Haddrell-St-Mount-Pleasant-SC-29464/10922911_zpid/", "https://www.zillow.com/homedetails/20-Grove-St-Hicksville-NY-11801/31127407_zpid/"]
for num in fivenums:
    getZest(num)
4

1 回答 1

0

I was able to get informations about the first and second link with the following code, without any error:

from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
import selenium.webdriver.support.expected_conditions as EC
from selenium.webdriver.common.by import By
import time
from selenium.common import exceptions

u = 'https://www.oddsportal.com/moving-margins/'
driver = webdriver.Chrome(executable_path=r"chromedriver.exe")
driver.maximize_window()

def getZest(url):
    zestlist = []
    yearlist = []
    driver.get(url)
    time.sleep(5)
    
    
    result = False
    attempts = 0
    action = webdriver.ActionChains(driver)
    wait = WebDriverWait(driver, 300)
    while(attempts < 5):
        try:
            Home_Value = wait.until(EC.presence_of_element_located((By.XPATH, "//a[text()='Home value']")))
            action.move_to_element(Home_Value).click().perform()
    
            zestimate = driver.find_element_by_xpath('//*[@id="ds-home-values"]/div/div[3]/button')
            action.move_to_element(zestimate).perform()
            result = True
            break
        except exceptions.StaleElementReferenceException as e:
            print(e)
        attempts = attempts + 1
fivenums = ["https://www.zillow.com/homedetails/212-Haddrell-St-Mount-Pleasant-SC-29464/10922911_zpid/", "https://www.zillow.com/homedetails/20-Grove-St-Hicksville-NY-11801/31127407_zpid/"]
for num in fivenums:
    getZest(num)

In your code there it's not showed where some variables are instantiated so, maybe this is were the problem is located.

However when opening the first link, the website showed me the Google Captcha protection, so, I suppose you have some kind of authorization to scrape the informations with the permission of the owner.

于 2020-09-06T15:59:21.820 回答