通过单击Selenium网络驱动程序上的关闭图标关闭框架时出错"x"



我正在努力单击框架上显示的"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
 Driver.findElement(By.className('x-tool-close')).click()

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

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

最新更新