简体   繁体   English

硒:点击“ <div><a></a></div> ”按钮

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

I tried to click on a button. 我试图单击一个按钮。 It has this structure: 它具有以下结构:

HTML代码

<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 谢谢

您应该使用css选择器单击链接,例如:
a[x-ng-click*='verifySfdcConnection']

I think <a> element is clickable here. 我认为<a>元素可在此处单击。 You should try to locate <a> element instead and perform click() action as below :- 您应该尝试定位<a>元素,然后执行click()操作,如下所示:-

  • using By.cssSelector() :- 使用By.cssSelector() :-

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

     driver.findElement(By.linkText("Verify Connection")).click(); 
  • using By.xpath() :- 使用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作为替代解决方案,如下所示:-

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

driver.findElement(By.id("button-verify-wrapper")).click(); driver.findElement(By.id( “按钮验证-包装”))点击();

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. 注意:1.如果直接给定ID或类名,而无需使用xpath,则可以直接使用By.id和By.class。2.要对Web元素执行操作,我们应该知道该元素ex的属性。 if it is not a button you can not perform click on it 如果不是按钮,则无法执行单击

试试这个: $("#button-verify-wrapper > a").click()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM