Selenium – Set the location and height width of the browser

Foreplay

When web automation, if you only have one display, if you start Google Chrome and occupy the entire display, then you must be unable to do other things NS. Of course, you can also use the phantomjs headless browser, and you can’t miss the compatibility of the browser. If at this time, selenium can automatically set the size of the browser, it will not affect your work. It will not hinder automation.

Get the location of the browser

from selenium import webdriver



def test_window_position():
url
= 'http://www.baidu.com span>'
driver
= webdriver.Chrome()
driver.get(url)

# Get the position of the current browser on the screen, the return is Dictionary object
position = driver.get_window_position()
print(position)
print('The abscissa of the current browser location:', position[ 'x'])
print('The ordinate of the current browser location:', position[ 'y'])


test_window_position()

Result:

{'x': 10, 'y': 10}

The abscissa of the current browser location:
10
The ordinate of the current browser location:
10

Description:

The obtained browser location refers to the upper left corner of the browser The position on the screen, the x and y coordinates are returned, which is the horizontal and vertical coordinates.

Set the position of the browser

< pre>from selenium import webdriver

def test_window_position():

url
= http://www.baidu.com span>

driver
= webdriver.Chrome()

driver.get(url)


position = driver.get_window_position()# Set the position of the current browser on the screen

driver.set_window_position(y=200, x=400)

print(driver.get_window_position())

test_window_position()

Result:

{'x': 400, 'y': 200} pre> 

Get the height and width of the browser

 from selenium import webdriver


def test_window_size():
url
='http://www.baidu.com'
driver
=webdriver.Chrome()
driver.get(url)

#Get the height and width of the current browser, and return a dictionary Type
sizeDict=driver.get_window_size()
print(sizeDict)
print('The width of the current browser window:',sizeDict['< /span>width'] )
print('The height of the current browser window:',sizeDict['< /span>height'] )


test_window_size()

Result:

{'width': 1050, 'height': 708}

The width of the current browser window:
1050
The height of the current browser window:
708

Set the height and width of the browser

< span style="color: #0000ff;">from selenium import webdriver


def test_window_size():
url
='http://www.baidu.com'
driver
=webdriver.Chrome()
driver.get(url)
sizeDict
=driver.get_window_size()

# Set the size of the current browser window
driver.set_window_size(width=500,height=400,windowHandle='current span>')
# After setting the browser window size, get the browser window size again Information
print(driver.get_window_size(windowHandle='current'))


test_window_size()

Result:

{'width': 516, 'height': 400} 

from selenium import webdriver



def test_window_position():
url
= 'http://www.baidu.com span>'
driver
= webdriver.Chrome()
driver.get(url)

# Get the position of the current browser on the screen, the return is Dictionary object
position = driver.get_window_position()
print(position)
print('The abscissa of the current browser location:', position[ 'x'])
print('The ordinate of the current browser location:', position[ 'y'])


test_window_position()

{'x': 10, 'y': 10}

The abscissa of the current browser location:
10
The ordinate of the current browser location:
10

from selenium import webdriver



def test_window_position():
url
= 'http://www.baidu.com span>'
driver
= webdriver.Chrome()
driver.get(url)

position = driver.get_window_position()# Set the position of the current browser on the screen
driver.set_window_position(y=200, x=400)
print(driver.get_window_position())

test_window_position()

{'x': 400, 'y': 200}

from selenium import webdriver


def test_window_size():
url
='http://www.baidu.com'
driver
=webdriver.Chrome()
driver.get(url)

#Get the height and width of the current browser, and return a dictionary Type
sizeDict=driver.get_window_size()
print(sizeDict)
print('The width of the current browser window:',sizeDict['< /span>width'] )
print('The height of the current browser window:',sizeDict['< /span>height'] )


test_window_size()

{'width': 1050, 'height': 708}

The width of the current browser window:
1050
The height of the current browser window:
708

from selenium import webdriver


def test_window_size():
url
='http://www.baidu.com'
driver
=webdriver.Chrome()
driver.get(url)
sizeDict
=driver.get_window_size()

# Set the size of the current browser window
driver.set_window_size(width=500,height=400,windowHandle='current span>')
# After setting the browser window size, get the browser window size again Information
print(driver.get_window_size(windowHandle='current'))


test_window_size()

{'width': 516, 'height': 400}

Leave a Comment

Your email address will not be published.