Selenium: Click on a “<div><a></a></div>” button

空扰寡人 提交于 2021-01-27 18:18:33

问题


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

<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


回答1:


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



回答2:


You should click on the link using a css selector like:
a[x-ng-click*='verifySfdcConnection']




回答3:


driver.findElement(By.id("button-verify-wrapper")).click();

Note: 1. if ID or class name is directly given no need to use xpath, can directly use By.id and By.class 2. To perform an action on web element we should know the properties of that element ex. if it is not a button you can not perform click on it




回答4:


try this: $("#button-verify-wrapper > a").click()



来源:https://stackoverflow.com/questions/39620887/selenium-click-on-a-diva-a-div-button

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