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>
5 Answers 5
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– Arjun2018年11月21日 16:50:29 +00:00Commented 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.Jan_Zdunek_DE– Jan_Zdunek_DE2018年11月21日 20:17:05 +00:00Commented Nov 21, 2018 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– Arjun2018年11月16日 11:24:01 +00:00Commented 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– Arjun2018年11月16日 11:27:43 +00:00Commented Nov 16, 2018 at 11:27
-
try
//div[@class='question-container']//div[class='question-group']/label[text()='Mrs']
or just debug by placingThread.sleep(3000)
before you clickNarendraR– NarendraR2018年11月16日 11:33:53 +00:00Commented 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– Arjun2018年11月16日 11:37:24 +00:00Commented Nov 16, 2018 at 11:37
-
is there any iframe ?NarendraR– NarendraR2018年11月16日 11:38:06 +00:00Commented Nov 16, 2018 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 messageArjun– Arjun2018年11月19日 10:36:31 +00:00Commented Nov 19, 2018 at 10:36
-
tried with Css as well driver.findElement(By.cssSelector("[value='MRS']")).click(); got error messageArjun– Arjun2018年11月19日 10:38:48 +00:00Commented Nov 19, 2018 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 codeArjun– Arjun2018年11月21日 13:46:47 +00:00Commented Nov 21, 2018 at 13:46
-
Can you update the main question with the exact error trace logs for further analysis?undetected Selenium– undetected Selenium2018年11月21日 13:49:00 +00:00Commented 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– Arjun2018年11月21日 13:53:25 +00:00Commented Nov 21, 2018 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.
div
? That will make it hard for anyone to help you as well.