2

I have a button I want to click, Selenium isn't giving me an error just stops. I've tried both xpath and css and it just stops.

<a ng-click="login();" ng-disabled="loginForm.$invalid || submitting == true" ng-class="submitting == true ? 'm-progress' : ''" class="btn btn-primary btn-lg btn-block pink button" tabindex="4">sign in</a

This is the code for the button. And these are the two attempts I've tried:

driver.findElement(By.xpath(".//*[@id='ngdialog1']/div[2]/form/div[3]/div[1]/div[2]/a")).click();

Or with CSS:

driver.findElement(By.cssSelector(".btn.btn-primary.btn-lg.btn-block.pink.button")).click();

I am new to selenium so I could be doing it wrong. Any advice would be appreciated.

4

Try this xpath

//*[contains(@class,'btn btn-primary btn-lg btn-block pink button') and contains(@tabindex,'4')]

The xpath basically means : any element that has class attribute containing 'btn btn-primary btn-lg btn-block pink button' and a tabindex attribute containing 4.

You should try to read a little about how to write custom xpaths as addons like Firepath are not always useful in case of dynamic web application/pages testing

  • Thanks this worked, Interestingly enough my original syntax works, I just had to do it twice. – Christian Oct 20 '16 at 18:44
  • 2
    Glad to know this worked. The reason why your xpath may have not worked in the first run is either your application is dynamic and position of elements keep changing or the element had not loaded during your first run. If it was due to the later, I would recommend you add driver.manage().timeouts().implicitlyWait(60,TimeUnit.SECONDS) after declaring driver to ensure driver waits and tries to locate the element for 60 seconds if element is not found/loaded – BountyHunter Oct 20 '16 at 18:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.