加载ChromeOptions后,Selenium Click()无效

时间:2016-09-07 11:02:22

标签: c# selenium selenium-chromedriver

我正在学习硒的过程,所以这是我的代码(我已经用Google搜索了好几个小时,这是我第一次找不到任何问题的答案所以这个是我第一个被问到的问题)。它启动Chrome浏览器转到www.google.hr类型,然后在搜索栏中输入,然后在WebPageObject中保存自然搜索结果和广告搜索结果,然后单击下一页按钮,然后重复该过程。问题是如果我实例化driver = new ChromeDriver();但不是在这种情况下driver = new ChromeDriver(options);.因此,对于构造函数中的选项,它会转到第一个搜索结果页面并将同一页面保存3次。在调试模式中,我观察并在WPO中有IWebElement NextPageButton,其中包含正确的URL和所有内容。

public static IWebDriver driver;
List<WebPageObject> WPOList = new List<WebPageObject>();
private void Form1_Load(object sender, EventArgs e)
{
    ChromeOptions options = new ChromeOptions();
    options.AddArguments("user-data-dir=C:/Users/Smirglpapir/AppData/Local/Google/Chrome/User Data");
    options.AddArguments("--start-maximized");
    driver = new ChromeDriver(options);
    driver.Navigate().GoToUrl("http://www.google.hr");
    IWebElement seacrhBarElement = driver.FindElement(By.Name("q"));
    seacrhBarElement.SendKeys("What is the purpouse of life");
    seacrhBarElement.SendKeys(OpenQA.Selenium.Keys.Enter);
    for (int i = 0; i < 3; i++)
    {
        Thread.Sleep(4000); // this fixes million errors
        WebPageObject WPO = new WebPageObject(); // Saves 
        WPOList.Add(WPO);
        Thread.Sleep(500);
        WPO.nextButton.Last().Click(); // last because it clicks the previous button . 
    }

    class WebPageObject
    {
        public string URL { get; set; }
        public List<IWebElement> nextButton { get; set; }
        public List<string> organicSearchResults { get; set; }
        public List<string> stText { get; set; }

        private List<IWebElement> organicSRElementList = new List<IWebElement>(); 
        public WebPageObject()
        {
            //gets the next button
            nextButton = Form1.driver.FindElements(By.Id("pnnext")).ToList();
            // saves the href
            URL = nextButton.Last().GetAttribute("href");
            // saves the IWebelements of organic seacrh results
            organicSRElementList = Form1.driver.FindElements(By.ClassName("rc")).ToList();
            // stores searchresults in organicSearchResults property (List<string>)
            SaveSearchResults();            
        }

        private void SaveSearchResults()
        {
            stText = new List<string>();
            organicSearchResults = new List<string>();
            foreach (IWebElement IwebEl in organicSRElementList)
            {
                organicSearchResults.Add(IwebEl.Text);
                stText.Add(IwebEl.FindElement(By.ClassName("st")).Text);
            }
        }
    }
}

也许我过于复杂化了这个问题,但这段代码也没有成功;

private void Form1_Load(object sender, EventArgs e)
{
    ChromeOptions options = new ChromeOptions();
    options.AddArguments("--user-data-dir=C:\\Users\\Smirglpapir\\AppData\\Local\\Google\\Chrome\\User Data");
    IWebDriver driver = new ChromeDriver(options);
    driver.Navigate().GoToUrl("http://www.google.hr");
    IWebElement seacrhBarElement = driver.FindElement(By.Name("q"));
    seacrhBarElement.SendKeys("What is the purpouse of life");
    seacrhBarElement.SendKeys(OpenQA.Selenium.Keys.Enter);

    Thread.Sleep(4000);
    var nextButton = driver.FindElements(By.Id("pnnext"));
    nextButton.Last().Click();
    Thread.Sleep(4000);
    nextButton = driver.FindElements(By.Id("pnnext"));
    Thread.Sleep(4000);
    nextButton.Last().Click();
}

它停留在同一页面上

1 个答案:

答案 0 :(得分:0)

我找到了一个解决方案:而不是

options.AddArguments("--user-data-dir=C:\\Users\\Smirglpapir\\AppData\\Local\\Google\\Chrome\\User Data");

您只需在用户数据之后添加例如John_Doe,如下所示:

options.AddArguments("--user-data-dir=C:\\Users\\Smirglpapir\\AppData\\Local\\Google\\Chrome\\User Data\\John_Doe");

并且chrome将在该loaction中自动为您创建配置文件。为什么它不能使用默认配置文件我真的不知道。