'Syntax for using XPATH and Sendkeys in case of DeprecatedWarnings using Latest Selenium version

I am trying to find email and password fields by XPATH and trying to send keys as well but I am getting the error, need syntax of finding element by XPATH while using latest selenium version and getting DeprecationWarning:

enter image description here

Image link



Solution 1:[1]

You are using findElement method in Python-Selenium bindings. Note that findElement is available in Java-Selenium bindings.

for Python you should use:

driver.find_element(By.XPATH, "XPath here").send_keys('keys to be sent here')

this should help you resolve syntax error.

Your final code block should look like this:

driver.maximize_window()
driver.get("https://www.facebook.com")

driver.find_element(By.XPATH, "//input[@id='email']").send_keys('[email protected]')
driver.find_element(By.XPATH, "//input[@id='pass']").send_keys('password here')
driver.find_element(By.XPATH, "//button[@name='login']").click()

You can replace these find_element with the explicit wait as well like below:

wait = WebDriverWait(driver, 30)
wait.until(EC.visibility_of_element_located((By.XPATH, "//input[@id='email']"))).send_keys('[email protected]') 

for this you will have to import:

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1