samedi 29 août 2020

Python Selenium Geckodriver should be in PATH

I'm trying to fill a form automatically using Selenium. Here is the HTML code:

<!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. 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?




Aucun commentaire:

Enregistrer un commentaire