Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium webdriver: finding all elements with similar id

I have this xpath: //*[@id="someId::button"]

Pressing it shows a dropdown list of values.

Now, I know all the elements in the list have an id like this :

//*[@id="someId--popup::popupItemINDEX"]

, where INDEX is a number from 1 to whatever the number of options are.

I also know the value which I must click.

One question would be: since I will always know the id of the button which generates the dropdown, can I get all the elements in the dropdown with a reusable method? (I need to interact with more than one dropdown)

The way I thought about it is: get the root of the initial ID, as in:

//*[@id="someId 

then add the rest : --popup::popupItem. I also need to add the index and I thought I could use a try block (in order to get though the exceptions when I give a bigger than expected index) like this:

 for(int index=1;index<someBiggerThanExpectedNumber;index++){
     try{
         WebElement aux= driver.findElement(By.xpath(builtString+index+"\"]"));
         if(aux.getText().equals(myDesiredValue))
             aux.click();
     }catch(Exception e){}
 }

Note that I am using the webdriver api and java.

I would like to know if this would work and if there is an easier way of doing this, given the initial information I have.

EDIT: The way I suggested works, but for an easier solution, the accepted answer should be seen

like image 568
CosminO Avatar asked May 22 '12 08:05

CosminO


People also ask

How to find elements using Selenium WebDriver?

We can find elements using Selenium webdriver with the help of the findElements method. This can be applied to the locators like id, class, name, link text, partial link text, css, xpath and tagname. The findElements method returns a list of elements which match with the locator (with the By object) passed as a parameter to the method.

How to find elements using the Attribute “class name” in selenium?

How to find an element using the attribute “class name” in Selenium? Here the value of the “class” attribute is passed as the locator. This strategy is mostly used to find multiple elements that use similar CSS classes. The locator strategy ‘By Class Name‘ finds the elements on the web page based on the CLASS attribute value.

What is the difference between findelement and findelements in selenium?

findElement (): This method uniquely finds a web element on the web page. findElements (): This method finds a list of web elements on the web page. Let's understand the usage and details of these methods in the following sections: The findElement () method of the Selenium WebDriver finds a unique web element within the webpage.

What are the different types of locators in selenium?

Selenium provides us eight types of locators or Identifiers which are as follows: 1 By Using Id: Id is an attribute assigned to a web element, and it uniquely defines the element on the page. So, it is the most preferred locator. 2 findElement () method: 3 By Using Name: 4 Using Class Name: 5 By Using Tag Name: More items


2 Answers

As a rule of thumb, try to select more elements by one query, if possible. Searching for many elements one-by-one will get seriously slow.

If I understand your needs well, a good way to do this would be using

driver.findElement(By.id("someId::button")).click();
driver.findElement(By.xpath("//*[contains(@id, 'someId--popup::popupItem') " +
    "and text()='" + myDesiredValue + "']"))
    .click();

For more information about XPath, see the spec. It's surprisingly a very good read if you can skip the crap!

That finds and clicks an element with text equal to you desired value which contains "someId--popup::popupItem" in its ID.

List<WebElement> list = driver.findElements(By.xpath("//*[contains(@id, 'someId--popup::popupItem')]"));

That finds all just all elements that contain "someId--popup::popupItem" in their ID. You can then traverse the list and look for your desired element.

Did you know you can call findElement() on a WebElement to search just it's children? - driver.findElement(By.id("someId")).findElements(By.className("clickable"))

Without a peek on the underlying HTML, I guess I can't offer the best approach, but I have some in my head.

like image 65
Petr Janeček Avatar answered Oct 06 '22 23:10

Petr Janeček


Have you tried using JavascriptExecutor?

If you are willing to write a little JavaScript then this would be straightforward than in java (I think)

All you will need to do is have some JavaScript crawl through the DOM subtree, and return a list of DOM elements matching your criteria. WebDriver will then happily marshall this as List<WebElement> in the java world.

like image 45
Ashwin Prabhu Avatar answered Oct 06 '22 23:10

Ashwin Prabhu