[英]Python Selenium Geckodriver should be in PATH
I'm trying to fill a form automatically using Selenium.我正在尝试使用 Selenium 自动填写表单。 Here is the HTML code:
这是 HTML 代码:
<!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>
Here is the Python/Selenium code: It says the Geckodriver needs to be in PATH.这是 Python/Selenium 代码:它说 Geckodriver 需要在 PATH 中。 Previously when I got this error, I specified the executablepath but that doesn't seem to be working anymore.
以前,当我收到此错误时,我指定了可执行路径,但这似乎不再起作用。 What am I doing wrong?
我究竟做错了什么?
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")
This is the error I am getting:这是我得到的错误:
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.
What am I doing wrong?我究竟做错了什么?
Try replacing the \\
with /
in the executable_path
.尝试在
executable_path
中用/
替换\\
。
Replace this:替换这个:
driver = webdriver.Firefox(executable_path="C:\Users\Anna\Downloads\geckodriver")
with:和:
driver = webdriver.Firefox(executable_path="C:/Users/Anna/Downloads/geckodriver")
Or you can move the geckodriver to the same directory where you have saved your source code.或者,您可以将 geckodriver 移动到保存源代码的同一目录中。 Then you will not need to provide the executable path.
那么您将不需要提供可执行路径。
driver = webdriver.Firefox() #will also work fine
There are at list two issues with your current code;您当前的代码有两个问题; the easier one - the way you're passing the path to the driver.
更简单的 - 您将路径传递给驱动程序的方式。
In Python strings the character \\
is called an escape character, and used to specify special chars (like \\n
for a new line).在 Python 字符串中,字符
\\
称为转义字符,用于指定特殊字符(如\\n
换行)。
So if you need to actually use it - like in paths - you need to escape itself;因此,如果您需要实际使用它 - 就像在路径中一样 - 您需要逃避自己; also, the executables in Windows are usually with the extension ".exe", which you have omitted.
此外,Windows 中的可执行文件通常带有您已省略的扩展名“.exe”。 Thus a proper argument in that call would be:
因此,该调用中的正确参数是:
driver = webdriver.Firefox(executable_path="C:\\Users\\Anna\\Downloads\\geckodriver.exe")
You can also use Python's raw strings , not to escape the \\
character:您还可以使用 Python 的原始字符串,而不是转义
\\
字符:
driver = webdriver.Firefox(executable_path=r"C:\Users\Anna\Downloads\geckodriver.exe")
driver.get("example form.html") driver.get("示例表单.html")
Now with the bigger problem, which resolution is totally within your hands - by the exception stack trace it's obvious you are running the script in some Linux OS environment.现在有了更大的问题,哪个解决方案完全掌握在您的手中 - 通过异常堆栈跟踪,很明显您正在某些 Linux 操作系统环境中运行脚本。 In the same time, the geckodriver you are trying to pass is in a Windows drive, and such executable.
同时,您尝试传递的 geckodriver 位于 Windows 驱动器中,并且是此类可执行文件。
You have to change one of the two - either run it in Windows, OR pass a path to a Linux geckodriver executable.您必须更改两者之一 - 要么在 Windows 中运行它,要么将路径传递给 Linux geckodriver 可执行文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.