如何单击 link 并在新选项卡中打开它 Selenium WebDriver C#

How to click a link and open it in a new tab Selenium WebDriver C#

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

        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++;
            }

        }

您可以在按住 Control 键的同时单击以在新选项卡中强制打开 link。您可以使用操作 API 来达到同样的效果。

Actions action = new Actions(webDriver);

action.KeyDown(Keys.LeftControl).Click(r).KeyUp(Keys.LeftControl).Build().Perform();

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