Python:Selenium Webdriver Chrome打开浏览器,但未打开所选的网站

时间:2020-04-10 23:09:10

标签: python selenium google-chrome selenium-webdriver webdriver

我刚刚下载了Anaconda(v 1.7.2)和spyder(Python 3.7.6)。 我有Chrome(版本81.0.4044.92 64位) 硒版本3.141.0

当我在下面编写代码时,得到的响应是chrome打开了一个新实例和一个选项卡,但只有起始页面,它从未打开过我指出的站点。 我也尝试过关闭所有其他chrome实例,但这没有帮助。 我在系统环境变量中添加了以下内容 C:\ Users \ aaa.bbb \ Anaconda3 \ Scripts; c:\ users \ aaa.bbb \ anaconda3 \ lib \ site-packages \ chromedriver;


from selenium import webdriver

from bs4 import BeautifulSoup

import pandas as pd

import time

driver = webdriver.Chrome("C:\Program Files (x86)\Google\Chrome\Application\chrome.exe")

driver.get('http://www.aftonbladet.se');

time.sleep(3)

driver.quit()

我在间谍中得到的答复是:

runfile('C:/Users/aaa.bbb/.spyder-py3/temp.py', wdir='C:/Users/aaa.bbb/.spyder-py3')
Traceback (most recent call last):

  File "C:\Users\aaa.bbb\.spyder-py3\temp.py", line 8, in <module>
    driver = webdriver.Chrome("C:\Program Files (x86)\Google\Chrome\Application\chrome.exe")

  File "C:\Users\aaa.bbb\Anaconda3\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 73, in __init__
    self.service.start()

  File "C:\Users\aaa.bbb\Anaconda3\lib\site-packages\selenium\webdriver\common\service.py", line 98, in start
    self.assert_process_still_running()

  File "C:\Users\aaa.bbb\Anaconda3\lib\site-packages\selenium\webdriver\common\service.py", line 111, in assert_process_still_running
    % (self.path, return_code)

WebDriverException: Service C:\Program Files (x86)\Google\Chrome\Application\chrome.exe unexpectedly exited. Status code was: 0

2 个答案:

答案 0 :(得分:1)

您需要使用Chrome Webdriver而不是实际的Chrome应用程序。 Download Chrome Webdriver for Windows.

编辑此行,使其在下载后指向Webdriver。

driver = webdriver.Chrome("C:\Program Files (x86)\Google\Chrome\Application\chrome.exe")

答案 1 :(得分:1)

请下载支持您浏览器版本的最新chrome驱动程序: 您可以下载Latest Chrome driver from here,其中r表示相对路径::

from selenium import webdriver
driver = webdriver.Chrome(executable_path=r" path to your chromedriver.exe")
driver.maximize_window()
baseUrl = "https://www.google.com"
driver.get(baseUrl)