如何使用多个定位器在Selenium WebDriver中查找元素

如何使用多个定位器在Selenium WebDriver中查找元素

问题描述:

如何使用Selenium Webdriver通过同时使用多个定位器来定位页面中的元素.我有2个具有相同ID但值不同的元素.因此,为了访问它们,我需要同时使用id和value的组合.语法是什么?我正在使用Java.另外,我正在自动化仅在IE中工作的应用程序.由于无法访问xpath,因此未使用它.

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']"));

Xpath允许您使用andor评估多个属性. 因此您可以使用

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']

例如,在Google主页上,有两个按钮,Google SearchI'm Feeling Lucky.两者都具有相同的submit类型以找到这些按钮,我可以形成一个与此类似的xpath

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"]