0

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="#">
demouser123
3,5325 gold badges30 silver badges41 bronze badges
asked Dec 21, 2015 at 9:16
6
  • 2
    paste yours html code we try to answer Commented Dec 21, 2015 at 9:21
  • This question doesn't clearly reflect what you are trying to ask. Please add more details Commented Dec 21, 2015 at 10:19
  • 1
    It 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?? Commented 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 it Commented Dec 22, 2015 at 7:13
  • yes ID is dynamic which are changing for each row Commented Dec 23, 2015 at 6:21

3 Answers 3

1

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.

answered Dec 23, 2015 at 4:04
3

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.

answered Dec 21, 2015 at 14:58
1
  • 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. Commented Dec 23, 2015 at 8:37
0

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.

Kate Paulk
31.5k8 gold badges56 silver badges109 bronze badges
answered Sep 14, 2016 at 14:28

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.