如何使用 Java Selenium 单击 AngularJS 链接?

我在 Java 中使用 Selenium WebDriver。我做了大部分事情,但我被困在某一点上,我有以下 HTML 代码:


<a ng-href="#/studyenrollments/new" ng-show="canCreate" class="btn btn-primary edit-btn" href="#/studyenrollments/new">New Study Enrollment</a>

我试过以下方法:


//new study enrollment

driver.findElement(By.linkText("New Study Enrollment")).click();    

driver.findElement(By.xpath("/html/body/div[2]/div[3]/a")).click();

((JavascriptExecutor)driver).executeScript("document.querySelector(\"body > div.container.page.ng-scope > div.text-right.ng-scope > a\")");

我希望点击“新研究注册”按钮并进入下一页以完成注册过程


侃侃无极
浏览 155回答 2
2回答

蝴蝶不菲

检查元素是否在框架内如果是 - 然后 - 首先选择框架 -Select Frame&nbsp; &nbsp; name or id of iframe然后执行以下操作 -WebDriverWait wait = new WebDriverWait(driver, 10);wait.until(ExpectedConditions.elementToBeClickable(By.xpath(".//a[@ng-show='canCreate']"))).click();如果没有框架那么你可以直接与元素交互 -WebDriverWait wait = new WebDriverWait(driver, 10);wait.until(ExpectedConditions.elementToBeClickable(By.xpath(".//a[@ng-show='canCreate']"))).click();

长风秋雁

您需要添加显式等待条件,尤其是在处理包含 Angular JS 元素的页面时&nbsp;WebElement newStudyEnrollment= driver.findElement(By.xpath("//a[contains(text(),'New&nbsp;&nbsp; &nbsp; &nbsp; Study Enrollment')]"));&nbsp; &nbsp; WebDriverWait wait = new WebDriverWait(driver, 10);&nbsp; &nbsp; wait.until(ExpectedConditions.elementToBeClickable(newStudyEnrollment));&nbsp; &nbsp; newStudyEnrollment.click();
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java