selenium无法使用python

时间:2018-04-30 14:39:17

标签: python selenium

我试图这样做的原因是因为有时候如果网络不好,那么加载/刷新最终会挂起,所以我有这个想法,首先为chromedriver设置超时,然后尝试抓住达到超时并再次重新加载时出现异常。

这是简单的代码,我试图将超时捕获功能集成到selenium中。 看来镀铬永远不会真正重新加载。即使我在load_page函数中将超时更改为100秒(这不在当前代码中。)

这是Chrome驱动程序的错误吗?为什么不重装?

import os
from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from time import sleep


def load_page(func, arg, driver):
    load_retry = 0
    while 1:
        if load_retry > 3:
            return False
        try:
            func(arg)
            print('load success!')
            return True
        except TimeoutException:
            load_retry += 1
            print('retrying')

            try:
                driver.execute_script("window.stop();")
            except TimeoutException:
                sleep(2)
                continue




chrome = os.path.join('../resources/chromedriver.exe')
options = webdriver.ChromeOptions()
# options.set_headless(headless=True)
driver = webdriver.Chrome(chrome_options=options, executable_path=chrome)
driver.maximize_window()
driver.set_page_load_timeout(5)

url = 'http://tieba.baidu.com/f?ie=utf-8&kw=c%2B%2B&fr=search&red_tag=f1842571998'

load_page(driver.get, url, driver)

driver.quit()

更新: 我实际上用webdriver.Chrome做了自己的重做,基本上我只是运行driver.quit()如果我捕获异常,然后在下一个循环中再次启动一个新的webdriver.Chrome实例。

我认为这很难看,但确实有效。

1 个答案:

答案 0 :(得分:0)

我为webdriver.Chrome做了一次返工,以便进行更强大的重载和刷新行为。随意拿它。

import os
from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from time import sleep


class BetterChrome(webdriver.Chrome):
    default_timeout = 10
    better_setting = {
        'options': '', 'path':''
    }
    def set_better_setting(self, options, path):
        BetterChrome.better_setting['options'] = options
        BetterChrome.better_setting['path'] = path
        self.set_page_load_timeout(BetterChrome.default_timeout)

    def better_refresh(self):
        return self.better_load_page(self.current_url)


    def better_load_page(self, url):
        load_retry = 0
        timeout = BetterChrome.default_timeout
        default_setting = BetterChrome.better_setting
        while 1:
            print('retrying')
            if load_retry > 8:
                return False, False
            try:
                self.get(url)
                print('load success!')
                self.set_better_setting(default_setting['options'], default_setting['path'])
                return True, self
            except TimeoutException:
                load_retry += 1
                # print('retrying')
                self.quit()

                self = BetterChrome(chrome_options=default_setting['options'], executable_path=default_setting['path'])
                self.maximize_window()
                timeout += 15
                self.set_page_load_timeout(timeout)

chrome = os.path.join('../resources/chromedriver.exe')
options = webdriver.ChromeOptions()
driver = BetterChrome(chrome_options = options, executable_path = chrome)
driver.set_better_setting(options, chrome)
url = 'https://www.google.de'
status, driver = driver.better_load_page(url)
if status:
    driver.quit()