How to use multiple locators to find an element in selenium webdriver

aswathy picture aswathy · Jul 24, 2017 · Viewed 12.5k times · Source

How can I locate an element in a page with selenium webdriver by using multiple locators at the same time . I am having 2 elements with same id but different values. So in order to access them I need to use a combination of both id and value. What is the syntax. I'm using java. Also I'm automating an application that works only in IE. Since I'm unable to access xpath, I'm not using it.

element=driver.findElement(By.id("id").cssSelector("input[@value='value1']"));

Answer

Gaurang Shah picture Gaurang Shah · Jul 24, 2017

Xpath allows you to use and and or to evalute multiple attributes. so you can form an xpath using this

//input[@id='id' and @value='value1' or @value='value2']

For example on google home page, there are two buttons, Google Search and I'm Feeling Lucky. Both has same type submit to find these buttons I can form an xpath similar to this

//input[@type='submit' and @value='Google Search' or @value="I'm Feeling Lucky"]