如何在 Selenium 中将 @FindBy 注释用于跨文本? [英] How to use @FindBy annotation in Selenium for span text?
问题描述
我想知道如何使用 @FindBy 注释在 span 元素中获取Apple"文本.
I want to know what can I do to get the "Apple" text in span element with @FindBy annotation.
这是 html 代码:
Thats html code:
<span class="ui-cell-data">Apple</span>
我尝试了类似的方法:
@FindBy(className = "ui-cell-data:Samsung")
WebElement customerName;
但是没有用!
推荐答案
根据您分享的 HTML
,您可能/可能无法获得 Apple 文本在带有 :
As per the HTML
you have shared you may/maynot have been able to get the Apple text within the span element with :
@FindBy(className = "ui-cell-data")
WebElement customerName;
您的代码几乎是完美的,但 className
中的尾随部分 :Samsung
是不必要的.
Your code was nearly perfect but the trailing part in the className
as :Samsung
was unnecessary.
但是,再次查看 class
属性,预计会有更多的 标签具有相同的 <代码>类代码>.因此,为了唯一标识预期的
WebElement
,我们需要引用父节点并跟随其后代到达该特定节点.
But again, looking at the class
attribute it is expected that a couple of more <span>
tags will have the same class
. So to uniquely identify the intended WebElement
we need to refer to a parent node and follow its decendents to reach this particular node.
最后,使用给定的 HTML
,下面的代码块会更简洁:
Finally, with the given HTML
the following code block will be much cleaner :
cssSelector:
@FindBy(cssSelector = "span.ui-cell-data")
WebElement customerName;
xpath:
@FindBy(xpath = "//span[@class='ui-cell-data']")
WebElement customerName;
这篇关于如何在 Selenium 中将 @FindBy 注释用于跨文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!