1

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

7
  • 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. Commented Nov 14, 2018 at 16:23
  • yes in xpath and i have pasted complete xml. there is nothing missing
    – Arjun
    Commented Nov 14, 2018 at 16:45
  • i have added screenshot as well
    – Arjun
    Commented Nov 14, 2018 at 16:51
  • Please edit the question and add the code you have tried and the results you got.
    – Kate Paulk
    Commented Nov 14, 2018 at 19:54
  • Hi Kate,I have added the code and error message in above question.
    – Arjun
    Commented Nov 15, 2018 at 10:22

5 Answers 5

1

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.

2
  • used below code and getting error message driver.findElement(By.xpath("//div[@class='question-group']/label[text()='Mrs']")).click();
    – Arjun
    Commented Nov 21, 2018 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. Commented Nov 21, 2018 at 20:17
0

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']
14
  • tried below code and still showing error driver.findElement(By.xpath("//div[@class='question-group']/input[@value='MRS']")).click();
    – Arjun
    Commented Nov 16, 2018 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
    Commented Nov 16, 2018 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
    Commented Nov 16, 2018 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
    Commented Nov 16, 2018 at 11:37
  • is there any iframe ?
    – NarendraR
    Commented Nov 16, 2018 at 11:38
0

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.

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

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();
    
3
  • still getting error with above code
    – Arjun
    Commented Nov 21, 2018 at 13:46
  • Can you update the main question with the exact error trace logs for further analysis? Commented Nov 21, 2018 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
    Commented Nov 21, 2018 at 13:53
0

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.

1
  • No it did not worked
    – Arjun
    Commented Dec 10, 2018 at 13:54

Your Answer

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

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