使用Selenium编写脚本来打开URL:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.firefox.service import Service as FirefoxService
from selenium.webdriver.firefox.options import Options as FirefoxOptions
import json
# create a Firefox service and options object
firefox_service = FirefoxService(executable_path='/usr/local/bin/geckodriver')
firefox_options = FirefoxOptions()
# set the binary location for Firefox
firefox_options.binary_location = '/usr/bin/firefox'
# enable network logging
firefox_options.set_preference("devtools.netmonitor.har.enableAutoExportToFile", True)
firefox_options.set_preference("devtools.netmonitor.har.autoConnect", True)
firefox_options.set_preference("devtools.netmonitor.har.forceExport", True)
firefox_options.set_preference("devtools.netmonitor.har.fileName", "network_logs")
# create a Firefox driver instance with the specified service and options
driver = webdriver.Firefox(service=firefox_service, options=firefox_options)
# navigate to the lambdatest website
driver.get("https://www.lambdatest.com/")
# find the header navigation elements and click on each one
header_nav_elements = driver.find_elements_by_css_selector('.navbar-nav .nav-link')
for element in header_nav_elements:
element.click()
# wait for 5 seconds
driver.implicitly_wait(5)
# close the browser window and quit the driver
driver.close()
driver.quit()
# read the network logs from the HAR file
with open('network_logs.har', 'r') as f:
network_logs = json.load(f)
# print the network logs
print(json.dumps(network_logs, indent=2))
但当我运行它时,我得到一个错误:
Traceback (most recent call last):
File "/mnt/c/Users/HP/Desktop/Newstuff/Lambdatest/main.py", line 21, in <module>
driver = webdriver.Firefox(service=firefox_service, options=firefox_options)
File "/home/riley/.local/lib/python3.10/site-packages/selenium/webdriver/firefox/webdriver.py", line 199, in __init__
super().__init__(command_executor=executor, options=options, keep_alive=True)
File "/home/riley/.local/lib/python3.10/site-packages/selenium/webdriver/remote/webdriver.py", line 286, in __init__
self.start_session(capabilities, browser_profile)
File "/home/riley/.local/lib/python3.10/site-packages/selenium/webdriver/remote/webdriver.py", line 378, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "/home/riley/.local/lib/python3.10/site-packages/selenium/webdriver/remote/webdriver.py", line 440, in execute
self.error_handler.check_response(response)
File "/home/riley/.local/lib/python3.10/site-packages/selenium/webdriver/remote/errorhandler.py", line 245, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.InvalidArgumentException: Message: binary is not a Firefox executable
我尝试使用/usr/bin/firefox
来测试是否可以使用该命令打开Firefox,但我得到了
Command '/usr/bin/firefox' requires the firefox snap to be installed.
Please install it with:
snap install firefox
当我尝试snap install firefox
时,我得到
snap install firefox
error: cannot communicate with server: Post "http://localhost/v2/snaps/firefox": dial unix /run/snapd.socket: connect: no such file or directory
1条答案
按热度按时间mwkjh3gx1#
apt-get更新
apt-get升级
sudo apt-get install snapd
快照版本
sudo apt-get install -yqq daemonize dbus-user-session fontconfig
sudo daemonize /usr/bin/unshare --fork --pid --mount-proc /lib/systemd/systemd --system-unit= basic.target
exec sudo nsenter -t $(pidof systemd)-a su - $LOGNAME
sudo apt install firefox firefox-locale-fr(for frenchies)
安装Firefox