I am unable to write the correct XPath to select the title "MRS" for below code. I am using below code, but unable to execute it.

driver.findElement(By.xpath("//div[@value='MRS']")).click();

Getting below error message :

Exception in thread "main" org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//div[@value='MRS']"}

<div class="question-group">

    <label class="on">Mr</label>
    <input type="radio" name="title" value="MR" class="ui-helper-hidden-accessible">

    <label class="off">Mrs</label>
    <input type="radio" name="title" value="MRS" class="ui-helper-hidden-accessible">

    <label class="off">Miss</label>
    <input type="radio" name="title" value="MISS" class="ui-helper-hidden-accessible">

    <label class="off">Ms</label>
    <input type="radio" name="title" value="MS" class="ui-helper-hidden-accessible">

    <label class="off">Dr</label>
    <input type="radio" name="title" value="DR" class="ui-helper-hidden-accessible">

    <label class="off">Rev</label>
    <input type="radio" name="title" value="REV" class="ui-helper-hidden-accessible">
    <input type="text" maxlength="20" style="display:none" name="otherTitle" value="" placeholder="Other title:" id="otherTitle">

</div>

enter image description here

  • Syntax in what? Syntax in Xpath? Also, where is the ending tag for your leading div? That will make it hard for anyone to help you as well. – George Pantazes Nov 14 at 16:23
  • yes in xpath and i have pasted complete xml. there is nothing missing – Arjun Nov 14 at 16:45
  • i have added screenshot as well – Arjun Nov 14 at 16:51
  • Please edit the question and add the code you have tried and the results you got. – Kate Paulk Nov 14 at 19:54
  • Hi Kate,I have added the code and error message in above question. – Arjun Nov 15 at 10:22

I'm guessing a little bit from your code and the screen shot you provided: the radio buttons are hidden and the user is actually clicking the formatted labels instead, right?

In this case your problem is that Selenium / WebDriver interacts with visible objects only. So you simply cannot click any radio button - because there is none. You have to click the labels.

Try .//div[@class='question-group']/label[text()='Mrs'] as xpath expression.

  • used below code and getting error message driver.findElement(By.xpath("//div[@class='question-group']/label[text()='Mrs']")).click(); – Arjun Nov 21 at 16:50
  • I created a little test with the code fragement you provided above and the xpath expression is working and selects the appropriate label. Thus I think something different beside the xpath must create your problem. Please use driver.getPageSource() to log what the driver "is seeing" when the exception is thrown. – Jan_Zdunek_DE Nov 21 at 20:17

Problem is any <div> tag in your page do no have attribute value="MRS" that's why it throwing error NoSuchElementException

try with below xpath:

//div[@class='question-group']/input[@value='MRS']

or CSS

div[class='question-group']>input[value='MRS']
  • tried below code and still showing error driver.findElement(By.xpath("//div[@class='question-group']/input[@value='MRS']")).click(); – Arjun Nov 16 at 11:24
  • error message Exception in thread "main" org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//div[@class='question-group']/input[@value='MRS']"} – Arjun Nov 16 at 11:27
  • try //div[@class='question-container']//div[class='question-group']/label[text()='Mrs'] or just debug by placing Thread.sleep(3000) before you click – NarendraR Nov 16 at 11:33
  • tried below code and still not worksThread.sleep(5000); driver.findElement(By.xpath("//div[@class='question-container']//div[class='question-group']/label[text()='Mrs']")).click(); – Arjun Nov 16 at 11:37
  • is there any iframe ? – NarendraR Nov 16 at 11:38

There are a few options to try here. If you are trying to target the input element, then

XPATH:

.//*[@value='MRS']

or CSS:

[value='MRS']

should work.

  • Tried with xpath driver.findElement(By.xpath("//*[@value='MRS']")).click(); and got error message – Arjun Nov 19 at 10:36
  • tried with Css as well driver.findElement(By.cssSelector("[value='MRS']")).click(); got error message – Arjun Nov 19 at 10:38

As per the HTML you have provided to invoke click() on the element related to the title MRS as the element is having the clearfix class, you need to induce WebDriverWait for the desired element to be clickable and you can use the following solution:

  • xpath:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@class='question-controls clearfix']/div[@class='question-group']//label[contains(.,'Mrs')]//following::input[1]"))).click();
    
  • still getting error with above code – Arjun Nov 21 at 13:46
  • Can you update the main question with the exact error trace logs for further analysis? – DebanjanB Nov 21 at 13:49
  • error message Exception in thread "main" org.openqa.selenium.TimeoutException: Expected condition failed: waiting for element to be clickable: By.xpath: //div[@class='question-controls clearfix']/div[@class='question-group']//label[contains(.,'Mrs')]//following::input[1] (tried for 20 second(s) with 500 milliseconds interval) – Arjun Nov 21 at 13:53

According to your screenshot, the radio button is hidden so no point trying to click on it. Instead try this:

driver.findElement(By.xpath("//div[@class='question-group']/label[text()='Mrs']")).click();

Let me know if this worked for you.

  • No it did not worked – Arjun yesterday

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.