Home > Mobile >  Unable to fill the form using Selenium: AttributeError: 'WebDriver' object has no attribut
Unable to fill the form using Selenium: AttributeError: 'WebDriver' object has no attribut

Time:11-02

I am trying to fill the Uber form (enter image description here

I tried to use XPath to fill the form, here is the code:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_argument("user-data-dir=C:\\Users\\Zedas\\AppData\\Local\\Google\\Chrome\\User Data")
w = webdriver.Chrome(executable_path='chromedriver.exe', chrome_options=options)
w.get("https://m.uber.com/looking")
time.sleep(6)
w.get_element_by_xpath(f'"//*[@id="booking-experience-container"]/div/div[3]/div[2]/div/input"').send_keys("test")

But, this is the error:

w.get_element_by_xpath(f'"//*[@id="booking-experience-container"]/div/div[3]/div[2]/div/input"').send_keys("test")
AttributeError: 'WebDriver' object has no attribute 'get_element_by_xpath'

How can this error be fixed?

CodePudding user response:

There's no method known as get_element_by_xpath in Selenium-Python bindings.

Please use

driver.find_element_by_xpath("xpath here")

Also, Since find_element internally looks for implicit waits to wait and interact with web element. Often, It has been observed that it is not a consistent method to look for web element/elements in Selenium automation.

Please use Explicit waits :

WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "xpath here"))).send_keys("test")

You will need these imports as well.

Imports :

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

You can read more about waits from here

CodePudding user response:

w.get_element_by_xpath should be w.find_element_by_xpath

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_argument("user-data-dir=C:\\Users\\Zedas\\AppData\\Local\\Google\\Chrome\\User Data")
w = webdriver.Chrome(executable_path='chromedriver.exe', chrome_options=options)
w.get("https://m.uber.com/looking")
time.sleep(6)
w.find_element_by_xpath(f'"//*[@id="booking-experience-container"]/div/div[3]/div[2]/div/input"').send_keys("test")

CodePudding user response:

This error message...

w.get_element_by_xpath(f'"//*[@id="booking-experience-container"]/div/div[3]/div[2]/div/input"').send_keys("test")
AttributeError: 'WebDriver' object has no attribute 'get_element_by_xpath'

implies that the WebDriver object has no attribute as get_element_by_xpath().


You have to take care of a couple of things as follows:

  • If you look at the various WebDriver strategies to locate elements there is no method as get_element_by_xpath() and possibly you would like to replace with find_element_by_xpath()
  • While invoking click() on an element, Implicit Wait is no more effective and you need to replace it with Explicit Wait i.e WebDriverWait

Solution

So your effective code block will be:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

options = Options()
options.add_argument("user-data-dir=C:\\Users\\Zedas\\AppData\\Local\\Google\\Chrome\\User Data")
w = webdriver.Chrome(executable_path='chromedriver.exe', chrome_options=options)
w.get("https://m.uber.com/looking")
WebDriverWait(w, 20).until(EC.element_to_be_clickable((By.XPATH, f'"//*[@id="booking-experience-container"]/div/div[3]/div[2]/div/input"'))).send_keys("test")
  • Related