2016-02-26 125 views
0

我想在我所有运行的测试结束后删除我的所有历史记录,Cookie,缓存,所有内容。我可以进入Chrome的历史很好,但由于某些原因,硒无法通过ID找到元素。我甚至尝试使用SendKeys选项卡通过几次,然后按回车键,但这也不起作用。使用Selenium WebDriver无法点击Chrome历史记录中的任何按钮?

Driver.Keyboard.SendKeys($"{Keys.Control + "h"}"); 
Thread.Sleep(5000); 
Driver.FindElementById("clear-browsing-data").Click(); 
var checkboxDiv = Driver.FindElementById("clear-data-checkboxes"); 
List<IWebElement> checkboxes = checkboxDiv.FindElements(By.TagName("input")).ToList(); 
foreach (IWebElement checkbox in checkboxes) 
{ 
    if (!checkbox.Selected) 
    { 
     checkbox.Click(); 
     Thread.Sleep(1000); 
    } 
} 

(是的,一些检查,看看是否复选框被选中的检查的(哇很多的检查)可能需要一些提炼,但以后将解决) 我甚至不能简单地点击clear-browsing-data。任何想法为什么?或者我试图完成什么替代方案?提前致谢。

+0

是你的网址'chrome:// history-frame'?如果您使用该网址,则不存在iframe。 – djangofan

回答

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]); 
+0

我得到NoSuchFrameException。 “没有找到具有名称或ID历史记录的框架元素”。尝试使用索引也没有运气。 –

+0

@DannyGoodall你在历史页面打开后试过了吗? – Guy

+0

@DannyGoodall您也可以尝试Driver.SwitchTo()。Frame(Driver.FindElementByTagName(“iframe”)); – Guy

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,然后切换到设置框并勾选/取消勾选复选框。

我希望这有助于!