简体   繁体   English

在 Selenium C# 中出现 Headless Chrome 驱动程序错误

[英]Getting Error in Headless Chrome driver in Selenium C#

I am getting error using Headless chrome driver in Selenium C# .我在 Selenium C# 中使用 Headless chrome 驱动程序时出错。 I am doing automation the website which involve the User Name and password.我正在自动化涉及用户名和密码的网站。 It is doing good without Headless chrome.在没有 Headless chrome 的情况下它做得很好。 But when I used the headless chrome .但是当我使用无头 chrome 时。 Its mostly giving me the error它主要是给我错误

{"no such element: Unable to locate element: {\"method\":\"css selector\",\"selector\":\"

current-portfolio-collapsible > div > div > div > table > tbody > tr:nth-child(6) > td:nth-child(1) > div > a > span\\"}\\n current-portfolio-collapsible > div > div > div > table > tbody > tr:nth-child(6) > td:nth-child(1) > div > a > span\\"}\\n

(Session info: headless chrome=64.0.3282.167.. enter code here (会话信息:headless chrome=64.0.3282.167.. enter code here

I am getting this error in Headless chrome only.我仅在 Headless chrome 中收到此错误。 Below is the Code .下面是代码。

 var driverService = ChromeDriverService.CreateDefaultService();
            driverService.HideCommandPromptWindow = true;
               ChromeOptions option = new ChromeOptions();
                option.AddArgument("--headless");
                option.AddArgument("--window-size=2160,3840");
            using (var driver = new ChromeDriver(driverService, option))
            {
                WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(30));
                driver.Manage().Timeouts().PageLoad = TimeSpan.FromMinutes(2);
                driver.Navigate().GoToUrl("https://www.wikifolio.com/");
                label1.Text = "Browser is opened now";                
                    try
                    {

                        driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromMinutes(2);
                        IJavaScriptExecutor main = (IJavaScriptExecutor)driver;
                        Task.Delay(10000);

                    main.ExecuteScript("arguments[0].click();", driver.FindElement(By.XPath("//*[@id=\"page-content\"]/header/div[2]/div/div/nav/div[2]/a[1]")));

                    driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(20);
                        var username = driver.FindElement(By.CssSelector("input#Username"));
                        username.SendKeys("derhenker55@hotmail.com");

                        driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(20);

                        var password = driver.FindElement(By.CssSelector("input#Password"));
                        Task.Delay(1000);
                        password.SendKeys("password");
                        driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(20);

                        IJavaScriptExecutor loginclick = (IJavaScriptExecutor)driver;
                    Task.Delay(1000);
                    loginclick.ExecuteScript("arguments[0].click();", driver.FindElement(By.CssSelector("form > button.c-button")));

                    }
                    catch(Exception e)
                    {
                    }


            }

And this is the website.这是网站。

https://www.wikifolio.com/ https://www.wikifolio.com/

Here you can find the top right corner "Login" Button.在这里您可以找到右上角的“登录”按钮。

Please help me in this regard.请在这方面帮助我。 Help will be highly appreciated.帮助将不胜感激。 Thanks谢谢

var service = ChromeDriverService.CreateDefaultService();
        service.HideCommandPromptWindow = true;
        ChromeOptions option = new ChromeOptions();
        option.AddArgument("--headless");

        driver = new ChromeDriver(service, option);
        wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
        driver.Navigate().GoToUrl("https://www.wikifolio.com/");
        wait.Until(ExpectedConditions.ElementIsVisible(By.XPath("/html/body/div[2]/div[1]")));
        Actions actions = new Actions(driver);
        actions.MoveToElement(driver.FindElement(By.XPath("/html/body/div[2]/div[1]/div[2]/div/div[1]/div[1]/div/div[3]/a[3]"))).Build().Perform();

        Thread.Sleep(2000);
        actions.Click(driver.FindElement(By.CssSelector("#change-language-dialog-container > div > div > div > div > div.c-disclaimer__buttons > div"))).Build().Perform();
        Thread.Sleep(2000);
        driver.FindElement(By.XPath("/html/body/div[2]/header/div[2]/div/div/nav/div[2]/a[1]")).Click();
        wait.Until(ExpectedConditions.ElementToBeClickable(By.XPath("//*[@id='Username']")));
        var username = driver.FindElement(By.XPath("//*[@id='Username']"));
        var password = driver.FindElement(By.XPath("//*[@id='Password']"));

        username.Clear();
        username.SendKeys("username");
        password.Clear();
        password.SendKeys("pass");

        IJavaScriptExecutor executor = (IJavaScriptExecutor)driver;
        executor.ExecuteScript("arguments[0].click();", driver.FindElement(By.CssSelector("button.c-button:nth-child(8)")));

Hope this helps!希望这可以帮助! I run it without problems.. Tell me if that helped you我运行它没有问题..告诉我这是否对你有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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