如何使用多个定位器在Selenium WebDriver中查找元素 [英] How to use multiple locators to find an element in selenium webdriver

查看:416
本文介绍了如何使用多个定位器在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"]

这篇关于如何使用多个定位器在Selenium WebDriver中查找元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆