简体   繁体   中英

Python: Selenium webdriver Chrome opens up browser but not the site selected

I have just downloaded Anaconda (v 1.7.2) and spyder (Python 3.7.6). I have chrome (Version 81.0.4044.92 64-bit) Selenium version 3.141.0

When I write the code below I get the response that chrome opens up a new instance and a tab, but only the starting page, it never opens up the site I have pointed out. I have tried to close all other chrome instances also but that didn't help. I have added the following to the system environment variables 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()

The response I get in spyder is:

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

You need to use a Chrome Webdriver instead of using the actual Chrome application. Download Chrome Webdriver for Windows.

Edit this line to point to the Webdriver after download.

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

Please download latest chrome driver which support to your browser version: you can download Latest Chrome driver from here , Here r denotes relative path::

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM