简体   繁体   中英

.NET Tests with Selenium, headless Chrome error

I've been trying to implement headless browser to my tests and I'm getting an error message that shows me this: "Unable to locate element: {"method":"id","selector":"my_id"}". This is the code that I'm working with:

[TestFixture]
class ClientesSystemTest
{
    private ChromeOptions options;
    private NewClientesPage page;
    private IWebDriver driver; 
    public ClientesSystemTest()
    {
        options = new ChromeOptions();
        options.AddArgument("--headless");
        driver = new ChromeDriver(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), options);
        page = new NewClientesPage(driver);
    }

    [Test]
    public void CadastraCliente()
    {
        page.Visita();
        page.Cadastra("Paulo", "Guedes", "00870021087", "Rua abcdwxyz, 14", 15);
        driver.Close();
    }
}

and this is the Cadastra method.:

public void Cadastra(string nome, string sobrenome, string cpf, string endereco, int idade)
    {
        IWebElement nomeCliente = driver.FindElement(By.Id("Nome"));
        IWebElement sobrenomeCliente = driver.FindElement(By.Id("Sobrenome"));
        IWebElement cpfCliente = driver.FindElement(By.Id("CPF"));
        IWebElement enderecoCliente = driver.FindElement(By.Id("Endereco"));
        IWebElement idadeCliente = driver.FindElement(By.Id("Idade"));
        IWebElement estadoCivilCliente = driver.FindElement(By.Name("EstadoCivil"));
        driver.FindElement(By.CssSelector("[value=Divorciado]")).Click();

        nomeCliente.SendKeys(nome);
        sobrenomeCliente.SendKeys(sobrenome);
        cpfCliente.SendKeys(cpf);
        enderecoCliente.SendKeys(endereco);
        idadeCliente.SendKeys(idade.ToString());
        nomeCliente.Submit();
    }

I've tried everything by this point. The test runs normally without the headless feature. Does anyone have a solution for this error? Thanks.

My guess would be that your site shows different elements depending on the browser resolution. Typically the headless browser is a smaller setting so I'd make sure it's set to the same size you use when you test non-headless.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM