driver.find_element(By.XPATH, "xpath") not working

Question:

I am trying to learn Selenium in Python and I have faced a problem which stopped me from processing.

As you might know, previous versions of Selenium have different syntax comparing the latest one, and I have tried everything to fill the form with my code but nothing happens. I am trying to find XPATH element from [https://demo.seleniumeasy.com/basic-first-form-demo.html] but whatever I do, I cannot type my message into the message field.

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service as ChromeService
import time

options = webdriver.ChromeOptions()
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option("useAutomationExtension", False)
service = ChromeService(executable_path="C:/Users/SnappFood/.cache/selenium/chromedriver/win32/110.0.5481.77/chromedriver.exe")


driver = webdriver.Chrome()
driver.maximize_window()
driver.get("https://demo.seleniumeasy.com/basic-first-form-demo.html")
time.sleep(1000)
message_field = driver.find_element(By.XPATH,'//*[@id="user-message"]')
message_field.send_keys("Hello World")
show_message_button = driver.find_element(By.XPATH,'//*[@id="get-input"]/button')
show_message_button.click()

By this code, I expect to fill the message field in the form and click the "SHOW MESSAGE" button to print my typed text, but what happens is that my code only opens a new Chrome webpage with empty field.
I have to mention that I don’t get any errors by PyCharm and the code runs with no errors.

I would really appreciate if you help me through this to understand what I am doing wrong.

Asked By: Pedram Najafizadeh

||

Answers:

with this xpath you have two elements //*[@id="user-message"] . you need to make it unique. Try below xpath this will work as expected.

message_field = driver.find_element(By.XPATH,'//input[@id="user-message"]')

browser snapshot:

enter image description here

Answered By: KunduK

You need to wait for the page loads correctly.

For this, the best approach is using WebDriverWait:

driver = webdriver.Chrome()
driver.maximize_window()
driver.get("https://demo.seleniumeasy.com/basic-first-form-demo.html")

# Wait for the page to load
wait = WebDriverWait(driver, 10)
wait.until(lambda driver: driver.find_element(By.XPATH, '//*[@id="user-message"]'))

message_field = driver.find_element(By.XPATH,'//*[@id="user-message"]')
message_field.send_keys("Hello World")

show_message_button = driver.find_element(By.XPATH,'//*[@id="get-input"]/button')
show_message_button.click()

Tested, it works fine.

Also you can use time.sleep() to wait the load if you know the loading times:

import time

driver = webdriver.Chrome()
driver.maximize_window()
driver.get("https://demo.seleniumeasy.com/basic-first-form-demo.html")
time.sleep(5)

message_field = driver.find_element(By.XPATH,'//*[@id="user-message"]')
message_field.send_keys("Hello World")
time.sleep(5)

show_message_button = driver.find_element(By.XPATH,'//*[@id="get-input"]/button')
show_message_button.click()
time.sleep(5)
Answered By: Juan Melnechuk