selenium.common.exceptions.WebDriverException: Message: 未知错误:使用ChromeDriver和Selenium Python无法创建Chrome进程

5
我正在尝试使用Selenium点击按钮,因此我使用xpath找到了元素,因为我找不到id。编辑:我认为代码的其余部分与此无关,但我添加了它以防万一。
这是代码:
import requests
import os
import selenium
from selenium import webdriver
os.system("cls")
print("                                         ")
print("______________     _____________         ________________")
print("| |         | |    |  |       | |        |______________|")
print("| |________ //     |  |  __   | |               | |")
print("| |________ \\\\     |  | |__|  | |               | |")
print("| |          | |   |  |       | |               | |")
print("| |__________| |   |__|_______|_|               |_|")

print("\u001b[34m Welcome To Movie Downloader")
print("\u001b[31m Please Make Sure To Not Put Every First Letter In Every Word Capital And Also Make Sure To Put Hyphens Instead Of Spaces Between Words, Also Put The Date The Movie Was Made")
print("\u001b[32m For Example: spider-man-homecoming-2017")


def Bot():
    URL = input("\u001b[34m What Movie Do You Want To Download:\n")
    r = requests.get("https://bila.egy.best/movie/" + URL + "/?ref=search-p1")
    if r.status_code == 200:
        print("\u001b[32m The Url Is Valid | Movie Has Been Found")
    else:
        print("\u001b[31m The Url Is Invalid")
    print("\u001b[0m")
    driver = webdriver.Chrome(executable_path=r'C:\chromedriver.exe')
    driver.get("https://bila.egy.best/movie/" + URL + "/?ref=search-p1")
    driver.find_element_by_xpath("//*[@id=watch_dl]/table/tbody/tr[2]/td[4]/a[1]").click()
Answer = input("Would You Like To Bot?")
if Answer == "Yes" or "yes" or "sure" or "Sure":
    Bot()

这是错误信息

Traceback (most recent call last):
  File "Movie_Download.py", line 32, in <module>
    Bot()
  File "Movie_Download.py", line 27, in Bot
    driver = webdriver.Chrome(executable_path="C:\chromedriver.exe")
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 81, in __init__
    desired_capabilities=desired_capabilities)
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 157, in __init__
    self.start_session(capabilities, browser_profile)
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 252, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: Failed to create a Chrome process.

你使用的是哪个版本的Chrome和ChromeDriver?这似乎是浏览器和驱动程序版本不匹配的问题。 - Greg Burghardt
我有ChromeDriver版本ChromeDriver 79.0.3945.36和Chrome版本79.0.3945.88。 - user12601652
这可能会有所帮助:https://dev59.com/aq3la4cB1Zd3GeqPQKcm#54663888 - josifoski
如果以管理员身份运行Chrome可以解决问题,也许可以更改Chrome安装的文件夹,或更改文件权限,这样就不需要以管理员身份运行了? - Greg Burghardt
3个回答

5

您需要注意以下几点:

  • While passing the absolute location of chromedriver binary use a single forward slash along with the raw i.e. r switch. So the effective line of code will be:

    driver = webdriver.Chrome(executable_path=r'C:\chromedriver.exe')
    
  • Execute your @Test as non-administrator / non-root user.


更新

另一种可能的原因是,Chrome未按照规范安装在默认位置:

Chrome_default_location

解决方案

有两种方法可以解决这种情况:

  • Uninstall Chrome and reinstall Chrome at default location.
  • Use binary_location property to point to the chrome binary location.

    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options
    
    options = Options()
    options.binary_location = "C:/Program Files (x86)/Google/Chrome/Application/chrome.exe"
    driver = webdriver.Chrome(chrome_options=options, executable_path="C:/Utility/BrowserDrivers/chromedriver.exe", )
    driver.get('http://google.com/')
    

驱动程序 = webdriver.Chrome(executable_path=r'C:\chromedriver.exe')。错误仍然相同。 - user12601652
@mohamedelgamal 那非管理员/非root用户怎么办? - undetected Selenium
是的,非管理员命令提示符仍然无法工作。 - user12601652
@mohamedelgamal,请查看更新后的答案并告知状态。 - undetected Selenium
我检查了Chrome是否在默认位置,然后意识到我甚至没有AppData文件。 - user12601652
显示剩余2条评论

1
如果您仍然遇到此错误,请尝试重新安装Chrome浏览器。
这对我有用,我遇到了同样的问题,尝试了所有方法但仍无法解决,但在重新安装Chrome浏览器后,问题得以解决!

0

你需要编辑

driver = webdriver.Chrome(executable_path="C:\chromedriver.exe")

driver = webdriver.Chrome(executable_path="C:\\chromedriver.exe")

你漏了 \

如果你得到同样的错误,请尝试以管理员身份运行

或者

chromedriver.exe 移动到其他路径,例如 c:/seleniumdriver/chromedriver.exe ,并编辑 executable_path


你有chromedriver.exe吗?https://chromedriver.storage.googleapis.com/80.0.3987.16/chromedriver_win32.zip - joonghyup cha
你可以尝试使用 driver = webdriver.Chrome(executable_path="C:/chromedriver.exe") 这段代码。 - joonghyup cha
仍然无法工作,我认为这与斜杠无关。 - user12601652
你是否遇到了类似的错误selenium.common.exceptions.WebDriverException: Message: unknown error: Failed to create a Chrome process. - joonghyup cha
如果您遇到相同的错误,请尝试以管理员身份运行或移动chromedriver.exe并编辑executable_path - joonghyup cha
在管理员模式下无法运行该文件。 - user12601652

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接