简体   繁体   English

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

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

I am having trouble figuring out how to open a link in a new tab using selenium Webdriver.我无法弄清楚如何使用 selenium Webdriver 在新选项卡中打开链接。 I am getting stale exceptions in the loops because the pages are not correct after the first iteration.我在循环中遇到陈旧的异常,因为第一次迭代后页面不正确。 So my idea is to open the link in a new tab, do all the operations I want to do on that tab, and switch back to the old tab to continue the loop, but I am not too sure how to open these tabs and manage them.所以我的想法是在新选项卡中打开链接,在该选项卡上执行我想做的所有操作,然后切换回旧选项卡继续循环,但我不太确定如何打开这些选项卡并进行管理他们。

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

        }

You can perform a click while holding the control key to force open the link in a new tab.您可以在按住 control 键的同时单击以在新选项卡中强制打开链接。 You can use actions API for the same.您可以使用相同的操作 API。 Actions action = new Actions(webDriver); Actions action = new Actions(webDriver);

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

However, I believe you might still get a stale reference exception when you come back to tab 0 and continue looping over results collection.If this happens to be the case, you can retrieve the count first and convert your foreach loop to a while/for loop and lookup your results collection every time inside while/for loop and then use results[i] to process that element further.但是,我相信当您返回选项卡 0 并继续循环遍历结果集合时,您可能仍然会遇到陈旧的引用异常。如果发生这种情况,您可以先检索计数并将 foreach 循环转换为 while/for每次在 while/for 循环中循环并查找结果集合,然后使用 results[i] 进一步处理该元素。 Another option could be to wrap your loop in a retry block eg using Polly framework and lookup results collection again in case of stale reference and retry the entire thing.另一种选择可能是将您的循环包装在重试块中,例如使用 Polly 框架并再次查找结果集合,以防引用过时并重试整个过程。

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

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