How to access a second element that has the same class name in selenium using java

When trying to automate our application, there are two buttons with the same name.

I cannot find a way to recognize them. Please let me know what other ways to identify these elements in selenium webdriver in java can be

+6
source share
3 answers

You can use the xpath indexing option.

By.xpath("(//input[@name='Button'])[2]") 
+11
source

You can always go with xpath if there is no uniqueness with an attribute. E.g. if you want to find an element with the text foo and name button , then I will prefer xpath as below, if the name is not unique there:

 //*[@name='button' and text()='foo'] 

Or For another class, but with the same name

 //button[@name='button' and @class='xyz'] 

or for other text but with the same name

 //input[@name='button' and contains(text(),'Click Here')] 

or for different tags, but with the same name

 //button[@name='button'] //input[@name='button'] 

Just go with any unique property and create a customized xpath.

Hope you can also use java script for this, e.g. for

 WebElement butttonToClick = driver.findElement(By.name("button")); ((JavascriptExecutor)driver).executeScript("arguments[1].click();",butttonToClick ); 

Where arguments[1] means the second element with the same name.

+1
source

You can use xpath methods such as next-siblings / previous siblings.

For example, if the button is located to any unique web element, try to identify this web element first and using different xpath methods, such as siblings, content, previous siblings, you can access the web element.

0
source

Source: https://habr.com/ru/post/969752/


All Articles