问题
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']"));
回答1:
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"]
回答2:
cssSelector can be used to locate elements by id, class or any other attribute, or combination of those. For example, you can locate the element using
element = driver.findElement(By.cssSelector("#id[value='value1']"));
来源:https://stackoverflow.com/questions/45278946/how-to-use-multiple-locators-to-find-an-element-in-selenium-webdriver