ChromeDriver - 尝试使用javascript

时间:2017-11-17 21:04:02

标签: javascript c# google-chrome selenium selenium-chromedriver

我需要将页面保存为PDF,但我遇到了问题。我正在使用ChromeDriver并仅使用谷歌作为示例(因为我的应用需要登录)我导航到该页面(google.com)。然后在javascript中执行window.print()以获取打印窗口。 Chrome打开了打印预览页面,我得到了iframe的src,显示了pdf,因此我可以从网址中提取pdf。

我的问题是,在执行javascript时,它会打开打印预览,但执行会在那里停止。这就像ChromeDriver正在等待页面说它已经完成但消息永远不会出现所以我收到一个WebDriverException,说它超时了。

奇怪的是,直到昨天我收到了大量的Windows更新并更新了包括Chrome在内的一系列程序。

所以有人知道如何重新开始这项工作吗?

我正在使用Google Chrome版本62.0.3202.94(官方版)(64位) 操作系统是Windows 10 ChromeDriver版本为2.33.0 Selenium WebDriver是3.7.0

这是错误:

  

OpenQA.Selenium.WebDriverException:对URL http://localhost:12277/session/f8ac647c64a7e61a5d84e4ed9daa8c7c/execute的远程WebDriver服务器的HTTP请求在60秒后超时。 ---> System.Net.WebException:请求已中止:操作已超时。      在System.Net.HttpWebRequest.GetResponse()      在OpenQA.Selenium.Remote.HttpCommandExecutor.CreateResponse(WebRequest请求)      ---内部异常堆栈跟踪结束---      在OpenQA.Selenium.Remote.HttpCommandExecutor.CreateResponse(WebRequest请求)      在OpenQA.Selenium.Remote.HttpCommandExecutor.Execute(命令commandToExecute)      在OpenQA.Selenium.Remote.DriverServiceCommandExecutor.Execute(命令commandToExecute)      在OpenQA.Selenium.Remote.RemoteWebDriver.Execute(String driverCommandToExecute,Dictionary`2参数)      在OpenQA.Selenium.Remote.RemoteWebDriver.ExecuteScriptCommand(String script,String commandName,Object [] args)      在OpenQA.Selenium.Remote.RemoteWebDriver.ExecuteScript(String script,Object [] args)      在Err.Program.Main(String [] args)中的D:\ Temp \ Err \ Err \ Program.cs:第34行

以下是代码:

        static void Main(string[] args)
    {
        IWebDriver driver = new ChromeDriver();
        try
        {

            driver.Navigate().GoToUrl("http://www.google.com");
            IJavaScriptExecutor js = (IJavaScriptExecutor)driver;
            js.ExecuteScript("window.print();"); //this is the line that throws the error
        }
        catch (WebDriverException ex)
        {
            //just eat the error here since it's done opening the pdf viewer
        }


        //cannot execute the line below because the driver is dead in the water at this point - anything I try to do with it just times out...
        var q = driver.FindElement(By.XPath("descendant::input[@class = 'q']"));
    }

0 个答案:

没有答案