thangaraj1980
thangaraj1980

Reputation: 155

Unable to locate element error while using find_element_by_css_selector in python

I am learning Selenium using Python. I have tried to check the button status - whether enabled or not? For that I have used mercury demo site and wrote the below code. I am getting

Unable to locate element: input[value=roundtrip]

Code which I have used:

from  selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
driver = webdriver.Firefox(executable_path="C:\\Seleinum_Python\\WedDriver\\geckodriver.exe")
driver.get("http://newtours.demoaut.com/")
# Whether the user name & password enabled and displayed
user_ele = driver.find_element_by_name("userName")   # <input type="text" name="userName" size="10">
print(user_ele.is_displayed()) # Return True /False based on element status
print(user_ele.is_enabled())
pwd_ele = driver.find_element_by_name("password")   #<input type="password" name="password" size="10">
print(pwd_ele.is_displayed())
print(pwd_ele.is_enabled())
user_ele.send_keys("mercury")
pwd_ele.send_keys("mercury")
driver.find_element_by_name("login").click()
roundtrip_radio= driver.find_element_by_css_selector("input[value=roundtrip]") #<input type="radio" name="tripType" value="roundtrip" checked="">
print(roundtrip_radio.is_selected())
oneway_radio= driver.find_element_by_css_selector("input[value=oneway]")
print(oneway_radio.is_selected())
driver.close()

I even tried with below combination but still I am getting the same element not found issue.

 roundtrip_radio= driver.find_element_by_css_selector("input[value='roundtrip']")
 roundtrip_radio= driver.find_element_by_css_selector('input[value="roundtrip"]')
 roundtrip_radio= driver.find_element_by_css_selector("//input[value='roundtrip']")
 roundtrip_radio= driver.find_element_by_css_selector("input[value='roundtrip']")
 roundtrip_radio= driver.find_element_by_css_selector('input[name="tripType"][value="roundtrip"]')

Upvotes: 0

Views: 126

Answers (1)

sourabhR4ikwar
sourabhR4ikwar

Reputation: 82

In your css selector you can use :checked to identify selected elements.

for example :-

"input[type=radio]:checked"

Upvotes: 1

Related Questions