我已经安装了新的Linux Mint在我的电脑。我已经安装了Github,pycharm。我下载了代码。根据软件包的要求,我已经安装了Selenium和其他软件包。下面的代码是在以前的Ubuntu Linux中工作的。现在它正在下载代码,但它不能正常工作。它抛出了错误。这是第一次,它正在下载驱动程序。2第二次出现这个错误。3两次代码都不起作用。4请帮助我。
code:-
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from webdriver_manager.chrome import ChromeDriverManager
s = Service(ChromeDriverManager().install())
chrome_options = Options()
chrome_options.add_experimental_option("detach",True)
"""
The browser will be started.
"""
browser = webdriver.Chrome(service=s, options=chrome_options)
browser.maximize_window()
browser.get("https://rahulshettyacademy.com")
print(browser.title)
browser.get("https://rahulshettyacademy.com/angularpractice/")
print(browser.title)
Error message:-
/home/halovivek/PycharmProjects/yearcoding/venv/bin/python /home/halovivek/PycharmProjects/yearcoding/26092022_selenium1.py
Traceback (most recent call last):
File "/home/halovivek/PycharmProjects/yearcoding/26092022_selenium1.py", line 13, in <module>
browser = webdriver.Chrome(service=s, options=chrome_options)
File "/home/halovivek/PycharmProjects/yearcoding/venv/lib/python3.10/site-packages/selenium/webdriver/chrome/webdriver.py", line 69, in __init__
super().__init__(DesiredCapabilities.CHROME['browserName'], "goog",
File "/home/halovivek/PycharmProjects/yearcoding/venv/lib/python3.10/site-packages/selenium/webdriver/chromium/webdriver.py", line 92, in __init__
super().__init__(
File "/home/halovivek/PycharmProjects/yearcoding/venv/lib/python3.10/site-packages/selenium/webdriver/remote/webdriver.py", line 270, in __init__
self.start_session(capabilities, browser_profile)
File "/home/halovivek/PycharmProjects/yearcoding/venv/lib/python3.10/site-packages/selenium/webdriver/remote/webdriver.py", line 363, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "/home/halovivek/PycharmProjects/yearcoding/venv/lib/python3.10/site-packages/selenium/webdriver/remote/webdriver.py", line 428, in execute
self.error_handler.check_response(response)
File "/home/halovivek/PycharmProjects/yearcoding/venv/lib/python3.10/site-packages/selenium/webdriver/remote/errorhandler.py", line 243, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: cannot find Chrome binary
Stacktrace:
#0 0x55d1d6012693 <unknown>
#1 0x55d1d5e0bb0a <unknown>
#2 0x55d1d5e2e46e <unknown>
#3 0x55d1d5e2c1f4 <unknown>
#4 0x55d1d5e67953 <unknown>
#5 0x55d1d5e61743 <unknown>
#6 0x55d1d5e37533 <unknown>
#7 0x55d1d5e38715 <unknown>
#8 0x55d1d60627bd <unknown>
#9 0x55d1d6065bf9 <unknown>
#10 0x55d1d6047f2e <unknown>
#11 0x55d1d60669b3 <unknown>
#12 0x55d1d603be4f <unknown>
#13 0x55d1d6085ea8 <unknown>
#14 0x55d1d6086052 <unknown>
#15 0x55d1d60a071f <unknown>
#16 0x7f5a5a21a1da <unknown>
#17 0x7f5a5a2a2d84 <unknown>
Process finished with exit code 1
根据建议修改了代码。
import os
from selenium import webdriver
from selenium.webdriver.firefox.service import Service as FirefoxService
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.select import Select
from selenium.webdriver.support.wait import WebDriverWait
from webdriver_manager.firefox import GeckoDriverManager
#options = Options()
options = webdriver.FirefoxOptions()
options.binary_location = r"/usr/bin/"
options.add_argument("start-maximized")
options.add_experimental_option("detach", True)
service = FirefoxService(log_path=os.devnull,)
browser = webdriver.Firefox(service= FirefoxService(GeckoDriverManager().install()), options=options)
#browser = webdriver.Firefox(service=service,options=options,)
capabilities = webdriver.FirefoxOptions().to_capabilities()
options.set_capability("cloud:options", capabilities)
browser.maximize_window()
browser.implicitly_wait(5)
browser.get("https://the-internet.herokuapp.com/windows")
print(browser.title)
仍然存在错误:-
/home/halovivek/PycharmProjects/yearcoding/venv/bin/python /home/halovivek/PycharmProjects/yearcoding/27092002_selenium3.py
[WDM] - Downloading: 16.2kB [00:00, 11.9MB/s]
Traceback (most recent call last):
File "/home/halovivek/PycharmProjects/yearcoding/27092002_selenium3.py", line 15, in <module>
browser = webdriver.Firefox(service= FirefoxService(GeckoDriverManager().install()), options=options)
File "/home/halovivek/PycharmProjects/yearcoding/venv/lib/python3.10/site-packages/selenium/webdriver/firefox/webdriver.py", line 177, in __init__
super().__init__(
File "/home/halovivek/PycharmProjects/yearcoding/venv/lib/python3.10/site-packages/selenium/webdriver/remote/webdriver.py", line 270, in __init__
self.start_session(capabilities, browser_profile)
File "/home/halovivek/PycharmProjects/yearcoding/venv/lib/python3.10/site-packages/selenium/webdriver/remote/webdriver.py", line 363, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "/home/halovivek/PycharmProjects/yearcoding/venv/lib/python3.10/site-packages/selenium/webdriver/remote/webdriver.py", line 428, in execute
self.error_handler.check_response(response)
File "/home/halovivek/PycharmProjects/yearcoding/venv/lib/python3.10/site-packages/selenium/webdriver/remote/errorhandler.py", line 243, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.InvalidArgumentException: Message: binary is not a Firefox executable
Process finished with exit code 1
1条答案
按热度按时间enyaitl31#
类似于Selenium gives "unknown error: cannot find Chrome binary" when running chrome driver on Ubuntu,它意味着Chrome没有安装在您的系统上,或者它没有安装在PATH中的默认位置。如果Chrome已经安装,但是在一个特殊的位置,您可以这样设置它(在您输入一个有效的路径之后):
如果您的Linux机器上根本没有安装Chrome,您可以尝试现有的解决方案,如https://stackoverflow.com/a/62234251/7058266或https://www.digitalocean.com/community/tutorials/install-chrome-on-linux-mint,以便将其安装到PATH中的默认位置。
下面是Linux上Firefox无头模式的一个示例: