簡體   English   中英

.NET 使用 Selenium 進行測試,無頭 Chrome 錯誤

[英].NET Tests with Selenium, headless Chrome error

我一直在嘗試在我的測試中實現無頭瀏覽器,但收到一條錯誤消息,向我顯示:“無法定位元素:{“方法”:“id”,“選擇器”:“my_id”}”。 這是我正在使用的代碼:

[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();
    }
}

這是地籍法。:

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();
    }

到目前為止,我已經嘗試了一切。 測試正常運行,沒有無頭功能。 有沒有人有這個錯誤的解決方案? 謝謝。

我的猜測是您的網站會根據瀏覽器分辨率顯示不同的元素。 通常,無頭瀏覽器的設置較小,因此我會確保將其設置為與測試非無頭瀏覽器時使用的大小相同。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM