C#Geckodriver / Selenium 3.0 - Mozilla 47.0.1自行停止

时间:2017-01-03 15:14:25

标签: c# selenium selenium-webdriver geckodriver

您好我使用Selenium Web Driver和Geckodriver在Mozilla上自动执行任务。 当Geckodriver推出一个Mozilla实例时,没有问题:

  • 帐户登录
  • 点击按钮
  • 导入文件(之后出现问题)。
  • 下一个代码未编译。

问题:Mozilla关闭后,Geckodriver重新启动Mozilla并执行相同的过程(帐户登录,点击按钮......),就像一个无限循环。

我想纠正它,任何帮助将不胜感激。谢谢!

FirefoxProfile fox = new FirefoxProfile();
fox.SetPreference("browser.helperApps.neverAsk.saveToDisk", "application/octet-stream;application/csv;text/csv;application/vnd.ms-excel;");
fox.SetPreference("browser.helperApps.alwaysAsk.force", false);
fox.SetPreference("browser.download.folderList", 2);
fox.SetPreference("browser.download.dir", temp);
using (var driver = new FirefoxDriver(new FirefoxBinary(@"C:\Mozilla Firefox\firefox.exe"), fox))
            {
               driver.Manage().Window.Maximize();
               driver.Navigate().GoToUrl("//");
               //Click on buttons without problems
               System.Threading.Thread.Sleep(5000);
               //Import file
               var inputUpload = driver.FindElementById("uploadedPrevisionsFileId-button");
               inputUpload.Click();
               //I put Thread.Sleep to wait until the element appears
               System.Threading.Thread.Sleep(3000);
               SendKeys.Send("C:/Test.xls");
               SendKeys.Send("{ENTER}");
               //HERE, Mozilla stops but I can see the file has been imported
               //Code ...
             }
//And it restarts a new instance of Mozilla which does the same thing

1 个答案:

答案 0 :(得分:0)

我对您的上传代码感到困惑。您是否已在其他浏览器中成功使用此策略?这是通过类似于此的浏览器窗口上传文件吗? enter image description here

如果是这样,selenium只需在upload元素上调用SendKeys()即可处理文件IO。 inputUpload.SendKey("C:/Test.xls");

我不确定导致浏览器关闭的原因,但我是否可以建议使用NUnit等测试框架?这样你可以避免使用using块并让框架管理testSetup和tearDown。