我开发了一个小型应用程序(在C#中),它可以在一个使用Selenium的网站上实现测试自动化。一切都进行得很顺利。但是当我用“无头”浏览器尝试同一个应用程序时,测试就不起作用了。下面的代码我有一个问题:
var emailTextBox = driver.FindElement(By.Id("j_username"));OpenQA.Selenium.WebDriverException :‘对远程WebDriver服务器的http://localhost:49309/session/d4416c4b-e674-468b-8d6e-6a8bfc9bdf1d/element请求在60秒后超时。’
同样的测试适用于普通浏览器,但不适用于无头模式,我尝试使用火狐、Chrome、PhantomJS (都是在无头浏览器中),但它不起作用.
你有什么主意吗?
我的全部代码是:
“”“
using System;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
namespace MacDo
{
class Program
{
static void Main(string[] args)
{
var driverService = FirefoxDriverService.CreateDefaultService();
driverService.HideCommandPromptWindow = true;
var options = new FirefoxOptions();
options.AddArguments("-headless");
IWebDriver driver = new FirefoxDriver(driverService, options);
driver.Url = "https://www.mcdonalds.fr/";
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10000);
System.Threading.Thread.Sleep(2000);
var seConnecter = driver.FindElement(By.Id("seconnecter"));
seConnecter.Click();
driver.Close();
driver.Quit();
Console.ReadKey();
}
}
}“”“
我使用Firefox浏览器73.0.1 (32位) (->我使用Geckodriver0.26.0版)
就像前面说的,它很好,但不是在无头模式下.
发布于 2020-03-09 12:43:40
我以前也遇到过同样的问题,脚本没有在无头状态下工作,我发现这是默认解决方案造成的。您可以尝试添加
options.addArguments("window-size=1920,1080");值得一试!
https://stackoverflow.com/questions/60533712
复制相似问题