【问题标题】:Selenium: Click on a "<div><a></a></div>" buttonSelenium:单击“<div><a></a></div>”按钮
【发布时间】:2017-01-29 23:02:12
【问题描述】:

我试图点击一个按钮。它有这样的结构:

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

任何帮助如何做到这一点?我试过这个:

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

但这无济于事。 谢谢

【问题讨论】:

  • 你的意思是它没有帮助??有没有例外??你确定想要div 元素是可点击的还是a 元素是可点击的??
  • 我认为是按钮内的链接.. 也不例外,只是不要点击它。这是代码的按钮:imgur.com/a/OmcDS

标签: javascript jquery html selenium selenium-webdriver


【解决方案1】:

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

【讨论】:

  • 我试过这个:driver.findElement(By.cssSelector("a[x-ng-click*='verifySfdcConnection']")).click();没用。
【解决方案2】:

我认为&lt;a&gt; 元素在这里是可点击的。您应该尝试找到&lt;a&gt; 元素并执行click() 操作,如下所示:-

  • 使用By.cssSelector() :-

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

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

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

如果您仍然无法执行点击,请尝试使用JavascriptExecutor 作为替代解决方案,如下所示:-

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

【讨论】:

  • 我都试过了,还是不行。这里的好奇是……它也不例外……我猜……是点击文本而不是真正的按钮。
  • 但在提供的 HTML 中,它清楚地看起来点击事件正在由 a 元素接收,因为 JavaScript 函数 verifySfdcConnection() 在点击事件上调用 a 元素。
  • 尝试使用JavascriptExecutor 以((JavascriptExecutor)driver).executeScript("arguments[0].click()", driver.findElement(By.cssSelector("div#button-verify-wrapper &gt; a"))); 执行点击,然后告诉我..
  • 它看起来是复制粘贴问题,尝试手动输入相同的内容并执行单击并告诉我..
【解决方案3】:

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

注意:1.如果直接给出ID或类名不需要使用xpath,可以直接使用By.id和By.class 2. 要对 web 元素执行操作,我们应该知道该元素的属性,例如。如果它不是一个按钮,你不能执行点击它

【解决方案4】:

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

【讨论】:

  • 试过这个:driver.findElement(By.cssSelector("#button-verify-wrapper > a")).click();没用。
  • 元素是否在 IFrame 中?如果是,那将不起作用,您需要切换到 IFrame
  • 我已经用“JavascriptExecutor”解决了这个问题,谢谢:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-03-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-12-27
相关资源
最近更新 更多