7

I'm trying to select a checkbox / radio button by all the possible ways that I can think of, but none of them have worked.

This is the way I am trying to identity de checkbox

WebElement selectGender = driver.findElement(By.id("id_gender1"));
selectGender.click();

Here is the HTML code

<div class="radio-inline">
    <label for="id_gender1" class="top">
     <div class="radio" id="uniform-id_gender1"><span class="checked"><input type="radio" name="id_gender" id="id_gender1" value="1">
     </span>
     </div>
       Mr.
   </label>
</div>

This is the error message I am receiving.

org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"css selector","selector":"#id_gender1"}
  (Session info: chrome=75.0.3770.142)
For documentation on this error, please visit: https://www.seleniumhq.org/exceptions/no_such_element.html
Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:17:03'
System info: host: 'JEFFERSONPC', ip: '192.168.100.6', os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.8.0_211'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 75.0.3770.142, chrome: {chromedriverVersion: 75.0.3770.140 (2d9f97485c7b..., userDataDir: C:\Users\JEFFER~1\AppData\L...}, goog:chromeOptions: {debuggerAddress: localhost:63666}, javascriptEnabled: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: XP, platformName: XP, proxy: Proxy(), setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify}
Session ID: 65163859269265511fa3118a5c9b1f79
*** Element info: {Using=id, value=id_gender1}
Krishnan Mahadevan
  • 14,121
  • 6
  • 34
  • 66
Jefferson Silva
  • 71
  • 1
  • 1
  • 2
  • were you able to locate the element using the same css in the chrome browser dev tools? – supputuri Jul 24 '19 at 15:48
  • Refer to [this](https://stackoverflow.com/questions/55870609/is-there-a-way-to-learn-xpath-without-using-firebug-or-xpath-as-firefox-is-not-s/55870909#55870909) if you need help with how to work with xpath in the browser devtool. – supputuri Jul 24 '19 at 15:50

5 Answers5

5

Induce WebDriverWait and elementToBeClickable to click on the element.

Try the following options.

WebDriverWait wait = new WebDriverWait(driver, 30);
WebElement selectGender = wait.until(ExpectedConditions.elementToBeClickable(By.id("id_gender1")));
selectGender.click()

OR

WebDriverWait wait = new WebDriverWait(driver, 30);
WebElement selectGender = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@class='radio-inline']/label[@for='id_gender1']")));
selectGender.click()
KunduK
  • 32,888
  • 5
  • 17
  • 41
0

Here is your code

  findElement(By.xpath("//input[@name='id_gender']")).click();
IPolnik
  • 619
  • 5
  • 13
0

--Disclaimer: cant comment yet due to lack of reputation--

the code where you fetch the id seems fine, and no spelling errors in the code either. But it could be that your answer is found here:

Unable to locate an Element By ID in Selenium

Also: please post what you have tried so far in a bit more detail. You say in all ways you know, but we dont know what that is :).

For example, reference the stackoverflow answers you have tried that are similar to yours

0

Your code works fine. Why your error message have css Selector? when you are not using cssSelector.

if using cssSelector then: WebElement selectGender = driver.findElement(By.cssSelector("#id_gender1")); selectGender.click();

sanj
  • 16
  • 3
0

I am sure you are trying to click element which is inside the frame. First of all you have to switch driver on that frame and perform your test script.

driver123.switchTo().frame(<some frame index or id>;//Switching frame
WebElement selectGender = driver.findElement(By.id("id_gender1"));//Storing element
selectGender.click();//Clicking on element

Note: If you do not have frame id or index use xpath for switch to that frame.

sumit
  • 1
  • 3