【问题标题】:How to click a link and open it in a new tab Selenium WebDriver C#如何单击链接并在新选项卡中打开它 Selenium WebDriver C#
【发布时间】:2021-12-21 12:48:12
【问题描述】:

我无法弄清楚如何使用 selenium Webdriver 在新选项卡中打开链接。我在循环中遇到过时的异常,因为第一次迭代后页面不正确。所以我的想法是在新选项卡中打开链接,在该选项卡上做我想做的所有操作,然后切换回旧选项卡继续循环,但我不太清楚如何打开这些选项卡和管理他们。

        string year = this.yearTextBox2.Text;
        string semester = this.semesterTextBox2.Text;
        int numCourses = (int)this.numEnrollments.Value;
        int count = 0;

        string URL = GetURL(year, semester, "index");

        _driver.Navigate().GoToUrl(URL);

        //var result = _driver.FindElement(By.XPath("//*[@id=\"uu-skip-target\"]/div[2]/div"));
        var results = _driver.FindElements(By.CssSelector(".btn.btn-light.btn-block"));

        // Loop through each department
        foreach (var r in results)
        {
            // Make sure not to include the letter link
            // Click on this department and get the list of all courses

            r.Click();
            var result2 = _driver.FindElement(By.Id("class-details"));
            var results2 = result2.FindElements(By.XPath("./*[@class=\"class-info card mt-3\"]"));

            var courseCount = 0;

            // Loop through each course in the department
            foreach (var r2 in results2)
            {
                // Stop the process once reached the amount of courses needed to be scraped
                if (count >= numCourses)
                    break;

                Course c = new Course();
                c.year = year;
                c.semester = semester;

                var header = r2.FindElement(By.TagName("h3"));

                if (header != null)
                {
                    // Gets the course (CS 2420)
                    string courseNum = header.Text.Split('-')[0].Trim().ToUpper();
                    string[] depAndNum = courseNum.Split(' ');

                    // Separate department and number
                    c.department = depAndNum[0];
                    c.number = depAndNum[1];

                    // Get the course title
                    string text = header.Text.Split('-')[1].Trim();
                    c.title = text.Substring(4);

                    // Check if the course is a lecuture/seminar, if not then continue.
                    var list = result2.FindElement(By.CssSelector(".row.breadcrumb-list.list-unstyled"));
                    if (CourseIsLecture(list.FindElements(By.TagName("li"))))
                    {
                        c.count = courseCount;
                        GetCourseInformation(r2, c);
                    }
                    else
                    {
                        courseCount++;
                        continue;
                    }
                }

                // Increment the course count on this department page
                courseCount++;
                // Increment total course count
                count++;
            }

        }

【问题讨论】:

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


    【解决方案1】:

    您可以在按住控制键的同时单击以强制在新选项卡中打开链接。您可以使用相同的操作 API。

    Actions action = new Actions(webDriver);
    
    action.KeyDown(Keys.LeftControl).Click(r).KeyUp(Keys.LeftControl).Build().Perform();
    

    但是,我相信当您返回选项卡 0 并继续循环遍历结果集合时,您可能仍然会遇到过时的引用异常。如果发生这种情况,您可以先检索计数并将您的 foreach 循环转换为while/for 循环并每次在 while/for 循环中查找结果集合,然后使用 results[i] 进一步处理该元素。 另一种选择可能是将循环包装在重试块中,例如再次使用 Polly 框架和查找结果集合,以防引用过时并重试整个事情。

    【讨论】:

    • 感谢您的回复!我想我可能必须打开 3 个标签,这可能吗?如果是这样,切换选项卡是否会根据创建进行索引?就像原来的标签是0,第一个新标签是1,然后下一个新标签是2,以此类推。
    猜你喜欢
    • 2019-07-06
    • 1970-01-01
    • 1970-01-01
    • 2016-04-22
    • 1970-01-01
    • 2017-03-18
    • 2016-05-06
    • 2014-05-25
    • 1970-01-01
    相关资源
    最近更新 更多