如何在java中为Selenium webdriver编写自己的自定义定位器? [英] How to write my own customize locator for Selenium webdriver in java?

查看:284
本文介绍了如何在java中为Selenium webdriver编写自己的自定义定位器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想编写自己的定位器来访问元素。 WebDriver的API提供了八个定位器,允许通过id,name属性,标签名称,完整或部分链接文本,XPath,类名和css选择器来检索元素。但是现在这些默认定位器对我来说还不够,因为我必须通过新属性访问元素。让我给出一个例子,以便你可以理解我真正想要的东西。

I want to write my own locator to access the elements. WebDriver’s API offers currently eight locators allowing to retrieve elements by id, name attribute, tag name, complete or partial link text, XPath, class name, and css selector. However those default locators not enough for me now because I have to access the elements through a new attribute. Let me give an xample so that you can understand what I really want here.

示例:
选择你的用户名:

现在我想编写一个代码,以便我可以使用myLocator定位器访问用户名按钮,如:

Now I want to write a code so that I can access the username button using the myLocator locator like:

*driver.findElement(By.myLocator("username")).*

如果有人能给我们一些好主意,我怎么能重写BY类来添加我自己的定位器,这将是非常有帮助的。

It would be very helpful if anybody can give us some good idea how could I rewrite the BY class to add my own locator.

提前感谢您的支持帮助。

Thank you in advance for your help.

推荐答案

您需要继承 By 类并提供 findElement findElements 方法的实现,因为这是实际元素查找的'meat'发生的位置。

You would need to subclass the By class and provide an implementation for findElement and findElements methods, since this is where the 'meat' of the actual element finding occurs.

然后你应该能够使用正常的 driver.FindElement 然后。

You should then be able to use it with the normal driver.FindElement then.

这篇关于如何在java中为Selenium webdriver编写自己的自定义定位器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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