如何通过Chromedriver和Selenium启动Chrome浏览器

时间:2018-05-03 00:16:29

标签: python selenium selenium-webdriver webdriver selenium-chromedriver

硒和chromedriver突然出现错误问题。我还没有改变一件事,但我遇到了这些错误消息。该剧本几个小时前完成了工作,现在没有任何调整,它无法正常工作。

 traceback (most recent call last):
  File "email.py", line 3, in <module>
    from selenium import webdriver
  File "C:\ProgramData\Anaconda3\lib\site-packages\selenium\webdriver\__init__.py", line 18, in <module>
    from .firefox.webdriver import WebDriver as Firefox  # noqa
  File "C:\ProgramData\Anaconda3\lib\site-packages\selenium\webdriver\firefox\webdriver.py", line 20, in <module>
    import http.client as http_client
  File "C:\ProgramData\Anaconda3\lib\http\client.py", line 71, in <module>
    import email.parser
  File "C:\Users\Doe Labs\Desktop\Austin\Scripts\email.py", line 12, in <module>
    options = webdriver.ChromeOptions()

这是我的相应代码:

import pyautogui
import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.support.ui import WebDriverWait

caps = DesiredCapabilities().CHROME
#caps["pageLoadStrategy"] = "eager"

options = webdriver.ChromeOptions() 
options.add_argument(r'load-extension=C:\Users\Doe Labs\Desktop\Austin\sales_prospecting\facebookpixelhelper')
#options.add_argument('start-fullscreen')
options.add_argument('disable-infobars')
driver=webdriver.Chrome(desired_capabilities = caps, executable_path=r'C:\Users\Doe Labs\Desktop\Austin\sales_prospecting\chromedriver', chrome_options=options)

driver.get('http://www.doelabs.com/')
driver.maximize_window()

更奇怪的是,当打开新终端,加载python,并从selenium import webdriver输入时,我不会收到任何错误。但是,当我导航到脚本所在的文件夹,并从selenium import webdriver加载python和type时,我收到上面显示的错误消息。我希望这可以让我对目前的困境有所了解。

2 个答案:

答案 0 :(得分:0)

您可能想要更改

executable_path=r'C:\Users\Doe Labs\Desktop\Austin\sales_prospecting\chromedriver',

executable_path=r'C:\Users\Doe Labs\Desktop\Austin\sales_prospecting\chromedriver.exe',

您似乎错过了.exe,可执行文件的扩展名。

答案 1 :(得分:0)

关于解决方案的几句话:

  • 电子邮件 Python语言中的保留字 / 关键字,请避免使用电子邮件在用户定义的文件名/方法/类中。
  • pageLoadStrategy由于 eager 尚未在 ChromeDriver 中实施,请使用 none 或根据您的要求 normal
  • 要最大化 Chrome浏览器窗口而不是maximize_window(),请使用参数 start-maximized ChromeOptions() < / LI>
  • 要加载扩展程序,请使用 ChromeOptions ,如下所示:

    options.addExtensions(new File("/path/to/extension.crx"));
    

以下是通过 ChromeDriver 初始化 Chrome浏览器的四种方法:

  • Vanila Method

    from selenium import webdriver
    
    driver = webdriver.Firefox(r'C:\path\to\chromedriver.exe')
    driver.get('http://www.doelabs.com/')
    print("Page Title is : %s" %driver.title)
    driver.quit()
    
  • ChromeOptions 的参数:

    from selenium import webdriver
    
    options = webdriver.ChromeOptions() 
    options.addExtensions(new File("C:\Users\Doe Labs\Desktop\Austin\sales_prospecting\facebookpixelhelper.crx"));
    options.add_argument('start-maximized')
    options.add_argument('disable-infobars')
    driver=webdriver.Chrome(chrome_options=options, executable_path=r'C:\path\to\chromedriver.exe')
    driver.get('http://www.doelabs.com/')
    print("Page Title is : %s" %driver.title)
    driver.quit()
    
  • 能力为 DesiredCapabilities

    from selenium import webdriver
    from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
    
    caps = DesiredCapabilities().CHROME.copy()
    caps["pageLoadStrategy"] = "normal"
    driver = webdriver.Chrome(executable_path=r'C:\path\to\chromedriver.exe', desired_capabilities=caps)
    driver.get('http://www.doelabs.com/')
    print("Page Title is : %s" %driver.title)
    driver.quit()
    
  • ChromeOptions 的参数和 DesiredCapabilities 的功能:

    from selenium import webdriver
    from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
    
    caps = DesiredCapabilities().CHROME.copy()
    caps["pageLoadStrategy"] = "normal"
    
    options = webdriver.ChromeOptions() 
    options.addExtensions(new File("C:\Users\Doe Labs\Desktop\Austin\sales_prospecting\facebookpixelhelper.crx"));
    options.add_argument('start-maximized')
    options.add_argument('disable-infobars')
    driver=webdriver.Chrome(chrome_options=options, executable_path=r'C:\path\to\chromedriver.exe', desired_capabilities=caps)
    driver.get('http://www.doelabs.com/')
    print("Page Title is : %s" %driver.title)
    driver.quit()