Skip to content
This repository was archived by the owner on Nov 29, 2018. It is now read-only.

InvalidSelectorError: The result of the xpath expression is: [object Text] #5459

Closed
lukeis opened this issue Mar 4, 2016 · 8 comments
Closed

Comments

@lukeis
Copy link
Member

lukeis commented Mar 4, 2016

Originally reported on Google Code with ID 5459

Here is the html source code of the page:

<!DOCTYPE html>
<html>
<body>

<form action="">
<input type="radio" name="sex" value="female">Male<br>
<input type="radio" name="sex" value="male">Female
</form>

<p>Note: When a user clicks on a radio-button, it becomes checked, and all other radio-buttons
with equal name become unchecked.</p>

</body>
</html>


http://www.w3schools.com/html/tryit.asp?filename=tryhtml_radio

What steps will reproduce the problem?
1. Open the application using selenium webDriver

FirefoxDriver Browser = new FirefoxDriver();
Browser.get("http://www.w3schools.com/html/tryit.asp?filename=tryhtml_radio");



2. Read the text/label of the radio button whose html attribute value="female"
String XPATH = "//input[@value='female']/following-sibling::text()"
String LABEL = Browser.findElement(By.xpath(XPATH)).getText();

3. An InvalidSelectorError exception is raised by WebDriver:
Exception in thread "main" org.openqa.selenium.InvalidSelectorException: The given
selector //input[@value='Red']/following-sibling::text() is either invalid or does
not result in a WebElement. The following error occurred:
InvalidSelectorError: The result of the xpath expression "//input[@value='Red']/following-sibling::text()"
is: [object Text]. It should be an element.

This works fine with Selenium IDE and RC but not with WebDriver.

What is the expected output? What do you see instead?
Ideally it should return the label of the radio button

Selenium version: 2.31.0
OS: Windows XP Professional SP2
Browser: FireFox
Browser version: 19.0.2

Reported by vishalkharge on 2013-04-07 07:47:52

@lukeis
Copy link
Member Author

lukeis commented Mar 4, 2016

Your xpath doesn't return an element; it returns a text node. While this might have
been perfectly acceptable in Selenium RC (and by extension, Selenium IDE), the methods
on the WebDriver WebElement interface require an element object, not just any DOM node
object. WebDriver is working as intended. To fix the issue, you'd need to change the
HTML markup to wrap the text node inside an element, like a <span>.

Reported by james.h.evans.jr on 2013-04-07 14:29:26

  • Status changed: WorkingAsIntended
  • Labels removed: Status-Untriaged

@lukeis
Copy link
Member Author

lukeis commented Mar 4, 2016

Issue 3635 has been merged into this issue.

Reported by barancev on 2013-10-21 19:24:55

@lukeis
Copy link
Member Author

lukeis commented Mar 4, 2016

Issue 2871 has been merged into this issue.

Reported by barancev on 2013-10-21 19:25:23

@lukeis
Copy link
Member Author

lukeis commented Mar 4, 2016

You can use this code : 

FoundObjectCount = FindWebElementCount(ieDriver,"xpath", "//input[@type='radio' and
(contains(@name,'XXX'))]");

            for(int objcnt = 1; objcnt <= FoundObjectCount; objcnt++) {

                FoundObject = ieDriver.findElement(By.xpath(".//table[@class='XXX']/tbody/tr["+objcnt+"]/td/label"));

                if (FoundObject.getText().equalsIgnoreCase(strSalecycle)) {
                    FoundObject = ieDriver.findElement(By.xpath(".//table[@class='XXX']/tbody/tr["+objcnt+"]/td/input"));
                    ObjectClick(FoundObject);
                    LogInfo("WebElement is Selected of Object id is: "+FoundObject.getAttribute("id"));
                    break;
                } else{
                    LogInfo("WebElement is Not Present in UI");
                    break;
                }

When the HTML looks like this:

<Table>
<TR>
<TD><INPUT id=XXX  value="JUNK" type=radio name=ABCXXX><LABEL for=idXXX>TEXTNEEDTOREAD1
</LABEL>
</TD>
</TR>
<TR>
<TD><INPUT id=XXX  value="JUNK" type=radio name=ABCXXX><LABEL for=idXXX>TEXTNEEDTOREAD2
</LABEL>
</TD>
</TR>
</Table>

Reported by chandra402 on 2014-01-07 16:04:50

@lukeis
Copy link
Member Author

lukeis commented Mar 4, 2016

Any updates regarding this issue?

Reported by rizalyn.magaru on 2014-09-22 08:52:20

@lukeis
Copy link
Member Author

lukeis commented Mar 4, 2016

This is a "working as intended" issue, so if you've got any sample pages or code that
could help prove otherwise then you'll need to post them - otherwise there is literally
nothing anybody can do because the only piece of code and reproducible material someone
has given has proven WebDriver *is* working as it's intended to do.

Reported by arran.huxtable on 2014-09-22 09:17:41

@lukeis
Copy link
Member Author

lukeis commented Mar 4, 2016

http://only-testing-blog.blogspot.in/2014/01/textbox.html

I want to retreive and verify for the label 'First name' in the  above page.I've used
below 
String s3=d.findElement(By.xpath(".//text()[followingsibling::input[@name='fname']]")).getText();



Im getting

Caused by: org.openqa.selenium.InvalidSelectorException: The given selector .//text()[following-sibling::input[@name='fname']]
is either invalid or does not result in a WebElement. The following error occurred:
InvalidSelectorError: The result of the xpath expression ".//text()[following-sibling::input[@name='fname']]"
is: [object Text]. It should be an element.
For documentation on this error, please visit: http://seleniumhq.org/exceptions/invalid_selector_exception.html

pleasee help me out

Reported by vinod.komeershetty@zenq.com on 2015-01-26 13:18:37

@lukeis
Copy link
Member Author

lukeis commented Mar 4, 2016

Reported by luke.semerau on 2015-09-17 18:17:07

  • Labels added: Restrict-AddIssueComment-Commit

@lukeis lukeis closed this as completed Mar 4, 2016
@SeleniumHQ SeleniumHQ locked and limited conversation to collaborators Mar 4, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

1 participant