Java Selenium - How to Click a Button that has no ID or ng-class in AngularJS based page

浪尽此生 提交于 2019-12-11 00:33:51

问题


The following code I've used is not clicking the button and showing the error message.

WebElement clickNextButton = webDriver.findElement(By.cssSelector("button[ng-class='btn-success']"));
clickNextButton.click();

Error message shows "no such element: Unable to locate element. {"method":"css selector","selector":"button[ng-class='btn-success']"}

I've also tried the following code segments without success:

  1.   WebElement clickNext1 = webDriver.findElement(By.cssSelector("button[ng-class='pccCTRL.pow.Page1InputsValid() ? 'btn-success' : 'btn-default'']"));
      clickNext1.click();
    
  2.  webDriver.findElement(By.partialLinkText("Next")).click();
    
  3.   webDriver.findElement(By.cssSelector("button[type='button']")).click();   
    

Here is the screenshot showing the html code segment of the button I'm trying to

Hoping to get feedback. Thank you.


回答1:


Ok, I was able to solve this issue by using By.xpath

Here is the code segment that solved it:

WebElement clickNextButton = webDriver.findElement(By.xpath("//button[contains(text(),'Next')]"));

clickNextButton.click();


来源:https://stackoverflow.com/questions/40272857/java-selenium-how-to-click-a-button-that-has-no-id-or-ng-class-in-angularjs-ba

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!