3

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.

alecxe
11.4k11 gold badges52 silver badges107 bronze badges
asked Oct 20, 2016 at 17:50
0

1 Answer 1

5

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

Yu Zhang
9,9625 gold badges29 silver badges48 bronze badges
answered Oct 20, 2016 at 18:26
2
  • Thanks this worked, Interestingly enough my original syntax works, I just had to do it twice. Commented Oct 20, 2016 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 Commented Oct 20, 2016 at 18:57

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.