首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >陈旧异常web抓取selenium python

问陈旧异常web抓取selenium python
EN

Stack Overflow用户
提问于 2015-12-08 11:55:45
回答 1查看 365关注 0票数 0

我正在尝试从tripadvisor获取信息。我有时会得到

消息:陈旧元素引用:元素未附加到页面文档(会话信息: chrome=47.0.2526.73) (驱动程序信息: chromedriver=2.20.353124 (035346203162d32c80f1dce587c8154a1efa0c3b),platform=Mac OS X 10.10.4 x86_64)

然后元素就是我分配给它的任何东西。我如何修复我的代码来处理这个问题,然后找出解决方案,而不是重新运行代码?

代码语言:javascript
复制
  def getElements(driver):
        elements = []
        for dd in driver.find_elements_by_xpath("//*[contains(@class, 'ui_button original')]"):
            try:
                if dd.text == "Book Now":
                    elements.append(dd)
            except Exception as ee:
                print ee
        return elements 

def getBookingPartner(driver, ibInfo):
    data = []
    i = 0 
    elements = []
    time.sleep(2)
    elements = getElements(driver)
    elementCounter = 0
    while(elements == [] or elementCounter >5):
        elements = getElements(driver)
        elementCounter+=1

    print "Length of elements should be > 0 : " + str(len(elements))
    for ii in ibInfo:
        if ii[0] == "Yes":
            driver.implicitly_wait(3)
            bookingPartner = "Error"    
            print ii    
            driver.implicitly_wait(3)
            try:
                elements[i].click()
                driver.implicitly_wait(3)
                driver.switch_to_window(driver.window_handles[-1])
            except Exception as ee:
                try:
                    driver.refresh()
                    getElements(driver)[i].click()
                    time.sleep(1)
                    driver.switch_to_window(driver.window_handles[-1])
                except Exception as ee:
                    print "Stale Exception...."
                    print ee    
            try:
                driver.implicitly_wait(3)
                driver.find_elements_by_xpath("//*[contains(@class, 'book_now')]")[1].click()
                driver.implicitly_wait(1)
                page = etree.HTML(driver.page_source)
                bookingPartner = page.xpath("//div[contains(@class, 'custServiceMsg')]//text()")[0].split("will")[0].strip()                    
            except:
                try:
                    time.sleep(3)
                    driver.find_elements_by_xpath("//*[contains(@class, 'book_now')]")[1].click()
                    time.sleep(2)
                    page = etree.HTML(driver.page_source)
                    bookingPartner = page.xpath("//div[contains(@class, 'custServiceMsg')]//text()")[0].split("will")[0].strip()                    
                except:
                    try:
                        bookingPartner = page.xpath("//div[contains(@class, 'custServiceMsg')]//text()")[1].split("will")[0].strip()
                    except Exception as ee:
                        bookingPartner = "Error"
                        print "error"
            i+=1

            if bookingPartner == "The remainder":
                bookingPartner = page.xpath("//div[contains(@class, 'custServiceMsg')]//text()")[1].split("will")[0].strip()

            if len(driver.window_handles) > 1:
                driver.close()
                driver.switch_to_window(driver.window_handles[0])

            print bookingPartner

            data.append([ii[0], ii[1], bookingPartner])
        else:
            data.append([ii[0], ii[1], "N/A"])
            ii.extend(["N/A"])

        print data
    return data
EN

回答 1

Stack Overflow用户

发布于 2015-12-08 12:33:48

当以下元素出现时,将出现Stale Element Reference Exception:

  1. 已被删除
  2. 已不再附加到DOM (与您的情况相同)
  3. 已更改

从文档中:

您应该丢弃当前持有的引用并替换它,可能的方法是在元素附加到DOM后再次定位它。

即:再次“查找”该元素。

您需要修改代码,以便在适当的步骤中捕获此错误。

代码语言:javascript
复制
from selenium.common.exceptions import StaleElementReferenceException

elem = driver.find_element_by_xpath('something leaves dom')
# ... do other actions which change the page and then later...
try:
    elem.click()
except StaleElementReferenceException:
    elem = driver.find_element_by_xpath('something leaves dom')
    elem.click()

如果你对几个元素有广泛的需求,那就做一个可重用的版本。

顺便说一句,你不应该在你的代码中捕获 Exception。具体说明你想要处理的是哪些。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34147736

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档