You need to remove /text()
from your XPath:
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>
.
Reference: https://github.com/seleniumhq/selenium-google-code-issue-archive/issues/5459#issuecomment-192110781
NOTE: Unfortunately, if you remove /text()
from the XPath, you will now be selecting the text of the entire element. Without any selectors wrapped around the "Page 1 to 20"
text, it is not possible to just "select" this text.
Here's a naive way to solve your specific problem:
Select the <div>
.Text
and the <table>
.Text
, then subtract (string.Replace
) the <table>
.Text
from the <div>
.Text
:
// "Some text\r\nSome text\r\nSome text\r\nSome text\r\nPage 1 to 20"
string divText = Driver.FindElement(By.Id("tabletransaction")).Text;
// ""Some text\r\nSome text\r\nSome text\r\nSome text"
string tableText = Driver.FindElement(By.Id("ticketransaction")).Text;
// "Page 1 to 20"
string remainingText = divText.Replace(tableText, string.Empty).Trim();