rishabh
rishabh

Reputation: 133

Unable to select radio button using selenium java

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.

Upvotes: 2

Views: 559

Answers (3)

Harish Kannan
Harish Kannan

Reputation: 515

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

Upvotes: 0

undetected Selenium
undetected Selenium

Reputation: 193078

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();
    

Upvotes: 0

Ehler
Ehler

Reputation: 325

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.

Upvotes: 1

Related Questions