how do i use list for selenium web driver in java
I need to select id which are continuously changing, and one id is in one row.
need to select id from each
Edit :
<tr id="tr-132885-id" class="bgwhite" onmouseout="hilightOnMouseOver('tr-132885-id','out')" onmouseover="hilightOnMouseOver('tr-132885-id','in')">
<td class="companynm-td" valign="top"> <div id="expand-132885-div" class="left" style="vertical-align:top;">
<a onclick="expandCompanyDetail('185159','132885','show','current','1');return false;" href="#">
-
2paste yours html code we try to answersameer joshi– sameer joshi2015年12月21日 09:21:15 +00:00Commented Dec 21, 2015 at 9:21
-
This question doesn't clearly reflect what you are trying to ask. Please add more detailsdemouser123– demouser1232015年12月21日 10:19:55 +00:00Commented Dec 21, 2015 at 10:19
-
1It can be done easily by xpath (by position). But we need to see your HTML and also if you have any tried any code so far??TestingWithArif– TestingWithArif2015年12月21日 12:48:06 +00:00Commented Dec 21, 2015 at 12:48
-
I guess id is dynamic which gets changed everytime,you need to get the pattern of the id, and iterate over itsaikrishna– saikrishna2015年12月22日 07:13:09 +00:00Commented Dec 22, 2015 at 7:13
-
yes ID is dynamic which are changing for each rowPRasd– PRasd2015年12月23日 06:21:01 +00:00Commented Dec 23, 2015 at 6:21
3 Answers 3
Below is a way to list URLs, you can change the By.tagName
to your required selector like By.id
WebDriver driver = FirefoxDriver();
driver.get("www.google.com");
List<WebElement> elements = driver.findElements(By.tagName("a"));
Hope this will help you out.
Ask your developers to add a name (which does not need to be unique) to relevant elements. find_elements (Java may have different spelling) returns a LIST of elements.
If you cannot have name, you can find by CSS class. Xpath is locator of last hope, too brittle and too slow.
-
Yes! If locating an element is that difficult, its better to ask the developers to put unique, static and less fragile attributes to locate the element.Faiz– Faiz2015年12月23日 08:37:06 +00:00Commented Dec 23, 2015 at 8:37
If you want to store each and every ID from all of the rows or columns then look at the below code :
Step1: grab the table, Step2: grab the rows, Step3: grab the columns.
WebElement table=driver.findElement(By.id("someID"));
//you can choose id,xpath or any other unique locator.
List<WebElement> rowID=table.findElements(By.tagName("tr"));
for(WebElement row:rowID){
List<WebElement> colID=row.findElements(By.tagName("td"));
}
now colID and rowID contains ID's of columns and rows respectively.
Explore related questions
See similar questions with these tags.