Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Selenium Browser Operations

Selenium Browser Operations

Selenium can not only manipulate web page elements but also control the browser's own behavior, such as managing windows, handling pop-ups, manipulating cookies, executing JavaScript, and more.

The following are common browser operations in Selenium and their descriptions:

Operation types Method Description
Open a new window execute_script("window.open('URL');") Open a new window using JavaScript.
Switch windows switch_to.window(window_handle) Switch to the specified window handle.
Close a window close() Close the current window.
Maximize the window maximize_window() Maximize the browser window.
Set window size set_window_size(width, height) Set the size of the browser window.
Fullscreen mode fullscreen_window() Set the browser window to full-screen mode.
Open a web page get(url) Open the specified URL.
Forward and backward back() and forward() Return to the previous page or go forward to the next page.
Refresh the page refresh() Refresh the current page.
Handle Alert pop-up switch_to.alert combined with accept() or dismiss() Handle an Alert pop-up, clicking confirm or cancel.
Handle Confirm pop-up switch_to.alert combined with accept() or dismiss() Handle a Confirm pop-up, clicking confirm or cancel.
Handle Prompt pop-up switch_to.alert combined with send_keys() and accept() or dismiss() Handle a Prompt pop-up, entering text then clicking confirm or cancel.
Add a cookie add_cookie(cookie_dict) Add the specified cookie.
Get a cookie get_cookie(name) or get_cookies() Get the specified cookie or all cookies.
Delete a cookie delete_cookie(name) or delete_all_cookies() Delete the specified cookie or all cookies.
Execute JavaScript execute_script(script) Execute the specified JavaScript code.
Get JavaScript return value execute_script("return ...") Execute JavaScript and return the result.
Switch to iframe switch_to.frame(iframe_element) Switch to a specified iframe.
Switch back to the main page switch_to.default_content() Switch back to the main page.

1. Handling Browser Pop-ups (alert, confirm, prompt)

In web applications, pop-ups (such as alert, confirm, prompt) are common user interaction methods. Selenium provides Alert a class to handle these pop-ups.

1.1 Handling alert pop-ups

alert A pop-up is usually used to display a message, and the user can only close it by clicking the "OK" button.

Example

from

selenium

import

webdriver

from

selenium.

webdriver

.

chrome

.

service

import

Service

as

ChromeService

from

selenium.

webdriver

.

common

.

alert

import

Alert

Set the correct driver path

service

=

ChromeService

(

executable_path

=

"./chromedriver-mac-arm64/chromedriver"

)

options

=

webdriver.

ChromeOptions

(

)

driver

=

webdriver.

Chrome

(

service

=

service

,

options

=

options

)

Open a web page

driver.

get

(

"https://example.com"

)

Trigger an alert pop-up

driver.

execute_script

(

"alert('This is an alert pop-up');"

)

Switch to the alert pop-up

alert

=

Alert

(

driver

)

Get the pop-up text

print

(

alert.

text

)

Click the "OK" button to close the pop-up

alert.

accept

(

)

Close the browser

driver.

quit

(

)

1.2 Handling confirm pop-ups

confirm A pop-up is usually used to confirm an action; the user can choose "OK" or "Cancel".

Example

from

selenium

import

webdriver

from

selenium.

webdriver

.

chrome

.

service

import

Service

as

ChromeService

from

selenium.

webdriver

.

common

.

alert

import

Alert

Set the correct driver path

service

=

ChromeService

(

executable_path

=

"./chromedriver-mac-arm64/chromedriver"

)

options

=

webdriver.

ChromeOptions

(

)

driver

=

webdriver.

Chrome

(

service

=

service

,

options

=

options

)

Open a web page

driver.

get

(

"https://example.com"

)

Trigger a confirm pop-up

driver.

execute_script

(

"confirm('This is a confirm pop-up');"

)

Switch to the confirm pop-up

alert

=

Alert

(

driver

)

Get the pop-up text

print

(

alert.

text

)

Click the "OK" button

alert.

accept

(

)

or click the "Cancel" button

alert.dismiss()

Close the browser

driver.

quit

(

)

1.3 Handling prompt pop-ups

prompt A pop-up is usually used to get user input; the user can enter text and choose "OK" or "Cancel".

Example

from

selenium

import

webdriver

from

selenium.

webdriver

.

chrome

.

service

import

Service

as

ChromeService

from

selenium.

webdriver

.

common

.

alert

import

Alert

Set the correct driver path

service

=

ChromeService

(

executable_path

=

"./chromedriver-mac-arm64/chromedriver"

)

options

=

webdriver.

ChromeOptions

(

)

driver

=

webdriver.

Chrome

(

service

=

service

,

options

=

options

)

Launch the browser

driver

=

webdriver.

Chrome

(

)

Open a web page

driver.

get

(

"https://example.com"

)

Trigger a prompt pop-up

driver.

execute_script

(

"prompt('This is a prompt pop-up');"

)

Switch to the prompt pop-up

alert

=

Alert

(

driver

)

Get the pop-up text

print

(

alert.

text

)

Enter text

alert.

send_keys

(

"User input"

)

Click the "OK" button

alert.

accept

(

)

or click the "Cancel" button

alert.dismiss()

Close the browser

driver.

quit

(

)

2. Switching Windows and Tabs

In web applications, users may open multiple windows or tabs. Selenium provides switch_to.window() methods to switch between windows or tabs.

2.1 Switching Windows

Example

from

selenium

import

webdriver

from

selenium.

webdriver

.

chrome

.

service

import

Service

as

ChromeService

Set the correct driver path

service

=

ChromeService

(

executable_path

=

"./chromedriver-mac-arm64/chromedriver"

)

options

=

webdriver.

ChromeOptions

(

)

driver

=

webdriver.

Chrome

(

service

=

service

,

options

=

options

)

Open the first window

driver.

get

(

"https://example.com"

)

Open the second window

driver.

execute_script

(

"window.open('https://example.org');"

)

Get all window handles

window_handles

=

driver.

window_handles

Switch to the second window

driver.

switch_to

.

window

(

window_handles

[

1

]

)

Operate in the second window

print

(

driver.

title

)

Switch back to the first window

driver.

switch_to

.

window

(

window_handles

[

0

]

)

Close the browser

driver.

quit

(

)

2.2 Switching Tabs

Switching tabs is similar to switching windows, because each tab is an independent window handle.

Example

from

selenium

import

webdriver

from

selenium.

webdriver

.

chrome

.

service

import

Service

as

ChromeService

Set the correct driver path

service

=

ChromeService

(

executable_path

=

"./chromedriver-mac-arm64/chromedriver"

)

options

=

webdriver.

ChromeOptions

(

)

driver

=

webdriver.

Chrome

(

service

=

service

,

options

=

options

)

Open the first tab

driver.

get

(

"https://example.com"

)

Open the second tab

driver.

execute_script

(

"window.open('https://example.org');"

)

Get all tab handles

tab_handles

=

driver.

window_handles

Switch to the second tab

driver.

switch_to

.

window

(

tab_handles

[

1

]

)

Operate in the second tab

print

(

driver.

title

)

Switch back to the first tab

driver.

switch_to

.

window

(

tab_handles

[

0

]

)

Close the browser

driver.

quit

(

)

3. Handling iframes

iframe is another web page embedded in a web page. Selenium provides switch_to.frame() a method to switch to an iframe.

3.1 Switch to an iframe

Example

from

selenium

import

webdriver

from

selenium.

webdriver

.

chrome

.

service

import

Service

as

ChromeService

Set the correct driver path

service

=

ChromeService

(

executable_path

=

"./chromedriver-mac-arm64/chromedriver"

)

options

=

webdriver.

ChromeOptions

(

)

driver

=

webdriver.

Chrome

(

service

=

service

,

options

=

options

)

Open a web page

driver.

get

(

"https://example.com"

)

Switch to iframe

iframe

=

driver.

find_element_by_tag_name

(

"iframe"

)

driver.

switch_to

.

frame

(

iframe

)

Operate inside the iframe

print

(

driver.

page_source

)

Switch back to the main page

driver.

switch_to

.

default_content

(

)

Close the browser

driver.

quit

(

)

3.2 Switch back to the main page

After finishing operations inside the iframe, you can use switch_to.default_content() a method to switch back to the main page.

Example

Switch back to the main page

driver.

switch_to

.

default_content

(

)

4. Browser Cookie Operations

Selenium provides get_cookies(), add_cookie(), delete_cookie() and other methods to manipulate the browser's cookies.

4.1 Get Cookies

Example

from

selenium

import

webdriver

from

selenium.

webdriver

.

chrome

.

service

import

Service

as

ChromeService

Set the correct driver path

service

=

ChromeService

(

executable_path

=

"./chromedriver-mac-arm64/chromedriver"

)

options

=

webdriver.

ChromeOptions

(

)

driver

=

webdriver.

Chrome

(

service

=

service

,

options

=

options

)

Open a web page

driver.

get

(

"https://example.com"

)

Get all cookies

cookies

=

driver.

get_cookies

(

)

print

(

cookies

)

Close the browser

driver.

quit

(

)

4.2 Add a Cookie

Example

from

selenium

import

webdriver

from

selenium.

webdriver

.

chrome

.

service

import

Service

as

ChromeService

Set the correct driver path

service

=

ChromeService

(

executable_path

=

"./chromedriver-mac-arm64/chromedriver"

)

options

=

webdriver.

ChromeOptions

(

)

driver

=

webdriver.

Chrome

(

service

=

service

,

options

=

options

)

Open a web page

driver.

get

(

"https://example.com"

)

Add a cookie

driver.

add_cookie

(

{

"name"

:

"test"

,

"value"

:

"123"

}

)

Get all cookies

cookies

=

driver.

get_cookies

(

)

print

(

cookies

)

Close the browser

driver.

quit

(

)

4.3 Delete a Cookie

Example

from

selenium

import

webdriver

from

selenium.

webdriver

.

chrome

.

service

import

Service

as

ChromeService

Set the correct driver path

service

=

ChromeService

(

executable_path

=

"./chromedriver-mac-arm64/chromedriver"

)

options

=

webdriver.

ChromeOptions

(

)

driver

=

webdriver.

Chrome

(

service

=

service

,

options

=

options

)

Open a web page

driver.

get

(

"https://example.com"

)

Add a cookie

driver.

add_cookie

(

{

"name"

:

"test"

,

"value"

:

"123"

}

)

Delete a cookie

driver.

delete_cookie

(

"test"

)

Get all cookies

cookies

=

driver.

get_cookies

(

)

print

(

cookies

)

Close the browser

driver.

quit

(

)

5. Execute JavaScript Code

Selenium provides execute_script() a method to execute JavaScript code.

5.1 Execute JavaScript Code

Example

from

selenium

import

webdriver

from

selenium.

webdriver

.

chrome

.

service

import

Service

as

ChromeService

Set the correct driver path

service

=

ChromeService

(

executable_path

=

"./chromedriver-mac-arm64/chromedriver"

)

options

=

webdriver.

ChromeOptions

(

)

driver

=

webdriver.

Chrome

(

service

=

service

,

options

=

options

)

Open a web page

driver.

get

(

"https://example.com"

)

Execute JavaScript code

driver.

execute_script

(

"alert('This is a JavaScript pop-up');"

)

Close the browser

driver.

quit

(

)

5.2 Get the JavaScript Return Value

Example

from

selenium

import

webdriver

from

selenium.

webdriver

.

chrome

.

service

import

Service

as

ChromeService

Set the correct driver path

service

=

ChromeService

(

executable_path

=

"./chromedriver-mac-arm64/chromedriver"

)

options

=

webdriver.

ChromeOptions

(

)

driver

=

webdriver.

Chrome

(

service

=

service

,

options

=

options

)

Open a web page

driver.

get

(

"https://example.com"

)

Execute JavaScript code and get the return value

result

=

driver.

execute_script

(

"return document.title;"

)

print

(

result

)

Close the browser

driver.

quit

(

)


6. Comprehensive Example

The following is a complete example showing how to use multiple browser operations:

Example

from

selenium

import

webdriver

from

selenium.

webdriver

.

chrome

.

service

import

Service

as

ChromeService

from

selenium.

webdriver

.

common

.

by

import

By

from

selenium.

webdriver

.

common

.

keys

import

Keys

Set the correct driver path

service

=

ChromeService

(

executable_path

=

"./chromedriver-mac-arm64/chromedriver"

)

options

=

webdriver.

ChromeOptions

(

)

driver

=

webdriver.

Chrome

(

service

=

service

,

options

=

options

)

Open a web page

driver.

get

(

"https://www.example.com"

)

Maximize the window

driver.

maximize_window

(

)

Add a cookie

driver.

add_cookie

(

{

"name"

:

"test"

,

"value"

:

"123"

}

)

Get a cookie

cookie

=

driver.

get_cookie

(

"test"

)

print

(

"Cookie:"

,

cookie

)

Execute JavaScript

driver.

execute_script

(

"window.scrollTo(0, document.body.scrollHeight);"

)

Handle Alert pop-up

driver.

execute_script

(

"alert('Hello, Selenium!');"

)

alert

=

driver.

switch_to

.

alert

print

(

"Pop-up text:"

,

alert.

text

)

alert.

accept

(

)

Switch to iframe

iframe

=

driver.

find_element

(

By.

ID

,

"iframe-id"

)

driver.

switch_to

.

frame

(

iframe

)

Switch back to the main page

driver.

switch_to

.

default_content

(

)

Close the browser

driver.

quit

(

)