我在使用 java 查找 Selenium 中的 span 元素时遇到问题。
HTML 看起来像:
<div class="settings-padding">
<span>Settings</span>
</div>
我尝试了以下方法但没有成功:
By.xpath("span[.='Settings']")
and
By.xpath("span[text()='Settings']")
and
By.cssSelector("div[class='settings-padding']"))
以及其他一些类似的尝试。你能指出我最好的方法吗?就目前情况而言,我在 Eclipse 中不断收到“无法定位元素”错误。
你的一切xpath
看起来不错,只是一些句法上 http://www.w3schools.com/xsl/xpath_syntax.asp不正确。你失踪了//
在你的xpath
正确的xpath
如下:-
By by = By.xpath("//span[.='Settings']")
Or
By by = By.xpath("//span[text()='Settings']")
Or
By by = By.xpath("//div[@class='settings-padding']/span"))
或者你可以使用cssSelector
as :-
By by = By.cssSelector("div.settings-padding > span"))
使用上述任何一种通过定位器,您可以按如下方式定位元素:-
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement el = wait.until(presenceOfElementLocated(by));
希望能帮助到你...:)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)