Oscar Ubillús Oscar Ubillús - 2 months ago 6
HTML Question

Selenium: Click on a "<div><a></a></div>" button

I tried to click on a button. It has this structure:

HTML Code

<div class="button-wrapper" id="button-verify-wrapper">
<a x-ng-click="verifySfdcConnection()" class="clearfix float-left button-green">
<div class="icon-green icon-green-verify"></div>
<div class="button-label ng-binding">Verify Connection</div>
</a>
<div x-ng-class="{'connection-verified':wizardData.inputSource.sfdc.connectionStatus}" x-ng-show="wizardData.inputSource.sfdc.connectionStatus" style="" class="connection-verified"></div>
</div>


Any help how to do it? I tried this:

driver.findElement(By.xpath(".//*[@id='button-verify-wrapper']/a/div[1]")).click();


But it doesn't help.
Thanks

Answer

I think <a> element is clickable here. You should try to locate <a> element instead and perform click() action as below :-

  • using By.cssSelector() :-

    driver.findElement(By.cssSelector("div#button-verify-wrapper > a")).click();
    
  • using By.linkText() :-

    driver.findElement(By.linkText("Verify Connection")).click();
    
  • using By.xpath() :-

    driver.findElement(By.xpath(".//a[normalize-space(.) = 'Verify Connection']")).click();
    

If you're still unable to perform click, try as an alternate solution using JavascriptExecutor as below :-

((JavascriptExecutor)driver).executeScript("arguments[0].cli‌​ck()", driver.findElement(By.cssSelector("div#button-verify-wrapper > a")));