2

I want to select a radio button using selenium web driver. My code is

<div class="radio-switch-group" aria-label="RequestForCheckBox" role="menu">
<label>
    <input type="radio" name="request_for" value="self" ng-model="data.form.requestFor" class="ng-pristine ng-untouched ng-valid">
    <span class="radio-label" translate=""><span class="ng-scope">Self</span></span>
</label>
<label>
    <input type="radio" name="request_for" value="other" data-ng-click="$scope.getDataHandler()" ng-model="data.form.requestFor" class="ng-pristine ng-untouched ng-valid">
    <span class="radio-label" translate=""><span class="ng-scope">Others</span></span>
</label>
</div>

I tried to locate the element with code shown below

WebElement other = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id=\"make-request-form\"]/div/section/div[2]/div/form/div[1]/div/div/div/label[2]/input")));
other.click();

But getting below

 Exception in thread "main" org.openqa.selenium.TimeoutException: Expected condition failed: waiting for visibility of element located by By.xpath: //*[@id="make-request-form"]/div/section/div[2]/div/form/div[1]/div/div/div/label[2]/input (tried for 20 second(s) with 500 milliseconds interval)
Build info: version: '3.11.0', revision: 'e59cfb3', time: '2018-03-11T20:33:08.638Z'
System info: host: 'AUUR01VP1341', ip: '10.97.2.56', os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.8.0_171'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities {acceptSslCerts: true, applicationCacheEnabled: false, browserConnectionEnabled: false, browserName: chrome, chrome: {chromedriverVersion: 2.34.522940 (1a76f96f66e3ca..., userDataDir: C:\Users\L100455\AppData\Lo...}, cssSelectorsEnabled: true, databaseEnabled: false, handlesAlerts: true, hasTouchScreen: false, javascriptEnabled: true, locationContextEnabled: true, mobileEmulationEnabled: false, nativeEvents: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: XP, platformName: XP, rotatable: false, setWindowRect: true, takesHeapSnapshot: true, takesScreenshot: true, unexpectedAlertBehaviour: , unhandledPromptBehavior: , version: 61.0.3163.100, webStorageEnabled: true}
Session ID: 9d15042c1c2c4eda197796e5fdf42243
    at org.openqa.selenium.support.ui.WebDriverWait.timeoutException(WebDriverWait.java:81)
    at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:271)
    at auto.Automation.main(Automation.java:44)

Can someone please help me with this.

2
  • Which radio button are you trying to locate and click Self or Others? Commented May 9, 2018 at 12:55
  • I am trying to locate Others Commented May 9, 2018 at 14:15

3 Answers 3

1

According to the thrown exception, you are running in the wait timeout.

Either your object is not existing after the default timeout of 20 seconds or the xPath you are using is wrong. You can try to use the following xPath to select the second label: (//input[@name="request_for"])[2]

Tipp: for a quick test, past your HTML code into a xPath generator (e.g.: https://www.freeformatter.com/xpath-tester.html) and test the xPath expression without the need to rerun Selenium.

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

1 Comment

I tried your suggestion but still I am not able to locate the element
0

As per the HTML you have shared and your code trials moving ahead as you will be clicking one of the Radio Buttons instead of ExpectedConditions visibilityOfElementLocated() you need to use elementToBeClickable() as follows :

  • To click on the Radio Button associated with the text Self you can use the following line of code :

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@class='ng-pristine ng-untouched ng-valid' and @value='self']"))).click();
    
  • To click on the Radio Button associated with the text Others you can use the following line of code :

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@class='ng-pristine ng-untouched ng-valid' and @value='other']"))).click();
    

2 Comments

I tried your suggestion but still I am not able to locate the element.
@rishabh not able to locate doesn't throws any light whats wrong happening. Update the question with your current code trials and error stack trace for further analysis.
0

Try using ExpectedConditions.elementToBeClickable(element) instead of visibilityOfElementLocated()

Comments

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.