如何在Selenium中找到具有特定文本的跨度? (使用Java) [英] How to locate a span with a specific text in Selenium? (Using Java)

查看:80
本文介绍了如何在Selenium中找到具有特定文本的跨度? (使用Java)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法使用Java在Selenium中定位span元素.

I'm having trouble locating a span element in Selenium using java.

HTML如下:

<div class="settings-padding">
<span>Settings</span>
</div>

而且我还没有运气尝试过以下方法:

And I've tried the following with no luck:

By.xpath("span[.='Settings']")

By.xpath("span[text()='Settings']")

By.cssSelector("div[class='settings-padding']"))

以及其他一些类似的尝试.您能指出我执行此操作的最佳方法吗?就目前而言,我在Eclipse中不断收到无法定位元素"错误.

as well as some other similar attempts. Could you point me to the best method to do this? As it stands I constantly get "Unable to locate element" error in eclipse.

推荐答案

您所有的xpath看起来都不错,只是一些语法上不正确.您在xpath

Your all xpath are looks OK, Just some syntactically incorrect. you are missing // in your xpath

正确的xpath如下:-

By by = By.xpath("//span[.='Settings']")

By by = By.xpath("//span[text()='Settings']")

By by = By.xpath("//div[@class='settings-padding']/span"))

或者您可以将cssSelector用作:-

By by = By.cssSelector("div.settings-padding > span"))

使用以上任何一个通过定位器,您都可以如下所示定位元素:-

Using anyone of the above By locator you can locate element as below :-

WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement el = wait.until(presenceOfElementLocated(by));

希望有帮助...:)

这篇关于如何在Selenium中找到具有特定文本的跨度? (使用Java)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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