5

I'm doing an automation using Selenium WebDriver and I'm getting all the elements of a Table.

I used the code below:

var qntd= driver.FindElements(By.XPath("//*[@id='dataTable']/tbody/tr")).Skip(3);

I then realized that each element generated an Id that is not of the Id as shown in the Html

enter image description here enter image description here

I tried to retrieve this id with a query but I could not because the return is the HTML Id attribute

var query = from a in qntd
                select a.GetAttribute("Id");

Where does this ID come from and how do I retrieve it?

Gokul
  • 788
  • 2
  • 12
  • 30
G. Sena
  • 429
  • 3
  • 7
  • 17
  • Possible duplicate of [Getting ALL attributes from an IWebElement with Selenium WebDriver](https://stackoverflow.com/questions/21681897/getting-all-attributes-from-an-iwebelement-with-selenium-webdriver) – Markiian Benovskyi Jul 27 '17 at 15:01
  • 1
    I do not want to get the HTML ID attribute, this I know how to do. I want to get the ID of this {Element (id= 0. 834823498234-1 } – G. Sena Jul 27 '17 at 15:35
  • 1
    That ID is not related to the HTML ID attribute... it's some sort of internal ID related to the collection at runtime. Why would you want that? It's likely to change each time you run the script. What is it that you are trying to do? There surely is a better way to do what you want without needing that ID. – JeffC Jul 27 '17 at 17:05

3 Answers3

1

Unfortunately you won't be able to access this field because the method "FindElements" returns the elements in the form of IWebElement. IWebElement does not have a method implemented to get the ID value you are looking for.

If the FindElements method was to return the type RemoteWebElement, or even ChromeWebElement, we would be able to access this field because the RemoteWebElement has a method to access it. However, this method is not implemented in the interface. So there is no way for us to get it. I've played around with casting and haven't been able to cast in the correct spot. So as of now, I'm not seeing a way to get this ID.

See RemoteWebElement.cs source code for more information: https://github.com/SeleniumHQ/selenium/blob/master/dotnet/src/webdriver/Remote/RemoteWebElement.cs

Hopefully this helps you out a bit

st0ve
  • 519
  • 3
  • 18
0

If you want to get the IDs from all elements as a List<String> you could do

List<String> query = qntd.Select(it => it.Id).ToList();
Looki
  • 832
  • 6
  • 13
0

It's a special id generated by the driver itself, not html property. You can use type cast to RemoteWebElement and then get this value using reflection as it is protected. But hardly this is the thing you need in your tests.

https://seleniumhq.github.io/selenium/docs/api/dotnet/?topic=html/P_OpenQA_Selenium_Remote_RemoteWebElement_Id.htm

zffman
  • 53
  • 5
  • BTW you can parse it from ToString() method if I'm not mistaken. Not the smartest solution ever, but might work. – zffman Aug 29 '18 at 11:46