在我的selenium自动化脚本中,我试图通过xpath值单击网页中的"Policy“选项卡。
下面是实际的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设置不起作用。有人能帮我吗?非常感谢!
发布于 2018-07-04 20:00:37
首先,你需要搬到iframe
WebElement iframe = driver.findElement(By.xpath("..."));
driver.switchTo().frame(popframe);之后,你可以用
driver.findElement(By.xpath("//span[text() = 'Policy']")).click();但是您需要确定的是,没有更多的span在该框架中包含文本“Policy”。
发布于 2018-07-04 20:04:20
( policy.templateList')]/span2")).click()
当您试图单击span元素(而不是按钮)时,它将无法工作。
有一次,我在python中也陷入了这种类型的问题,然后在selenium中使用action类。
首先,选择任何东西,然后通过selenium操作类,传递选项卡键,直到该元素聚焦,然后使用action类传递enter键,仅此而已。
祝你好运。
https://stackoverflow.com/questions/51180110
复制相似问题