在Selenium C中获取无头Chrome驱动程序错误#

时间:2018-02-14 18:15:18

标签: c# selenium selenium-webdriver selenium-chromedriver headless-browser

我在Selenium C#中使用Headless chrome驱动程序时遇到错误。我正在做自动化网站,涉及用户名和密码。没有Headless chrome,这样做很好。但是当我使用无头镀铬时。它主要给我错误

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

current-portfolio-collapsible> div> div> div>表> tbody> tr:nth-​​child(6)> td:nth-​​child(1)> div> a>跨度\“} \ n

(会话信息:无头铬= 64.0.3282.167 .. enter code here

我在Headless chrome中遇到此错误。以下是代码。

 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)
                    {
                    }


            }

这是网站。

https://www.wikifolio.com/

在这里,您可以找到右上角的“登录”按钮。

请在这方面帮助我。帮助将受到高度赞赏。感谢

1 个答案:

答案 0 :(得分:1)

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)")));

希望这有帮助!我没有问题地运行它。告诉我,如果这有助于你