首页 > 解决方案 > 在 selenium 的活动窗口中未单击 X 图标

问题描述

以下代码不起作用:

List<WebElement> close=driver.findElements(By.xpath(".//a[@aria-label='Close' and @role='button' and @class='k-button k-bare k-button-icon k-window-action']"));
close.get(4).click();

HTML 代码:

<div class="k-window-titlebar k-header" style="margin-top: -28px;"
    <span id="relatedNumberWindow_wnd_title"class="k-window-title" Related   Numbers
    </span>
         <div class="k-window-actions" >
             <a class="k-button k-bare k-button-icon k-window-action" role="button" aria-label="Close" </a>
         </div>
</div>

我想点击 X 图标(在上面没有写代码的最小化和最大化中,否则它会太长)来关闭我的活动窗口。

我怎样才能完成这项任务?

标签: javaseleniumxpathcss-selectorswebdriver

解决方案


要单击X图标,您可以使用以下任一解决方案:

  • cssSelector

    driver.findElement(By.cssSelector("div.k-window-titlebar.k-header a.k-button.k-bare.k-button-icon.k-window-action[aria-label='Close']")).click();
    
  • xpath

    driver.findElements(By.xpath("//div[@class='k-window-titlebar k-header']//a[@class='k-button k-bare k-button-icon k-window-action' and @aria-label='Close']")).click();
    

推荐阅读