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

折月煮酒 提交于 2020-01-02 07:17:29

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!