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

时间:2021-02-12 18:20:44

I am getting error issues all of a sudden with selenium and the chromedriver. I haven't changed a single thing yet I am met with these error messages. The script literally worked hours ago and now without any tweaks its not working.

我一直在用硒和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()

Here is my corresponding code:

这是我的相应代码:

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()

Even more strange is that when open new terminal, load python, and type from selenium import webdriver, i dont get any errors. But, when I navigate to the folder where the script lives, and load python and type from selenium import webdriver, i get the error message that shows up above. I hope this can give some insight into my current predicament.

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

2 个解决方案

#1


0  

A few words about the solution :

关于解决方案的几句话:

  • email is a reserved word / keyword in Python Language, avoid using the word email within user defined filename/methods/classes.
  • email是Python语言中的保留字/关键字,避免在用户定义的文件名/方法/类中使用单词email。

  • pageLoadStrategy as eager is yet to be implemented in ChromeDriver, use either none or normal instead as per your requirement.
  • pageLoadStrategy as eager尚未在ChromeDriver中实现,请根据您的要求使用none或normal。

  • To maximize the Chrome Browser Window instead of maximize_window() use the argument start-maximized through ChromeOptions()
  • 要最大化Chrome浏览器窗口而不是maximize_window(),请使用通过ChromeOptions()启动最大化的参数

  • To load an extension use ChromeOptions as follows :

    要加载扩展程序,请使用ChromeOptions,如下所示:

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

Here are the four methods to initialize Chrome Browser through ChromeDriver :

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

  • Vanila Method :

    Vanila方法:

    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()
    
  • Arguments as ChromeOptions :

    作为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()
    
  • Capabilities as DesiredCapabilities :

    能力为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()
    
  • Arguments as ChromeOptions and Capabilities as DesiredCapabilities :

    作为DesiredCapabilities的ChromeOptions和Capabilities的参数:

    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()
    

#2


0  

You might want to change

你可能想要改变

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

to

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

You seem to have missed .exe, the extension of the executable file.

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

#1


0  

A few words about the solution :

关于解决方案的几句话:

  • email is a reserved word / keyword in Python Language, avoid using the word email within user defined filename/methods/classes.
  • email是Python语言中的保留字/关键字,避免在用户定义的文件名/方法/类中使用单词email。

  • pageLoadStrategy as eager is yet to be implemented in ChromeDriver, use either none or normal instead as per your requirement.
  • pageLoadStrategy as eager尚未在ChromeDriver中实现,请根据您的要求使用none或normal。

  • To maximize the Chrome Browser Window instead of maximize_window() use the argument start-maximized through ChromeOptions()
  • 要最大化Chrome浏览器窗口而不是maximize_window(),请使用通过ChromeOptions()启动最大化的参数

  • To load an extension use ChromeOptions as follows :

    要加载扩展程序,请使用ChromeOptions,如下所示:

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

Here are the four methods to initialize Chrome Browser through ChromeDriver :

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

  • Vanila Method :

    Vanila方法:

    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()
    
  • Arguments as ChromeOptions :

    作为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()
    
  • Capabilities as DesiredCapabilities :

    能力为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()
    
  • Arguments as ChromeOptions and Capabilities as DesiredCapabilities :

    作为DesiredCapabilities的ChromeOptions和Capabilities的参数:

    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()
    

#2


0  

You might want to change

你可能想要改变

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

to

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

You seem to have missed .exe, the extension of the executable file.

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