[英]How to use @FindBy annotation in Selenium for span text?
我想知道我该怎么做才能使用 @FindBy 注释在 span 元素中获取“Apple”文本。
那是 html 代码:
<span class="ui-cell-data">Apple</span>
我试过这样的事情:
@FindBy(className = "ui-cell-data:Samsung")
WebElement customerName;
但它没有用!
按照您共享的HTML
,您可能会/可能不会通过以下命令在span元素内获取Apple文本:
@FindBy(className = "ui-cell-data")
WebElement customerName;
您的代码几乎是完美的,但className
的结尾部分是:Samsung
是不必要的。
但是,再次查看class
属性,可以预期会有更多的<span>
标签具有相同的class
。 因此,为了唯一地标识预期的WebElement
我们需要引用父节点并跟随其后继者到达该特定节点。
最后,使用给定的HTML
,以下代码块将更加整洁:
cssSelector :
@FindBy(cssSelector = "span.ui-cell-data") WebElement customerName;
xpath :
@FindBy(xpath = "//span[@class='ui-cell-data']") WebElement customerName;
您可以尝试使用下面所述的xpath
@FindBy(xpath = "//span[@class = 'ui-cell-data']")
private WebElement element;
您可以像链接元素查找一样使用@FindBys
@FindBys({@FindBy(className = "ui-cell-data")})
private WebElement element;
或者尝试使用以下方法:
@FindBy(xpath = "//*[@class = 'ui-cell-data']")
private WebElement element;
要么
@FindBy(css = ".ui-cell-data")
private WebElement element;
希望它能解决您的问题。
你好,上述解决方案部分解决了我的问题,我有多个具有相同类的按钮,在不同文本的跨度中,如何在上面的代码中添加文本与硒 java 中的 Xpath 对我来说这是行不通的 @FindBy(how= How.XPATH,using="//span[@class='mat-button-wrapper' and text()='North America']")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.