如何使用Selenium(Java)自动处理具有较高Z索引的div中的元素的点击

时间:2018-11-16 09:44:57

标签: java selenium xpath css-selectors z-index

我有一个具有较高z索引的div,它显示为模式,如何使硒与该模式中的按钮交互。

CSS:

.buX {
    background-color: #fff;
    display: flex;
    flex-direction: column;
    outline: none;
    padding: 0;
    position: absolute;
    width: 504px;
    z-index: 501;
    height: 556px;
    max-height: 100%;
    max-width: 100%;
    overflow: auto;
    width: 580px;
}

HTML:

<div class="buX" tabindex="0" role="dialog" aria-labelledby=":7c" 
  style="left: 115.5px; top: 28.5px;">
    <div class="buX-K7">
      <button name="welcome_dialog_next" class="J-at1-auR">Next</button
    </div>
</div>

我正在使用Chrome Webdriver和Java

1 个答案:

答案 0 :(得分:0)

根据您的问题以及作为元素共享的HTML在 Modal对话框中,以在需要诱导 WebDriverWait 的所需元素上调用click() em>使元素可点击,您可以使用以下任一解决方案:

  • cssSelector

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("div.buX button.J-at1-auR[name='welcome_dialog_next']"))).click();
    
  • xpath

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@class='buX']//button[@class='J-at1-auR' and @name='welcome_dialog_next'][contains(.,'Next')]"))).click();