Selenium webdriver selecting a new c # window

Trying to write some test cases using selenium webdriver in C # and have a script that I'm not sure how to resolve

the user script looks for a table for the patient, selects the patient, then opens a new window, and then approve the various elements in the window

My problem: I canโ€™t select a new window to approve anything, it is not a pop-up window, it is a complete new browser window, but it doesnโ€™t have a window title / name to define it, so that I could be able to switch the driver focus to this window?

early

+9
source share
6 answers

It is really easy in Selenium WebDriver. Using the SwitchTo Method

driver.SwitchTo().Window(driver.WindowHandles.Last()); 

See also blog post

http://binaryclips.com/2015/03/13/selenium-webdriver-in-c-switch-to-new-window/

+12
source

If I assemble your application correctly, you will create a window on it without additional user intervention. You should be able to wait for the page to load, and then you can name your statements as usual.

Selenium already has your browser session, so a new window is not a problem for selenium, it's just new content.

0
source
 foreach (string defwindow in driver.WindowHandles) { driver.SwitchTo().Window(defwindow); if(driver.Title == "") { selenium.WindowFocus(); selenium.SelectWindow(""); } } 

"" - indicates your window Title

0
source

I have a code that you might like. The quickest solution is to use the Popup Finder, but I also made my own method. I will never rely on the order that Window Handles processes to select the appropriate window. Popup window search:

 PopupWindowFinder finder = new PopupWindowFinder(driver); driver.SwitchTo().Window(newWin); 

My custom method. Basically, you give it the element you want to click, your webdriver and, if necessary, the time to wait before searching after clicking on the element.

It takes all of your current descriptors and makes a list. It uses this list to eliminate pre-existing windows from being accidentally switched. Then he clicks on the item that launches a new window. There should always be some kind of delay after the click, since nothing happens instantly. And then he creates a new list and compares it with the old one, until he finds a new window or the loop expires. If it cannot find a new window, it returns null, so if you have an iffy web element that does not always work, you can perform a null check to see if the switch is working.

 public static string ClickAndSwitchWindow(IWebElement elementToBeClicked, IWebDriver driver, int timer = 2000) { System.Collections.Generic.List<string> previousHandles = new System.Collections.Generic.List<string>(); System.Collections.Generic.List<string> currentHandles = new System.Collections.Generic.List<string>(); previousHandles.AddRange(driver.WindowHandles); elementToBeClicked.Click(); Thread.Sleep(timer); for (int i = 0; i < 20; i++) { currentHandles.Clear(); currentHandles.AddRange(driver.WindowHandles); foreach (string s in previousHandles) { currentHandles.RemoveAll(p => p == s); } if (currentHandles.Count == 1) { driver.SwitchTo().Window(currentHandles[0]); Thread.Sleep(100); return currentHandles[0]; } else { Thread.Sleep(500); } } return null; } 
0
source

This code worked for me. In my case, the new window / tab is a PDF, which has some weight, so I make some custom expectations while it loads.

 WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(5)); int previousWinCount = driver.WindowHandles.Count; // Perform the action to open a new Window wait.Until(driver => driver.WindowHandles.Count == (previousWinCount + 1)); driver.SwitchTo().Window(driver.WindowHandles.Last()); wait.Until(driver => driver.Url.Contains("desired_url_or_a_substring_of_it")); 

Please note that driver.Url when loading the PDF is "about: blank" .

0
source
  IWebDriver _driver = new FirefoxDriver(); _driver.Navigate().GoToUrl("https://www.google.com"); ReadOnlyCollection<string> WindowHandles = _driver.WindowHandles; foreach (string item in WindowHandles) { _driver.SwitchTo().Window(item); string browserTitle = _driver.Title; string browserPageSource = _driver.PageSource; string browserURL = _driver.Url; } 

Use ReadOnlyCollection and control your browser, get the title of your window, compare and focus on your browser window.

0
source

Source: https://habr.com/ru/post/983736/


All Articles