1

The element that i'm talking about is the folowing:

<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: First Step is Locate Element By Xath:

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

Then get Text with the method:

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

It's the best Way? or not? There is another way better thant this way? better than the use of Xpath.

user3446229
  • 95
  • 2
  • 3
  • 14

3 Answers3

2

Javascript Executor can also be used to return the element's text. Following script can be used:

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

or

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

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

IWebDriver driver; // 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 javacript command (document.ge...) in browser's console.

Stephen Kennedy
  • 18,869
  • 22
  • 90
  • 106
Ayush Goel
  • 54
  • 7
1

Yes, here is a best way than Xpath 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 :- Always gives the last priority to the Xpath locator because it is much slower than other locators, in your case if element has id attribute than best way to use By.Id() other wise try to locate element using other locators like By.Name(), By.ClassName(), By.CssSelector() etc If it could be possible.

Saurabh Gaur
  • 22,426
  • 8
  • 47
  • 70
  • 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 selector By.Id("your id").

2) For some elements its much more useful to use IWebElement.GetAttribute("text") than IWebElement.Text

Kovpaev Alexey
  • 1,645
  • 5
  • 19
  • 36
  • 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