0

Below is my HTML

<div id="slectrole" class="collapse in" role="tabpanel" aria-labelledby="selectrole">
<div class="panel-body">
<div class="dropdown">
<input class="search-control jsSayt jsRolesFreeText" onfocus="this.placeholder = ''" onblur="this.placeholder = 'Eg: Delivery, BPO, Driver'" placeholder="Eg: Delivery, BPO, Driver" value="" aria-expanded="false" aria-haspopup="true" data-toggle="dropdown" type="text">
<ul class="jsSaytList jsRolesFilter">
<li id="jsFilter_subRole_1" class="checkbox-inline jsFilterSubRole jsRoleValue_1" data-value="Accountant">
<input id="Accountant" class="radio-custom jsFilterRadio jsRole" value="Accountant" name="Role" data-roleid="1" type="radio">
<label class="radio-custom-label" for="Accountant">Accountant</label>

Below is the code I am using to click the radio button:

wait.until(EC.visibility_of_element_located((By.XPATH, "//div[@id='slectrole']/descendant::li[@data-value='Accountant']/label[@for='Accountant']")))


driver.find_element_by_xpath("//div[@id='slectrole']/descendant::li[@data-value='Accountant']/label[@for='Accountant']").click()

The code runs ok but it does not select the radio button.

6
  • You are clicking the <label>, but your radio-button is at <input>, so just go with: //input[@id='Accountant']. Your <label> just has the text. The functionality lays in the <input> field. Commented May 24, 2017 at 5:43
  • //div[@class='stickySearch']/div[@id='jsSideFilters']/ul[@id='accordion']/li[@id='jsFilter_roles']/div[@class='collapse in']/div[@class='panel-body']/div[@class='dropdown']/ul[@class='jsSaytList jsRolesFilter']/li[1]/input[@value='Accountant'] Commented May 24, 2017 at 6:01
  • //div[@class='stickySearch']/div[@id='jsSideFilters']/ul[@id='accordion']/li[@id='jsFilter_roles']/div[@class='collapse in']/div[@class='panel-body']/div[@class='dropdown']/ul[@class='jsSaytList jsRolesFilter']/li[1]/label[@for='Accountant'] Commented May 24, 2017 at 6:01
  • //div[@class='stickySearch']/div[@id='jsSideFilters']/ul[@id='accordion']/li[@id='jsFilter_roles']/div[@class='collapse in']/div[@class='panel-body']/div[@class='dropdown']/ul[@class='jsSaytList jsRolesFilter']/li[1] Commented May 24, 2017 at 6:01
  • I tried all the above xpath but none of them is working and it does not click the radio button @Mahipal Commented May 24, 2017 at 6:01

2 Answers 2

1

OK, so I can understand your frustration, I tried your code and wasn't able to .click() (select) the element when located via xpath. See bellow print-screen: enter image description here As you can see, it was only clicking the radio-button when issuing a .click() via a CSS-located element.

Question No.1: Are you bound to the xpath locator strategy in one way or another?

If NOT, then just use a regulat CSS selector: 'input[id="Accountant"]'. Else, you have to figure out what is wrong with the website you are testing, or switch to another WebElement locator strategy. (e.g.: ID, Class, CSS, LinkText, etc.)

If you would opt to go with the CSS locator-strategy, then your code would look like this:

wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "label[for='Accountant']")))
driver.find_element_by_css("input[id='Accountant']").click()

Alternatively, you can try to click on the <label> tag attached to the radio-button, which in my console works the same way:

wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "label[for='Accountant']")))
driver.find_element_by_css("label[for='Accountant']").click()

Explanation: In a real-life scenario, you can select the radio-button both via the actual radio-button, or via its label. That's why your solution worked.

Question No.2: Why are you using such a long xpath selector?

In order to have a optimal selector, you should ALWAYS go with the shortest, combination of tags/attributes that will UNIQUELY identify your target element. Else you will be susceptible to website changes, flaky test cases, etc.

Sign up to request clarification or add additional context in comments.

7 Comments

AttributeError: type object 'By' has no attribute 'CSS'
I am using python and selenium webdriver: wait.until(EC.visibility_of_element_located((By.CSS, "label[for='Accountant']"))) driver.find_element_by_css_selector("input[id='Accountant']").click()
I am new to selenium and having a tough time using xpath.i tried long xpath just to make sure I am able to select the correct element.I dont need to use xpath.I can use css or anything else that works
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "label[for='Accountant']"))) driver.find_element_by_css_selector("input[id='Accountant']").click() I ran the above code.it ran ok but dint click the radio button
@user8040338 can you retry the snippet of code? I had the wrong argument for the By class. It was supposed to be By.CSS_SELECTOR. (I'm coming from a Selenium/JavaScript background so Python is not my forte). Btw, you need to pro-actively go online and search for as many solutions as possible, but in order not to do it blindly, I would also recommend reading up the DOC files: selenium-python.readthedocs.io/locating-elements.html.
|
0

You can perform the click on the drop down and then wait for the radio button to appear, before clicking it. Hence, try following:

driver.find_element_by_xpath("//div[@id='slectrole']/div/div[@class='dropdown']/input[1]")).click()
wait = WebDriverWait(driver, 10)
wait.until(EC.visibility_of_element_located((By.XPATH, '//div[@id='slectrole']/descendant::li[@data-value='Accountant']/input[1]')))
driver.find_element_by_xpath("//div[@id='slectrole']/descendant::li[@data-value='Accountant']/input[1]").click()

Let me know, if above code works for you.

8 Comments

Let me know, if you have any queries.
I tried this code but it doesnt work.I dont get any error but it still does not click the radio button @Mahipal
Did it click on the drop-down and options appeared?
I finally got it to work using below code: roleSelect = WebDriverWait(driver, 20).until(lambda driver: driver.find_element_by_xpath(".//*[@id='slectrole']/div/div/input")) roleSelect.send_keys("Accountant") wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "label[for='Accountant']"))) driver.find_element_by_css_selector("input[id='Accountant']").click() Can someone please help me understand why the earlier code was not working?
There is no drop down.There is a input box and radio buttons below it
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.