Home > front end >  How to click a link and open it in a new tab Selenium WebDriver C#
How to click a link and open it in a new tab Selenium WebDriver C#

Time:11-09

I am having trouble figuring out how to open a link in a new tab using 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  ;
            }

        }

CodePudding user response:

You can perform a click while holding the control key to force open the link in a new tab. You can use actions API for the same.

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. Another option could be to wrap your loop in a retry block e.g. using Polly framework and lookup results collection again in case of stale reference and retry the entire thing.

  • Related