[英]How to use @FindBy annotation in Selenium for span text?
I want to know what can I do to get the "Apple" text in span element with @FindBy annotation.我想知道我该怎么做才能使用 @FindBy 注释在 span 元素中获取“Apple”文本。
Thats html code:那是 html 代码:
<span class="ui-cell-data">Apple</span>
and I tried something like that:我试过这样的事情:
@FindBy(className = "ui-cell-data:Samsung")
WebElement customerName;
but it didn't work!但它没有用!
As per the HTML
you have shared you may/maynot have been able to get the Apple text within the span element with : 按照您共享的
HTML
,您可能会/可能不会通过以下命令在span元素内获取Apple文本:
@FindBy(className = "ui-cell-data")
WebElement customerName;
Your code was nearly perfect but the trailing part in the className
as :Samsung
was unnecessary. 您的代码几乎是完美的,但
className
的结尾部分是:Samsung
是不必要的。
But again, looking at the class
attribute it is expected that a couple of more <span>
tags will have the same class
. 但是,再次查看
class
属性,可以预期会有更多的<span>
标签具有相同的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. 因此,为了唯一地标识预期的
WebElement
我们需要引用父节点并跟随其后继者到达该特定节点。
Finally, with the given HTML
the following code block will be much cleaner : 最后,使用给定的
HTML
,以下代码块将更加整洁:
cssSelector : cssSelector :
@FindBy(cssSelector = "span.ui-cell-data") WebElement customerName;
xpath : xpath :
@FindBy(xpath = "//span[@class='ui-cell-data']") WebElement customerName;
You can try with xpath stated below 您可以尝试使用下面所述的xpath
@FindBy(xpath = "//span[@class = 'ui-cell-data']")
private WebElement element;
You can use @FindBys
like a chained element look-up 您可以像链接元素查找一样使用
@FindBys
@FindBys({@FindBy(className = "ui-cell-data")})
private WebElement element;
Or try using below: 或者尝试使用以下方法:
@FindBy(xpath = "//*[@class = 'ui-cell-data']")
private WebElement element;
or 要么
@FindBy(css = ".ui-cell-data")
private WebElement element;
Hopefully it resolves your issue. 希望它能解决您的问题。
你好,上述解决方案部分解决了我的问题,我有多个具有相同类的按钮,在不同文本的跨度中,如何在上面的代码中添加文本与硒 java 中的 Xpath 对我来说这是行不通的 @FindBy(how= How.XPATH,using="//span[@class='mat-button-wrapper' and text()='North America']")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.