在我的 selenium 自动化脚本中,我尝试通过其 xpath 值单击网页中的“策略”选项卡。
下面是实际的 html 代码:
....
<iframe src="/abc/api/public/v1/security/redirect">
#document
<!DOCTYPE html>
<html class="ng-scope" ng-app="CFWApp" lang="en">
<body>
<div class="container no-padding main-view flex-col">
::before
<cfw-menu class="ng-isolate-scope">
<div class="security-menu">
<ul class = "flex-row">
<li class="tab ng-scope active" ng-repeat="tab in $ctrl.items" ng-class="{'active': $ctrl.active == $index}" ui-sref="policy.templateList" href="#!/policy/template" style="">
<span class="tab-icon">...</span>
<span class='ng-binding'>Policy</span>
</li>
<li> ... </li>
</ul>
</div>
</cfw-menu>
...
</div>
</body>
</html>
这是我的 Java 代码:
driver.findElement(By.xpath("//li[starts-with(@class = 'tab') and contains(@ui-sref = 'policy.templateList')]/span[2]")).click();
但不知何故,这个 xpath 设置不起作用。有人可以帮我吗?非常感谢!
最佳答案
首先,您需要移至 iframe
WebElement iframe = driver.findElement(By.xpath("..."));
driver.switchTo().frame(popframe);
之后,您可以使用
driver.findElement(By.xpath("//span[text() = 'Policy']")).click();
但您需要确保该框架中不再有包含文本“Policy”的 span
。
关于java - 如何在java selenium中xpath这个 "Policy"选项卡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51180110/