1

The element that I’m talking about is the following:

<tbody>
    <tr
        id="tableUsers_0__TR"
        class="active"
        name="Tr_tableUsers[0]_Selected">

        <td>
            <input
                id="tableUsers_0__POSName"
                type="hidden"
                value="Indian"
                name="tableUsers[0].POSName"/>
            Indian
        </td>

The solution that I propose is:

The first step is locate the element by XPath:

string xpath = ".//*[@id='tableUsers_0__PDVCode']/..";

Then get the text with the method:

driver.FindElement(By.XPath(xpath)).Text

Is it the best way? Or not?

Is there another better way than this way? Better than the use of XPath?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user3446229
  • 95
  • 3
  • 3
  • 14

3 Answers3

2

JavaScript Executor can also be used to return the element's text. The following script can be used:

document.getElementById('<your id>').innerText

or

document.getElementById('<your id>').textContent

To get this, the object of JavascriptExecutor has to be created first. Refer to the following script:

IWebDriver driver; // A driver object is created in the code somewhere
IJavaScriptExecutor js = (IJavaScriptExecutor)driver;
string valueFromId = (string)js.ExecuteScript("return document.getElementById('<your id>').innerText");

You can also verify this text by running the JavaScript command (document.ge...) in the browser's console.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ayush Goel
  • 54
  • 7
  • What is "JavaScript Executor"? Can you [add](https://stackoverflow.com/posts/49048435/edit) a reference to it? (But ****************** ***without*** ****************** "Edit:", "Update:", or similar - the answer should appear as if it was written today.) – Peter Mortensen Nov 18 '22 at 22:10
  • What are the programming languages for the code snippets? Are the first two [JavaScript](https://en.wikipedia.org/wiki/JavaScript) and the last [C#](https://en.wikipedia.org/wiki/C_Sharp_%28programming_language%29)? – Peter Mortensen Nov 18 '22 at 22:23
1

Yes, a better way than using an XPath expression is to use By.id as:

driver.FindElement(By.Id("tableUsers_0__TR")).Text

Or use By.CssSelector:

driver.FindElement(By.CssSelector("tr#tableUsers_0__TR  td")).Text

Note: It always gives the last priority to the XPath locator, because it is much slower than other locators. In your case, if the element has an id attribute then the best way is to use By.Id(). Otherwise, try to locate the element using other locators, like By.Name(), By.ClassName(), By.CssSelector(), etc. if it could be possible.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Saurabh Gaur
  • 23,507
  • 10
  • 54
  • 73
  • The problem here is that the text is under the balise **** that's have Nothing as Locator(ni Id ni name ....). That's why i'm trynig to locate this Text using the balise input than the parent of this balise?! – user3446229 Aug 05 '16 at 15:01
  • @user3446229 you are right but by locating input you can't get the text while locating tr or td you can get the text in your provided HTML because it's inside tr and td node.. – Saurabh Gaur Aug 05 '16 at 15:04
  • If after input the text to be present any HTML tag then defenetely you can't get with using input following sibling but in your case it just text which is not present any tag but it's present tr and td tag..:) – Saurabh Gaur Aug 05 '16 at 15:07
1
  1. You can use the selector By.Id("your id").

  2. For some elements, it’s much more useful to use IWebElement.GetAttribute("text") than IWebElement.Text

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Kovpaev Alexey
  • 1,725
  • 6
  • 19
  • 38
  • The problem here is that the text is under the balise that's have Nothing as Locator(ni Id ni name ....). That's why i'm trynig to locate this Text using the balise input than the parent of this balise?! – user3446229 Aug 05 '16 at 15:02
  • @user3446229 I think you either use more precise xpath with /td at the end or use .Text. – Kovpaev Alexey Aug 05 '16 at 15:14