通过单击selenium webdriver上的“x”关闭图标来关闭框架时出错

时间:2015-06-16 01:23:20

标签: selenium-webdriver

我正在努力点击框架上显示的“x”关闭图标。我能够在帧上执行所有其他操作,但是一旦我将数据保存在帧上,我需要通过帧顶部的“x”关闭图标来关闭帧。下面是框架中的html代码以及我尝试编写的代码,用于关闭失败并出现错误的框架

Error
====
Exception in thread "main" org.openqa.selenium.InvalidSelectorException: The given selector //*[contains(concat(' ', normalize-space(@class), ' '),' x-tool-close '] is either invalid or does not result in a WebElement. The following error occurred:
InvalidSelectorError: Unable to locate an element with the xpath expression //*[contains(concat(' ', normalize-space(@class), ' '),' x-tool-close '] because of the following error:
SyntaxError: The expression is not a legal expression.

HTML Code
=========
<div class="x-window-tc">
<div id="ext-gen180" class="x-window-header x-unselectable">
<div id="ext-gen185" class="x-tool x-tool-close"/>
<span class="x-window-header-text"/>
</div>
</div>

Webdriver Code
===========

    Driver.switchTo().frame(0); // works
    Driver.findElement(By.id("serviceButton_bottom")).click(); //// works
    Driver.findElement(By.xpath("//*[contains(concat(' ', normalize-space(@class), ' '),' x-tool-close ']")); //Fails

Selenium finds the close icon with above mentioned xpath

2 个答案:

答案 0 :(得分:0)

 Driver.findElement(By.className('x-tool-close')).click()

答案 1 :(得分:0)

我也是同一个问题.. 有时iframe关闭按钮会在主DOM中有一个引用。 尝试切换回主窗口并关闭。

    driver.switchTo().defaultContent();
//then write your code to close that iframe