首页 > 解决方案 > 无法在浏览器中打开网站网址

问题描述

我使用 Selenium 在 Python 中创建了简单的基本自动化脚本。得到不需要的异常。

文件:-

import pandas as pd
from pandas import ExcelWriter
from selenium import webdriver

import selenium as sel

# Data = pd.read_excel(r"C:\Users\Admin\PycharmProjects\Web_Automation_Form_Filling\challenge.xlsx",sheet_name="Sheet1")

# browser = webdriver.Chrome(executable_path=r'C:\Program Files (x86)\Google\Chrome\Application\chrome.exe')
browser = webdriver.Chrome("C:\Program Files (x86)\Google\Chrome\Application\chrome.exe");
browser.sleep(1000);
browser.get("http://www.python.org")

错误日志:-

C:\Users\Admin\PycharmProjects\Web_Automation_Form_Filling\venv\Scripts\python.exe C:/Users/Admin/PycharmProjects/Web_Automation_Form_Filling/venv/Web_Auto_Filling.py
Traceback (most recent call last):
  File "C:/Users/Admin/PycharmProjects/Web_Automation_Form_Filling/venv/Web_Auto_Filling.py", line 10, in <module>
    browser = webdriver.Chrome("C:\Program Files (x86)\Google\Chrome\Application\chrome.exe");
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python38-32\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 73, in __init__
    self.service.start()
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python38-32\lib\site-packages\selenium\webdriver\common\service.py", line 98, in start
    self.assert_process_still_running()
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python38-32\lib\site-packages\selenium\webdriver\common\service.py", line 109, in assert_process_still_running
    raise WebDriverException(
selenium.common.exceptions.WebDriverException: Message: Service C:\Program Files (x86)\Google\Chrome\Application\chrome.exe unexpectedly exited. Status code was: 0


Process finished with exit code 1

任何建议将不胜感激..谢谢...

标签: pythonseleniumselenium-webdriver

解决方案


此代码应该可以工作(最好使用 firefox for selenium):

from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.common.keys import Keys
from bs4 import BeautifulSoup

# noinspection PyUnresolvedReferences
import wget

DesiredCapabilities.PHANTOMJS[
    "phantomjs.page.settings.userAgent"
] = "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:16.0) Gecko/20121026 Firefox/16.0"

if browser == "firefox":
    driver = webdriver.Firefox()
else:
    driver = webdriver.PhantomJS(
        CFG_phantomjs
    )  # r"D:/_devs/webserver/phantomjs-1.9.8/phantomjs.exe"

driver.get("https://tourwebsite")
username = driver.find_element_by_id("login_field")
password = driver.find_element_by_id("password")
username.clear()

推荐阅读