Python Selenium Geckodriver应该在路径中
我正在尝试使用Selenium自动填写表单。以下是HTML代码:Python Selenium Geckodriver应该在路径中,python,html,selenium,web,automation,Python,Html,Selenium,Web,Automation,我正在尝试使用Selenium自动填写表单。以下是HTML代码: <!DOCTYPE html> <html> <body> <h2>Text input fields</h2> <form> <label for="fname">First name:</label><br> <input type="text" id="
<!DOCTYPE html>
<html>
<body>
<h2>Text input fields</h2>
<form>
<label for="fname">First name:</label><br>
<input type="text" id="fname" name="fname" value="John"><br>
<label for="lname">Last name:</label><br>
<input type="text" id="lname" name="lname" value="Doe"><br>
<label for="cars">Choose a car:</label>
<select id="cars" name="cars">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="fiat">Fiat</option>
<option value="audi">Audi</option>
</select><br>
<textarea name="message" rows="10" cols="30">The cat was playing in the garden.</textarea>
</form>
这就是我得到的错误:
Traceback (most recent call last):
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/selenium/webdriver/common/service.py", line 72, in start
self.process = subprocess.Popen(cmd, env=self.env,
File "/usr/lib/python3.8/subprocess.py", line 854, in __init__
self._execute_child(args, executable, preexec_fn, close_fds,
File "/usr/lib/python3.8/subprocess.py", line 1702, in _execute_child
raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: 'geckodriver.exe'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "main.py", line 7, in <module>
driver = webdriver.Firefox(executable_path="geckodriver.exe")
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/selenium/webdriver/firefox/webdriver.py", line 164, in __init__
self.service.start()
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/selenium/webdriver/common/service.py", line 81, in start
raise WebDriverException(
selenium.common.exceptions.WebDriverException: Message: 'geckodriver.exe' executable needs to be in PATH.
回溯(最近一次呼叫最后一次):
文件“/opt/virtualenvs/python3/lib/python3.8/site-packages/selenium/webdriver/common/service.py”,第72行,开头
self.process=subprocess.Popen(cmd,env=self.env,
文件“/usr/lib/python3.8/subprocess.py”,第854行,在__
self.\u execute\u child(参数、可执行文件、预执行文件、关闭文件、,
文件“/usr/lib/python3.8/subprocess.py”,第1702行,在执行子进程中
引发子项异常类型(错误号、错误消息、错误文件名)
FileNotFoundError:[Errno 2]没有这样的文件或目录:“geckodriver.exe”
在处理上述异常期间,发生了另一个异常:
回溯(最近一次呼叫最后一次):
文件“main.py”,第7行,在
driver=webdriver.Firefox(可执行文件_path=“geckodriver.exe”)
文件“/opt/virtualenvs/python3/lib/python3.8/site packages/selenium/webdriver/firefox/webdriver.py”,第164行,在__
self.service.start()
文件“/opt/virtualenvs/python3/lib/python3.8/site packages/selenium/webdriver/common/service.py”,第81行,开头
引发WebDriverException(
selenium.common.exceptions.WebDriverException:消息:“geckodriver.exe”可执行文件需要位于路径中。
我做错了什么?尝试用
可执行路径中的/
替换\
import selenium
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Firefox(executable_path="C:\Users\Anna\Downloads\geckodriver")
driver.get("example form.html")
#First name
element = driver.find_element_by_name("fname")
element.clear()
element.send_keys("Anna")
#Last name
element = driver.find_element_by_name("lname")
element.clear()
element.send_keys("Zharavina")
#Dropdown list | Car selection
select = Select(driver.find_element_by_name("cars"))
select.deselect_all()
select.select_by_value(volvo)
#Textarea
element = driver.find_element_by_name("message")
element.clear()
element.send_keys("Test message 10013341381")
替换此项:
driver = webdriver.Firefox(executable_path="C:\Users\Anna\Downloads\geckodriver")
与:
或者,您可以将geckodriver移动到保存源代码的同一目录,这样就不需要提供可执行路径
driver = webdriver.Firefox() #will also work fine
您当前的代码至少有两个问题;最简单的一个问题是您将路径传递给驱动程序的方式。
在Python字符串中,字符\
称为转义字符,用于指定特殊字符(如新行的\n
)
因此,如果您需要实际使用它,就像在路径中一样,您需要转义自身;而且,Windows中的可执行文件通常带有扩展名“.exe”,您省略了该扩展名。因此,该调用中的适当参数应该是:
driver = webdriver.Firefox(executable_path="C:\\Users\\Anna\\Downloads\\geckodriver.exe")
您还可以使用Python,而不是转义\
字符:
driver = webdriver.Firefox(executable_path=r"C:\Users\Anna\Downloads\geckodriver.exe")
get(“example form.html”)
现在有一个更大的问题,解决方案完全在您的手中-通过异常堆栈跟踪,很明显您正在某些Linux操作系统环境中运行脚本。同时,您试图传递的geckodriver在Windows驱动器中,并且是这样的可执行文件。
您必须更改这两个选项中的一个,或者在Windows中运行它,或者传递到Linux geckodriver可执行文件的路径。您好,谢谢您的快速回答。我尝试了这两种方法,但仍然出现相同的错误。
driver = webdriver.Firefox(executable_path=r"C:\Users\Anna\Downloads\geckodriver.exe")