Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to grab just element id - using Selenium WebDriver 2

EDIT: I also tried this

 var webElements1 = (Driver.FindElements(By.XPath("//*[@id='ctl00_ContentPlaceHolder1_Control1_lstCategory']//input"))).ToList();

I get the empty Text

I am trying to find a way to grab just ID from the list i am getting and below is my code and a print shot of my screen.

//WebDriver getting a list of Text

the below code returns me the correct number of records but it just give me the Text but I am after Text and Id of an particular Text

I tried this:

var webElements1 = (Driver.FindElements(By.XPath("//*[@id='ctl00_ContentPlaceHolder1_Control1_lstCategory']/tbody/tr/td/span"))).ToList();

this

var webElements2 = (Driver.FindElements(By.XPath("//*[@id='ctl00_ContentPlaceHolder1_Control1_lstCategory']/tbody/tr/td"))).ToList();

and this...

var webElements3 = (Driver.FindElements(By.XPath("//*[@id='ctl00_ContentPlaceHolder1_Control1_lstCategory']"))).ToList();

the all code of line gives me the correct returns but without Id.

Here is the print screen of my page:

enter image description here

like image 799
Nick Kahn Avatar asked Sep 20 '12 16:09

Nick Kahn


1 Answers

After getting all the elements using below method, run in loop to get all element's ids:

List<WebElement> element = driver.findElements(By.XPath("//*[@id='ctl00_ContentPlaceHolder1_Control1_lstCategory']//input")));

for(WebElement ele:elements)
{

        ele.getAttribute("id");     // for getting id of each element

        ele.getText();              //for getting text of each element

}
like image 125
Santoshsarma Avatar answered Sep 22 '22 06:09

Santoshsarma