Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in DevOps and Agile by (19.7k points)

I'm using Selenium WebDriver for Python. I want instantiate the browser with a specific width and height. So far the closest I can get is:

driver = webdriver.Firefox();

driver.set_window_size(1080,800);

Which works but sets the browser size after it is created, and I want it set at instantiation. I'm guessing there is an approach along the lines of:

profile = webdriver.FirefoxProfile();

profile.set_preference(foo, 1080);

driver = webdriver.Firefox(profile);

 But I don't know what foo would be, and I can't figure out where the docs are.

Q1: Is there a way to set width/height at instantiation?

Q2: Where are the reference docs listing all keys usable by profile.set_preference?

1 Answer

0 votes
by (62.9k points)

From Selenium's WebDriver API, Selenium allows resizing and maximizing window natively. You can use the 'Dimension' class to resize the window. You need to take the help of Selenium WebDrivers Dimension Class and declare object say, 'd' by initializing it with width and height as 420X600 as shown below: Do Note that, you are required to import the statement 'import org.openqa.selenium.Dimension'

import org.openqa.selenium.Dimension; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.testng.annotations.Test; public class browser { @Test public void openBrowserwithGivenDimension() { WebDriver driver = new FirefoxDriver(); driver.navigate().to("http://google.co.in"); System.out.println(driver.manage().window().getSize()); Dimension d = new Dimension(420,600); //Resize the current window to the given dimension driver.manage().window().setSize(d); } }

You can set the size and perform testing with lower resolution. Also, you can perform testing on Responsive sites which automatically get adjust based on the browser size.

If you are interested to learn Selenium on a much deeper level and want to become a professional in the testing domain, check out Intellipaat’s Selenium training courses!

Browse Categories

...