kgb26
kgb26

Reputation: 183

Python Selenium Webdriver Handling Multiple Windows At The Same Time

I am working on a project and in this project there are two webpages I need to examine.I have to open these two webpages at the same time in different windows and examine them at the same time.

There must be two windows and there is a function that checks an element's status.But this function must be checking them at the same time.How can I solve this problem?
Thanks

Upvotes: 0

Views: 3377

Answers (2)

Bryan Oakley
Bryan Oakley

Reputation: 386342

You can open more than one browser at a time:

from selenium import webdriver

driver1 = webdriver.Firefox()
driver2 = webdriver.Firefox()

driver1.get(...)
driver2.get(...)

If you want two windows into the current driver session you can use a little javascipt to open a second window with a single driver:

driver.execute_script("$(window.open('http://www.example.com'))")
window1 = driver.window_handles[0]
window2 = driver.window_handles[1]

# test the first window
driver.switch_to_window(window1)
...

# test the second window
driver.switch_to_window(window2)
...

Upvotes: 2

ashuns
ashuns

Reputation: 11

I'm not sure about doing at at exactly the same time, but you could open one of the pages in a separate tab, then move between the two do your comparison.

driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL +"t");
ArrayList<String> tabs = new ArrayList<String> (driver.getWindowHandles());
driver.switchTo().window(tabs.get(0));
***get value to compare**** 
driver.switchTo().window(tabs.get(1));
driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL +"Keys.TAB");
***get second value and compare***

If not, perhaps look into using Selenium Grid to run two tests in parallel. I only spotted that you're working in Python after writing the answer, but im sure there's an equivalent.

Upvotes: 0

Related Questions