Python selenium webdriver Chrome - Chrome由自动化软件控制

时间:2017-04-14 10:41:23

标签: python selenium-chromedriver

我正在使用selenium webdriver并创建一个chrome驱动程序实例。当我尝试向下滚动并向上滚动时,我收到以下错误消息。此外,我可以看到错误消息“Chrome正在由自动化软件控制”。虽然我无法在Chrome浏览器中更改任何设置以允许自动化,但很难看到此错误。

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait as wait
from selenium.common.exceptions import TimeoutException

from log_util.logger import log as Log

import time

log = Log()


class Wiki(object):
    def __init__(self):
        self.link = 'http://wikipedia.org'
        self.path = '/Users/swadhikar_c/Downloads/chromedriver'
        self.driver = webdriver.Chrome(executable_path=self.path)

    def open_wiki(self):
        driver = self.driver
        driver.get(self.link)
        return self.wait_for_page_load()

    def wait_for_page_load(self, timeout=200):
        is_element_located = EC.presence_of_element_located((By.CLASS_NAME, 'central-featured-logo'))

        try:
            wait(self.driver, timeout=int(timeout)).until(is_element_located)
            print("Page loaded successfully")
            time.sleep(5)
        except TimeoutException:
            print("Operation timed out")
            raise

        return 1

    def scroll_down_up(self):
        driver = self.driver

        element = driver.find_element(By.TAG_NAME, 'html')
        element.send_keys(Keys.END)

        print("Scrolled down to the end!")
        time.sleep(5)

        element.send_keys(Keys.HOME)

        print("Scrolled up to the top!")
        time.sleep(5)

    def close_driver(self):
        self.driver.quit()


if __name__ == '__main__':
    wiki = Wiki()

    try:
        if not wiki.open_wiki():
            print("Operation failed")

        wiki.scroll_down_up()

    finally:
        wiki.close_driver()

错误讯息:

  

/System/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7 /Users/swadhikar_c/pycharm_projects/pyselenium/PySelenium/test/selenium_test.py   页面加载成功   Traceback(最近一次调用最后一次):     文件“/Users/swadhikar_c/pycharm_projects/pyselenium/PySelenium/test/selenium_test.py”,第64行,       wiki.scroll_down_up()     在scroll_down_up中输入文件“/Users/swadhikar_c/pycharm_projects/pyselenium/PySelenium/test/selenium_test.py”,第43行       element.send_keys(Keys.END)     在send_keys中输入文件“/Library/Python/2.7/site-packages/selenium/webdriver/remote/webelement.py”,第349行       'value':keys_to_typing(value)})     在_execute中输入文件“/Library/Python/2.7/site-packages/selenium/webdriver/remote/webelement.py”,第493行       return self._parent.execute(command,params)     文件“/Library/Python/2.7/site-packages/selenium/webdriver/remote/webdriver.py”,第249行,执行       self.error_handler.check_response(响应)     在check_response中输入文件“/Library/Python/2.7/site-packages/selenium/webdriver/remote/errorhandler.py”,第193行       提出exception_class(消息,屏幕,堆栈跟踪)   selenium.common.exceptions.WebDriverException:消息:未知错误:无法聚焦元素     (会话信息:chrome = 57.0.2987.133)     (驱动信息:chromedriver = 2.29.461585(0be2cd95f834e9ee7c46bcc7cf405b483f5ae83b),platform = Mac OS X 10.12.4 x86_64)

Process finished with exit code 1

Chrome Message:

1 个答案:

答案 0 :(得分:3)

我做了一些挖掘并发现它已经解决了。刚刚修改了init函数,如下所示。

from selenium.webdriver.chrome.options import Options

def __init__(self):
    self._link = 'https://in.norton.com/'
    _path = '/Users/swadhikar_c/Downloads/chromedriver'
    _chrome_options = Options()
    _chrome_options.add_argument('disable-infobars')
    self.driver = webdriver.Chrome(executable_path=_path, chrome_options=_chrome_options)