Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stop selenium from opening a new window when clicking on a link

I am exporting my selenium test to python and running the test using the shell. I get to a certain point where I click on a link and the web-driver then opens a completely new Firefox window and then I get an error saying that the driver can't find the input option on the page. I think the problem is that when it opens the new window the driver is not running on the new window, which is why it can't find the input option. How do I get the script to stay on the same window or switch it's focus to the new window?

Thanks!

like image 935
seeiespi Avatar asked Aug 08 '13 23:08

seeiespi


People also ask

How does Selenium handle open a new window?

Open a New Tabget('https://selenium.dev'); // A new tab is opened and switches to it await driver. switchTo(). newWindow('tab'); // Loads Sauce Labs open source website in the newly opened window await driver. get('https://opensource.saucelabs.com/');

How do I change the default window in Selenium Webdriver?

We can switch back from a frame to default in Selenium webdriver using the switchTo(). defaultContent() method. Initially, the webdriver control remains on the main web page. In order to access elements within the frame, we have to shift the control from the main page to the frame with the help of the switchTo().

How do I stop Selenium from opening in Chrome?

We can stop a page loading with Selenium webdriver in Chrome browser by using the JavaScript Executor. Selenium can execute JavaScript commands with the help of the executeScript command. To stop a page loading, the command window. stop() is passed as a parameter to the executeScript method.

How do I close the whole browser window by keeping the Webdriver active?

In your case you have to use driver. close() which will close current window and keeps driver active. Just to add - if there is only browser window open and you use driver. close() , it will quit the webdriver session.


1 Answers

I don't know how to do it in python, but there should be a function to switch to a new window.

In Java, I do this:

Set<String> availableWindows = driver.getWindowHandles();
for (String windowHandle : availableWindows) {
    driver.switchTo().window(windowHandle);
    if (getTitle().contains(TITLE_TO_MATCH)){
           driver.manage().window().maximize();
            return;
    }
}

(The function want is driver.switchTo().window(NameOrHandle))

like image 118
Nathan Merrill Avatar answered Oct 21 '22 17:10

Nathan Merrill