如何使用Selenium打开下载的附件?

时间:2019-03-13 19:18:10

标签: c# selenium selenium-webdriver

我正在尝试使用以下代码使用Selenium打开上次下载的项目;

                IJavaScriptExecutor executor = (IJavaScriptExecutor)WebDriver;
                executor.ExecuteScript("window.open();");
                WebDriver.SwitchTo().Window(WebDriver.WindowHandles[1]);

                WebDriver.Navigate().GoToUrl("chrome://downloads");
                WebDriver.SwitchTo().Window(WebDriver.WindowHandles.Last());

                //Open up the attachment, close the downloads page, switch driver focus to attachment page,
                //gets the text from the attachment page, closes attachment page,
                try
                {
                    IWebElement manager = WebDriver.FindElement(By.CssSelector("body/deep/downloads-manager"));
                    IWebElement item = manager.FindElement(By.CssSelector("body/deep/downloads-item"));
                    IWebElement shadow = (IWebElement)executor.ExecuteScript("return arguments[0].shadowRoot;", item);
                    IWebElement link = shadow.FindElement(By.CssSelector("div#title-area>a"));
                    link.Click();

我切换到正确的帧,但是我的item无法正确加载并引发异常。有人可以告诉我为什么会这样吗?

0 个答案:

没有答案