使用 Selenium WebDriver 检测下拉菜单 [英] Detecting Drop down with Selenium WebDriver

查看:77
本文介绍了使用 Selenium WebDriver 检测下拉菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

http://i.stack.imgur.com/L4WUv.jpg

网格链接

我正在尝试检测此页面上的不同下拉菜单(由文本框的过滤器描述).我遇到的问题是过滤器似乎都具有相同的 ID.我可以让 webdriver 找到初始过滤器按钮,但不能定位下拉列表中的选项.

I'm trying to detect the different drop downs on this page (depicted by the filters by the text boxes). The problem i'm having is that it seems that the filters all have the same ids. I can get the webdriver to find the initial filter button but not target the options in the drop down.

  • 请注意,我所说的过滤器是来自漏斗按钮的过滤器.例如包含,isEqual,之间等 *

这是错误的,但举个例子

This is wrong but an example

it('Should filter grid to -contain Civic', function() {
    browser.element(by.id('ctl00_ContentPlaceHolder1_RadGrid1_ctl00_ctl02_ctl03_FilterTextBox_Model')).sendKeys("civic");
    browser.element(by.id('ctl00$ContentPlaceHolder1$RadGrid1$ctl00$ctl02$ctl03$FilterTextBox_Model')).click(); 
    browser.element(by.xpath("//*[contains(text(), 'Contains')]")).click();
})

推荐答案

注意 正在寻找的答案位于此答案底部的EDIT"一词之后.保留此答案的其余部分,因为它仍然有用.

NOTE The answer that was being looked for is at the bottom of this answer after the word "EDIT". The rest of this answer is retained because it is still useful.

测试动态生成 id 和其他属性的网页是一项挑战.有时您只需要弄清楚如何使用 xpath 导航稳定属性.这是找到所有四个下拉菜单的 xpath:

It's a challenge to test webpages that dynamically generate ids and other attributes. Sometimes you just have to figure out how to navigate the stable attributes with an xpath. Here's an xpath that finds all four dropdowns:

//tr[@class='rgFilterRow']//input

要区分每一个,你可以这样做:

To differentiate between each one, you can do this:

(//tr[@class='rgFilterRow']//input)[1]      // Brand Name
(//tr[@class='rgFilterRow']//input)[2]      // Classification
(//tr[@class='rgFilterRow']//input)[3]      // Transmission
(//tr[@class='rgFilterRow']//input)[4]      // Fuel

使用数字来指定 xpath 中的元素并不是真正可取的(如果表中列的顺序发生变化,它的行为将不正确),但在这种情况下,这可能是您能做的最好的事情,因为所有动态 ID 和普遍缺乏可靠的识别属性.

Using numbers to specify elements in an xpath isn't really desirable (it will behave incorrectly if the order of columns in the table changes), but it's probably the best you can do in this case because of all the dynamic ids and general lack of reliable identifying attributes.

编辑

我误解了您想要获取的内容,因为我没有查看您链接到的图片.打开该菜单后,您应该能够使用 xpath 通过文本获取您想要的任何选项.例如,如果您想要包含"选项:

I misunderstood what you were trying to get because I didn't look at the image that you linked to. Once you've opened up that menu, you should be able to use an xpath to get whichever option you want by the text. For example, if you want the "Contains" option:

//a[@class='rmLink']//span[text()='Contains']

这篇关于使用 Selenium WebDriver 检测下拉菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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