【问题标题】:Not able to find element of a web application in selenium webdriver无法在 selenium webdriver 中找到 Web 应用程序的元素
【发布时间】:2017-08-18 14:57:39
【问题描述】:

我正在尝试使用 selenium webdriver 单击按钮

driver.findElement(By.xpath(".//*[@id='addLog']")).click();

但我得到一个错误

org.openqa.selenium.WebDriverException: unknown error: Element <button id="addLog" type="button" class="btn btn-awh1" onclick="addLog()">...</button> is not clickable at point (516, 209). Other element would receive the click: <div id="BBOverlay" style="opacity: 0.216303; width: 1034px; height: 506px;"></div>

HTML:

<!-- Operation Button -->
<div class="row">
    <div class="col-md-12">
        <div class="form-group">
            <div class="col-md-4" />
            <div class="col-md-4" style="text-align: center;">
                <div class="btn-group">
                    <button id="addLog" class="btn btn-awh1" type="button"     onclick="addLog()">
                    Create My Health Log
                      <i class="fa fa-plus" aria-hidden="true"/>
                    </button>
                </div>
            </div>
            <div class="col-md-4" />
        </div>
    </div>
    <!-- End Of Operation Button -->
</div>

这个操作按钮之前的代码是

  <!-- Nav tabs -->
    <ul class="nav nav-tabs" role="tablist">
    <li class="active" role="presentation">
    <a href="#log" aria-controls="log" role="tab" data-toggle="tab">My Health Log</a>
    </li>
    <li role="presentation">
    <a href="#details" aria-controls="details" role="tab" data-toggle="tab">My Health Details</a>
    </li>
    <li role="presentation">
    <li role="presentation">
    </ul>
    <!-- Tab panes -->
    <div class="tab-content" style="margin-top:25px">
    <!-- My Health Log -->
    <div id="log" class="tab-pane active center" role="tabpanel">

感谢任何帮助。

【问题讨论】:

  • 它清楚地表明你在那个按钮上有一个覆盖。你有没有像 blockUI 这样覆盖整个页面的加载动画,也许正在等待休息响应?
  • 请发布该页面的链接。

标签: java html selenium-webdriver


【解决方案1】:

如果您阅读了错误消息,则表明有一个元素覆盖了您尝试单击的元素。我不确定元素是什么

<div id="BBOverlay" style="opacity: 0.216303; width: 1034px; height: 506px;"></div>

但从 id 来看,它看起来像是某种叠加层。我会等待该元素不可见,然后单击所需的按钮。鉴于您提供的信息,我无法确定这是否有效,因此您必须尝试并告诉我。

new WebDriverWait(driver, 10).until(ExpectedConditions.invisibilityOfElementLocated(By.id("BBOverlay")));
driver.findElement(By.id("addLog")).click();

【讨论】:

  • 我试过了,还是不能点击按钮,而是点击另一个标签
  • 你要点击的那个元素是不是在页面底部需要滚动?
  • 您确定页面上没有两个具有相同 ID 的元素吗?该 ID 与您提供的 ID 匹配,因此不应单击其他随机元素。请用您当前正在尝试的内容更新您问题中的代码(不仅仅是一行)。该页面的链接可以解决问题。
【解决方案2】:

试试下面的xpath,如果它适合你:

driver.findElement(By.xpath("//button[contains(text(),'Create My Health Log')]")).click();

driver.findElement(By.xpath("//button[@class='btn btn-awh1']")).click();

driver.findElement(By.xpath("//button[@id='addLog'][@class='btn btn-awh1']")).click();

driver.findElement(By.cssSelector(".btn-group #addLog")).click();

driver.findElement(By.cssSelector(".row .form-group .col-md-4 .btn-group #addLog")).click();

我猜该元素在页面下方,您需要滚动查看它: 我猜这会起作用

WebElement elements = driver.findElement(By.xpath("//button[@class='btn btn-awh1']"))   
Thread.sleep(3000L);
JavascriptExecutor js = (JavascriptExecutor) driver;
int yPosition = elements.getLocation().getY();
js.executeScript("window.scroll (0, " + yPosition + ") ");
Thread.sleep(3000L);
driver.findElement(By.xpath("//button[@class='btn btn-awh1']")).click();

让我知道它们是否适合你。

【讨论】:

  • 我都试过了,对我不起作用,仍然是同样的错误信息。
  • 谢谢!现在工作正常。
  • 不要在 selenium 中使用 thread.sleep!它会使您的测试变慢且不确定。如果您有叠加层,请等到该叠加层不可见。见stackoverflow.com/a/29084080/143585
  • 有很多waits可以使用。我只是告诉硬等待只是为了检查元素是否可见。
  • @KishanPatel 刚刚意识到有一些错误。此代码实际上是单击另一个选项卡而不是按钮,不知道为什么。
猜你喜欢
  • 1970-01-01
  • 2023-04-01
  • 2015-05-04
  • 2014-05-29
  • 1970-01-01
  • 2014-07-13
  • 1970-01-01
  • 1970-01-01
  • 2018-03-31
相关资源
最近更新 更多