克服python中的StaleElementReferenceException?

时间:2018-04-17 20:48:56

标签: python python-3.x selenium webdriver staleelementreferenceexception

意向:

我的目标是从whoscored.com抓取足球数据。匹配页面(see example here)包含时间轴视图(div id = match-center-timeline),它依次具有时间轴句柄(div class = timeline-handle)。可以拖放手柄,以便设置时间轴以显示匹配统计数据。例如,我想将下限时间轴设置为10分钟,上限设置为30分钟。

设置&假设

我在OSX 10上使用了带有chrome的selenium。下面的代码移动了较低的时间轴句柄,但抛出了StaleElementReferenceException。我的信念是页面应用程序因突然拖放而变得混乱。

问题:

有没有办法模拟较慢的鼠标拖动?这甚至是问题吗?怎么能克服这个?提前谢谢!

Python 3代码:

  url = "https://www.whoscored.com/Matches/1190514/Live"
    time_to_wait = 20
    wanted_element_id = "match-centre-timeline"

    opts = ChromeOptions()
    opts.add_experimental_option("detach", True)
    opts.add_argument("disable-infobars")
    opts.add_argument("disable-notifications")

    driver = webdriver.Chrome(executable_path="/Users/david/Dropbox/Code/gitCode/driver/chromedriver", chrome_options=opts)
    driver.set_page_load_timeout(time_to_wait)

    example_loaded = bool(True)
    while example_loaded:
        try:
            driver.get(url)
    WebDriverWait(driver,time_to_wait).until(EC.presence_of_element_located((By.ID, wanted_element_id)))
            print("[STATUS]\tFound " + wanted_element_id + ".") 
            example_loaded= False
        except TimeoutException:
            print("[WARNING]\tCannot find the " + wanted_element_id + ". Retrying.")

    tlh = driver.find_element_by_xpath("//*[@id='match-centre-timeline']/div[1]/div[2]/div[2]/div[2]/div[1]/span")
    tuh = driver.find_element_by_xpath("//*[@id='match-centre-timeline']/div[1]/div[2]/div[2]/div[2]/div[2]/span")

    driver.execute_script("arguments[0].scrollIntoView();", tlh)    

    actions = ActionChains(driver)

    # move to the lower timeline handle
    actions.move_to_element(tlh)
    actions.perform()

    print("[NOTE]\t\tLower handle stats: x:{}, y:{} and h:{}px, w:{}px.".format( tlh.location['x'], tlh.location['y'], tlh.size['height'], tlh.size['width'] ))
    print("[NOTE]\t\tUpper handle stats: x:{}, y:{} and h:{}px, w:{}px.".format( tuh.location['x'], tuh.location['y'], tuh.size['height'], tuh.size['width'] ))

    # let's click on the lower timeline handle
    try:
        actions.click_and_hold(tlh)
        actions.drag_and_drop_by_offset(tlh, 100, 0)
        actions.perform()
    print("[NOTE]\t\tLower handle stats: x:{}, y:{} and h:{}px, w:{}px.".format( tlh.location['x'], tlh.location['y'], tlh.size['height'], tlh.size['width'] ))
    print("[NOTE]\t\tUpper handle stats: x:{}, y:{} and h:{}px, w:{}px.".format( tuh.location['x'], tuh.location['y'], tuh.size['height'], tuh.size['width'] ))
    except StaleElementReferenceException:
        print("[WARN]\tThrew and caught a StaleElementReferenceException.")

控制台错误:

[STATUS]    Found match-centre-timeline.
[NOTE]      Lower handle stats: x:118, y:1341 and h:41px, w:19px.
[NOTE]      Upper handle stats: x:869, y:1341 and h:41px, w:30px.
[WARN]  Threw and caught a StaleElementReferenceException
See anything?
Traceback (most recent call last):
File "whoscored-scrape.py", line 442, in <module>
print("[NOTE]\t\tLower handle stats: x:{}, y:{} and h:{}px, w:{}px.".format( tlh.location['x'], tlh.location['y'], tlh.size['height'], tlh.size['width'] ))
File "/usr/local/Cellar/python3/3.6.2/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/selenium/webdriver/remote/webelement.py", line 404, in location
old_loc = self._execute(Command.GET_ELEMENT_LOCATION)['value']
File "/usr/local/Cellar/python3/3.6.2/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/selenium/webdriver/remote/webelement.py", line 493, in _execute
return self._parent.execute(command, params)
File "/usr/local/Cellar/python3/3.6.2/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 256, in execute
self.error_handler.check_response(response)
File "/usr/local/Cellar/python3/3.6.2/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/selenium/webdriver/remote/errorhandler.py", line 194, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document
(Session info: chrome=65.0.3325.181)(Driver info: chromedriver=2.36.540469 (1881fd7f8641508feb5166b7cae561d87723cfa8),platform=Mac OS X 10.12.6 x86_64)

支持图像:

0 个答案:

没有答案