获取已找到的WebElement的By定位器 [英] Get the By locator of an already found WebElement

查看:278
本文介绍了获取已找到的WebElement的By定位器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种优雅的方式来获取Selenium WebElement的By定位器,我已经找到/识别出来了?



要清楚关于这个问题:我想要用定位器来查找元素。我在这种情况下对特定属性或特定定位器(如css-locator)感兴趣。



我知道我可以解析WebElement的toString()方法的结果:

  WebElement element = driver.findElement(By.id(myPreciousElement)); 
System.out.println(element.toString());

输出将是例如:


[[FirefoxDriver:WINDOWS上的firefox(....)] - > id:myPreciousElement]


如果你通过xpath找到你的元素:

  WebElement element = driver.findElement(By.xpath(// div [@someId ='someValue'])); 
System.out.println(element.toString());

然后您的输出将是:


[[FirefoxDriver:WINDOWS(....)上的firefox] - > xpath:// div [@someId ='someValue']]


所以我目前编写了自己的方法来解析这个输出并给我重新创建的定位器。





但是是否有更优雅的方式在Selenium中实现以获取用于查找元素的By定位器?

到目前为止我找不到一个。 p>

如果你确定没有开箱即用,你能想到API创建者可能无法提供此功能的任何理由吗?






*尽管事实上这与问题无关,如果有人想知道你为什么需要这个功能,只需要两个例子:




  • 如果您使用PageFactory,您很可能不会将定位器作为Page类中的成员变量,但您可能会稍后在处理页面元素时需要它们。

  • 你正在使用那些只使用没有PageFactory的页面对象模式的人的API,因此希望你交出定位器而不是元素本身。*


解决方案

答案是。默认情况下,您无法从以前找到的WebElement中提取



这就是说 - 可以实现一个自定义解决方案,但Selenium不提供此开箱即用。



请考虑以下内容,关于为什么..

  By by = By.id(someId); 
WebElement e = driver.findElement(by);

你已经拥有对象,所以你不需要打电话给 e.getBy()



我可能会在未来,但没有承诺。再次 - 它只是糖。


Is there an elegant way to get the By locator of a Selenium WebElement, that I already found/identified?

To be clear about the question: I want the "By locator" as used to find the element. I am in this case not interested in a specific attribute or a specific locator like the css-locator.

I know that I could parse the result of a WebElement's toString() method:

WebElement element = driver.findElement(By.id("myPreciousElement"));
System.out.println(element.toString());

Output would be for example:

[[FirefoxDriver: firefox on WINDOWS (....)] -> id: myPreciousElement]

if you found your element by xpath:

WebElement element = driver.findElement(By.xpath("//div[@someId = 'someValue']"));
System.out.println(element.toString());

Then your output will be:

[[FirefoxDriver: firefox on WINDOWS (....)] -> xpath: //div[@someId = 'someValue']]

So I currently wrote my own method that parses this output and gives me the "recreated" By locator.


BUT is there a more elegant way already implemented in Selenium to get the By locator used to find the element?

I couldn't find one so far.

If you are sure, there is none out of the box, can you think of any reason why the API creators might not provide this functionality?



*Despite the fact that this has nothing to do with the question, if someone wonders why you would ever need this functionality, just 2 examples:

  • if you use PageFactory you most likely will not have the locators as as member variables in your Page class, but you might need them later on when working with the page's elements.
  • you're working with APIs of people who just use the Page Object Pattern without PageFactory and thus expect you to hand over locators instead of the element itself.*

解决方案

Answer is No. You cannot extract a By from a previously found WebElement by default.

That being said - it's possible to implement a custom solution, but Selenium does not offer this out-of-the-box.

Consider the following, on "why"..

By by = By.id("someId");
WebElement e = driver.findElement(by);

you already have the By object, so you wouldn't need to call something like e.getBy()

I might implement something like this in the future, but no promises. Again - it's just sugar.

这篇关于获取已找到的WebElement的By定位器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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