尝试使用无浏览器解决方案,因为我的云环境没有安装Chrome / Firefox,但是这两个驱动程序都存在问题。即使是无头的Chrome / Firefox解决方案仍在寻找浏览器安装。我目前有许多不同的id / xpath组合用于我的自动化测试,但是PhantomJS和HTMLUnit似乎都找不到这些元素。我试图找到的这些元素是有角度的,似乎使事情变得更加困难。
以下是我尝试在网页上找到的示例HTML:
<script src="/dist/app.js">
我已经尝试了所有这些适用于Chrome / Firefox,但不适用于PhantomJS / HTMLUnit:
<li ng-repeat="invoiceMenuItem in nav.invoiceMenu.menuItems" ng-class="{'active':invoiceMenuItem.selected}" id="invoiceMenuItem_4" style="" class="ng-scope ibm-active" role="presentation">
<a ng-click="nav.onSelectInvoiceMenuItem(invoiceMenuItem);$event.stopPropagation();" href="" class="ng-binding" role="tab" aria-selected="false" tabindex="-1" aria-label="Attachment Upload">Attachment Upload</a>
</li>
<a ng-click="nav.onSelectInvoiceMenuItem(invoiceMenuItem);$event.stopPropagation();" href="" class="ng-binding" role="tab" aria-selected="false" tabindex="-1" aria-label="Attachment Upload">Attachment Upload</a>
此处还有这个元素,适用于HTMLUnit,但不适用于PhantomJS:
driver.findElement(By.xpath("//li[@id='invoiceMenuItem_4']")).click();
driver.findElement(By.id("invoiceMenuItem_4")).click();
driver.findElement(By.xpath("/html[1]/body[1]/div[1]/div[1]/div[2]/div[1]/div[3]/div[2]/div[2]/ul[1]/li[9]")).click();
driver.findElement(By.xpath("//*[contains(text(), 'Attachment Upload')]")).click();
driver.findElement(By.xpath("//li[@ng-class='active':invoiceMenuItem.selected']")).click();
这是一个有效的解决方案,我尝试过其他类似的组合,而不是上面使用过的,并且通常没有Chrome / Firefox驱动程序的问题。
<li id="invoiceMenu" ng-show="nav.invoiceMenu.show" ng-class="{'active':nav.invoiceMenu.selected}" class="active" role="presentation">
<a ng-click="nav.onSelectInvoiceMenu();$event.stopPropagation();" role="tab" aria-selected="false" tabindex="-1" aria-label="Invoice">Invoice</a>
</li>
答案 0 :(得分:0)
带有附件上传文字的元素是 Angular 元素,因此您必须使用 ExpectedConditions 来诱导 WebDriverWait as elementToBeClickable 如下:
使用 linkText :
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.linkText("Attachment Upload"))).click();
使用 xpath :
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//li[@class='ng-scope ibm-active']/a[@class='ng-binding' and @aria-label='Attachment Upload']"))).click();
文本为 Invoice 的元素又是 Angular 元素,因此您必须使用 ExpectedConditions 来诱导 WebDriverWait as elementToBeClickable 如下:
使用 linkText :
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.linkText("Invoice"))).click();
使用 xpath :
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//li[@class='active' and @id='invoiceMenu']/a[@aria-label='Invoice' and contains(.,'Invoice')]"))).click();