0

Trying to do a browserless solution as my cloud environment does not have Chrome/Firefox installed, but having issues with both of these drivers. Even headless Chrome/Firefox solutions still are looking for browser installations. I currently have had many different id/xpath combinations working for my automated testing but both PhantomJS and HTMLUnit cannot seem to find these elements. These elements I am trying to locate are in angular and seemed to make things a bit more difficult.

Here is the sample HTML I am trying to locate on the page:

<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>

I have tried all of these which do work for Chrome/Firefox, but not PhantomJS/HTMLUnit:

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();

There is also this element here, which works for HTMLUnit, but not PhantomJS:

<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>

Here is one solution that was working, I have tried other similar combos to what I have used above and usually do not have problems with Chrome/Firefox drivers.

driver.findElement(By.xpath("//ul[@class='tabs']//li[@id='invoiceMenu']")).click();
user3413540
  • 49
  • 1
  • 2
  • 10

1 Answers1

0

The element with text as Attachment Upload is an Angular element so you have to induce WebDriverWait with ExpectedConditions as elementToBeClickable as follows :

  • Using linkText :

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.linkText("Attachment Upload"))).click();
    
  • Using 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();
    

The element with text as Invoice is again an Angular element so you have to induce WebDriverWait with ExpectedConditions as elementToBeClickable as follows :

  • Using linkText :

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.linkText("Invoice"))).click();
    
  • Using xpath :

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//li[@class='active' and @id='invoiceMenu']/a[@aria-label='Invoice' and contains(.,'Invoice')]"))).click();
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Sorry about that, in OP I forgot to mention I have used WebDriverWaits. Here is one of the lines I had in there: wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[contains(text(), 'Attachment Upload')]")));. Both of your suggestions unfortunately timeout and cannot find the element. Exception in thread "main" org.openqa.selenium.TimeoutException: Expected condition failed: waiting for element to be clickable: By.xpath: Caused by: org.openqa.selenium.NoSuchElementException: Cannot locate an element using By.xpath: – user3413540 May 09 '18 at 15:16
  • @user3413540 _TimeoutException_ is the outcome of failed _ExpectedConditions_. Debug your code through `findElement()` inconjunction with `Thread.sleep();`. If you are able to locate the element and update the question with the observations. – undetected Selenium May 09 '18 at 15:20
  • yeah it never seems to find the element and will just timeout waiting for it. Is there some issue with the angular here giving these drivers that are not Chrome/Firefox a hard time? I have placed quite a few Thread.sleep(); lines throughout the code and they help, but I still can't find these particular elements. – user3413540 May 09 '18 at 15:32
  • Can you be specific with the exact error you see with `findElement()` inconjunction with `Thread.sleep()` – undetected Selenium May 09 '18 at 15:34
  • Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element with ID: 'invoiceMenuItem_4' Or with whatever ID/xpath combination I try. I tried quite long sleep times as well. – user3413540 May 09 '18 at 15:56
  • As `findElement()` padded with `Thread.sleep()` returned _NoSuchElementException_ so _WebDriverWait()_ will always return _TimeoutException_. So first and foremost you have to address **NoSuchElementException** which wasn't evident earlier. Follow this discussion [NoSuchElementExeption, selenium unable to locate element](https://stackoverflow.com/questions/48471321/nosuchelementexeption-selenium-unable-to-locate-element/48472940#48472940) to solve _NoSuchElementExeption_. – undetected Selenium May 09 '18 at 15:59
  • Thanks for the link. I ran through those and it cannot find the element no matter what I try. The JavaScript executions I can't even try because the first line throws the error with unable to locate it. It's strange. This is only with PhantomJS/HTMLUnit drivers. Chrome and Firefox DOES find the element. – user3413540 May 09 '18 at 16:22
  • Hmm, if you can share the actual _url_ maybe I can get my hands dirty – undetected Selenium May 09 '18 at 16:24
  • Unfortunately the site is private on my intranet. I do notice when viewing the page's source code when initially loading in the element I am looking for is not in the source code. I do see
  • – user3413540 May 10 '18 at 14:32
  • I think this may solve the issue: https://github.com/paul-hammant/ngWebDriver. I'm looking into it now and will report back :) – user3413540 May 10 '18 at 14:41