无法使用Selenium WebDriver点击Chrome历史记录中的任何按钮?

时间:2016-02-26 13:39:22

标签: c# google-chrome selenium selenium-webdriver

我想在所有运行的测试结束后删除所有历史记录,Cookie,缓存,所有内容。我可以很好地进入Chrome的历史,但由于某些原因,selenium无法通过ID找到该元素。我甚至尝试使用//Before: $('.options.selected').parent().prev('.line'); //After: $('.options.selected').parent().next('.line'); 标签几次并按Enter键,但这也不起作用。

//Before:
$('.options.selected').prev('.line');

//After:
$('.options.selected').next('.line');

(是的,检查一下检查复选框是否被检查(哇大量检查)的一些检查可能需要一些改进,但稍后会解决) 我甚至无法点击SendKeys。有什么想法吗?或者我想要完成的任何替代方案?提前谢谢。

2 个答案:

答案 0 :(得分:1)

复选框和clear-browsing-data按钮位于iframe内。你需要先切换到它。

此外,Driver.Keyboard.SendKeys($"{Keys.Control + "h"}");会打开新标签,您需要切换到该标签以找到该框架。

// switch to the new tab
Driver.SwitchTo().Window(Driver.WindowHandles[1]);

// switch to the frame
Driver.SwitchTo().Frame("history"); // history is the frame name attribute, you can also use id attribute, WebElement or index

并切换回来

// get out of the frame
Driver.SwitchTo().DefaultContent();

// switch back to the website tab
Driver.SwitchTo().Window(Driver.WindowHandles[0]);

答案 1 :(得分:1)

[Test]
    public void ClearChromeHistory()
    {
        //Navigate to History
        driver.Navigate().GoToUrl("chrome://history/");

        //Switch to IFrame
        driver.SwitchTo().Frame("history");

        Thread.Sleep(5000); //Static wait is not recommended

        //Click on 'Clear Browsing Data' Button
        IWebElement clearBrowsingDataButton =  driver.FindElement(By.Id("clear-browsing-data"));
        clearBrowsingDataButton.Click();

        Thread.Sleep(2000); //Static wait is not recommended

        //Swich to Settings IFrame on 'chrome://settings/clearBrowserData' URL
        driver.SwitchTo().Frame("settings");

        Thread.Sleep(5000); //Static wait is not recommended

        //Click on 'Browsing History' checkbox
        IWebElement ClearHistoryCheckbox = driver.FindElement(By.Id("delete-browsing-history-checkbox"));
        ClearHistoryCheckbox.Click();
    }

这对我来说非常合适。

您可以跳过点击“清除浏览数据”按钮,您可以直接导航到chrome://settings/clearBrowserData,然后切换到设置框并勾选/取消勾选复选框。

我希望这有帮助!