Unable to navigate between windows in Webdriver using C# and CSharp

I'm facing an issue with switching windows using the latest version of Selenium WebDriver in C#.

The scenario is that I have a base window, and upon clicking a button, it opens a new window.

Here is the code snippet for opening the new window:

window.open(uri, "DisplayPage", " width=1200, scrollbars=yes , resizable = yes ,   toolbar =  no , menubar =  no");

To switch to the new window, I am using the following code:

string BaseWindow = _driver.CurrentWindowHandle;

ReadOnlyCollection<string> handles = _driver.WindowHandles;

foreach (string handle in handles)
{

    if (handle != BaseWindow)
    {
        _driver.SwitchTo().Window(handle).Title.Equals("Display - Transaction Page");

    }
}

Despite modifying the title check in the code, I am still unable to successfully switch to the new window.

Upon further investigation, I realized that the title of the opened window is actually "Display - Transaction Page" instead of "DisplayPage" as I initially assumed.

Any suggestions or ideas on how to resolve this issue would be greatly appreciated.

Best regards,

Hasan

Answer №1

Make sure to stop the loop once the window changes to the desired one, otherwise it will keep switching to the last opened window:

foreach (string handle in handles) {
 if (handle != BaseWindow) {
  if(_driver.SwitchTo().Window(handle).Title.Equals("Display - Transaction Page")) 
    break;
  }
}

You may consider using Contains instead of equal for a simpler window search process:

_driver.SwitchTo().Window(handle).Title.Contains("Display"); 

Answer №2

Even though you managed to figure out the solution on your own, there is an alternative method for handling window switching in C#.

// start webdriver
IWebDriver driver = new FirefoxDriver();

// perform an action to open a new window. For example, clicking a link.
driver.FindElement(By.Id("btnId")).Click();

// switch to the new window.
driver.SwitchTo().Window(driver.WindowHandles.Last());

// if you need to switch back to your original window
driver.SwitchTo().Window(driver.WindowHandles.First());

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Guide to finding a specific element on a website with Selenium and Python 3.6

Currently, I am working on a Python script to automate a process in Selenium. This is my first experience with xpath and I am puzzled as to why the xpath I obtained from Chrome web inspect (F12) is not working. The xpath I copied after clicking on the text ...

Interact with a web page by clicking on a random location using Selenium 2 with Python bindings

My current challenge revolves around figuring out the best method to execute a left-click within a specific area of a web browser window. I am utilizing Selenium version selenium-2.44.0 along with Python 2.7 for this task. While my end goal is to be able t ...

Enhance the speed of Selenium testing by implementing these proven methods

While attempting to gather information from a website that hides full telephone numbers, I realized I needed to use selenium to search for the complete number. However, in order to retrieve the phone numbers faster, I found that it needs to sleep for 0.2 s ...

The functionality of pressing the Escape key is not supported in Selenium WebDriver when using Java

I am trying to remove the enlarged book screen from the page by pressing the "Escape" key. Here is the code I am using: driver.get("http://www.packtpub.com/selenium-webdriver-practical-guide/book"); WebElement cookieClose = driver.findElement(By.id("cooki ...

"Troubleshooting problems with converting XML to Json and deserializing objects - Could InfoPath namespaces

I am currently exploring the process of loading a large batch of InfoPath XML files into Cosmos DB as JSON files to test out a potential project. However, I am facing challenges when it comes to converting the XML files into JSON and then loading them back ...

Sending a string array to MVC controllers through ajax

I've been struggling to pass a list of strings from a multiple select to the controller. Despite seeming like a simple requirement, I've spent hours trying to figure it out without success. I've done some research on this but haven't be ...

What methods are available for me to apply an assertion specifically on the type of currency

Currently, I am hardcoding currency values like USD and CAD to perform assertions on the type of currency I receive from the application response. While using an XML file to store all currency types for comparison is possible, it seems like overkill for ju ...

Unable to transfer data to /html/body/p within the iframe for the rich text editor

When inputting content in this editor, everything is placed within the <p> tag. However, the system does not recognize /html/body/p, so I have tried using /html/body or switching to the active element, but neither approach seems to work. driver.sw ...

Selecting elements using XPath

My goal is to target an i element using the following XPath: //div[contains(text(), "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="32565356564153565341725e5d5d1c515d5f">[email protected]</a>")]//parent//i[contains(@c ...

Troubleshooting Selenium WebDriver/ChromeDriver Issue While Scraping Websites with Selenium

Looking to extract data from a table on this website - I attempted to run the code below, but encountered an error message. I'm fairly new to web scraping, so any assistance with the issue mentioned would be much appreciated. Code: import time from ...

Extracting webpage data from an array of links

After successfully creating a script that scrapes information from various product search pages, I am now faced with the task of extracting data from the full description of each product. The current script utilizes a loop and increments a designated value ...

Mastering Python selenium: Extracting all dropdown list values with index, including intricate dropdown structures

Currently, I am utilizing Python Selenium and I am looking to extract all the values from a dropdown menu that is displayed on the main screen. It's important to note that the dropdown values are dynamic and depend on another selection made in a diffe ...

Using Selenium to choose an input field within an AngularJS element

<md-datepicker ng-model="mc.date.from" required="" md-val=""> <span class="input-group date" style="width:144px"> <input size="16" type="text" class="form-control" autocomplete="off"> <span class="input- ...

Scrolling for Screenshots with Selenium

I've been working on a python script that navigates through the UI and completes numerous pages. However, I've run into an issue where if a page is longer than the screen size, my screenshots end up cutting off important information. I experiment ...

Guide on setting a wait while downloading a PDF file with protractor

As I begin the download of a pdf file, I realize that it will take more than 2 minutes to complete. In order to verify if the file has successfully downloaded or not, I will need to wait for the full 2 minutes before performing any verification checks. C ...

Troubleshooting Stale Element Issue in Java and Selenium

While using Selenium, I keep facing the Stale Element Error. As per my research, this error occurs when you switch away from a page or when an element on the page has been modified. The problem arises while I'm traversing through a list of web element ...

Is it possible to utilize a shared data provider method for multiple test cases in Selenium? Can we specify the Excel file path and sheet name within the common data provider?

My current scenario involves having multiple test cases named classAtest1, classAtest2, classAtest3, and so on. Each class requires a separate data provider method to retrieve the necessary data. However, I am looking to streamline this process by creati ...

What are the steps to extract information from the elements tab using selenium?

Currently, I am utilizing the edge driver in Selenium to scrape a webpage. During this process, I have observed inconsistencies between the information displayed under the elements tab in the developer window, which can be viewed at: https://i.stack.imgur ...

Clearing user session data from the Selenium WebDriver (Chrome) can be accomplished by manually deleting cookies and local storage, as simply

Currently using Selenium 4.0.0 with Java for UI automation, utilizing a Chrome webdriver. The process involves logging in, navigating to page 1, and then logging out. Initially, everything worked smoothly when the driver loads the login page for the first ...

What is the best method to extract information from JavaScript tables using Python and Selenium?

As a beginner in Python, JavaScript, and Web-Scraping, I am facing the challenge of extracting data from tables on a specific webpage (https://www.mcmaster.com/cam-lock-fittings/material~aluminum/) and saving it into a csv file. Initially, I attempted to ...