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

Selenium Advanced Features

Selenium Advanced Features

Selenium provides many advanced features that help handle complex automated testing scenarios.

The following are detailed explanations of several advanced topics, including handling dynamic content, CAPTCHA, proxies, headless browser mode, and performance optimization techniques.

1. Handling Dynamic Content

Dynamic content refers to content on a web page that is dynamically generated after page load via JavaScript or other technologies. This content may include advertisements, user comments, real-time updated data, etc. Handling dynamic content is a common challenge in Selenium automated testing.

1.1 Wait Mechanisms

Selenium provides several wait mechanisms to handle dynamic content, including Implicit Wait and Explicit Wait.

Implicit wait: Set a global wait time; Selenium waits the specified time when locating elements. If the element is found within the specified time, execution continues; otherwise an exception is thrown.

Example

driver.

implicitly_wait

(

10

)

 

Wait 10 seconds

Explicit wait: Set a wait condition for a specific element until the condition is met or a timeout occurs. Explicit wait is more flexible and suitable for handling complex dynamic content.

Example

from

selenium.

webdriver

.

common

.

by

import

By

from

selenium.

webdriver

.

support

.

ui

import

WebDriverWait

from

selenium.

webdriver

.

support

import

expected_conditions

as

EC

element

=

WebDriverWait

(

driver

,

10

)

.

until

(

    EC.

presence_of_element_located

(

(

By.

ID

,

"dynamic-element"

)

)

)

1.2 Handling AJAX Requests

AJAX (Asynchronous JavaScript and XML) requests are a common source of dynamic content.

Selenium can handle dynamic content by waiting for AJAX requests to complete.

Example

Wait for the AJAX request to complete

WebDriverWait

(

driver

,

10

)

.

until

(

   

lambda

d: d.

execute_script

(

"return jQuery.active == 0"

)

)


2. Handling CAPTCHA

CAPTCHA is a security mechanism used to distinguish human users from automated scripts.

Since CAPTCHA is designed to prevent automated operations, handling CAPTCHA in Selenium is a complex issue.

2.1 Bypassing CAPTCHA

In some test environments, CAPTCHA can be bypassed in the following ways:

2.2 Automating CAPTCHA Handling

For CAPTCHAs that cannot be bypassed, consider the following methods:

Third-party services: Use third-party CAPTCHA recognition services such as 2Captcha or Anti-Captcha to automatically recognize CAPTCHAs via an API interface.

Example

import

requests

api_key

=

"your_api_key"

captcha_image_url

=

"https://example.com/captcha.jpg"

response

=

requests.

post

(

   

"https://2captcha.com/in.php"

,

    data

=

{

"key"

: api_key

,

"method"

:

"base64"

,

"body"

: captcha_image_url

}

)

captcha_id

=

response.

text

.

split

(

"|"

)

[

1

]

OCR technology: Use OCR (Optical Character Recognition) technology to recognize the text in CAPTCHA images.


3. Using a Proxy

In some cases, it may be necessary to access the target website through a proxy server to simulate users from different regions or bypass IP restrictions.

3.1 Configuring a Proxy

Selenium allows using a proxy by configuring browser options.

Example

from

selenium.

webdriver

.

chrome

.

options

import

Options

chrome_options

=

Options

(

)

chrome_options.

add_argument

(

"--proxy-server=http://your-proxy-server:port"

)

driver

=

webdriver.

Chrome

(

options

=

chrome_options

)

3.2 Dynamically Switching Proxies

In some scenarios, it may be necessary to switch proxies dynamically. This can be achieved in the following ways:

Example

from

selenium.

webdriver

.

common

.

proxy

import

Proxy

,

ProxyType

proxy

=

Proxy

(

)

proxy.

proxy_type

=

ProxyType.

MANUAL

proxy.

http_proxy

=

"http://your-proxy-server:port"

proxy.

ssl_proxy

=

"http://your-proxy-server:port"

capabilities

=

webdriver.

DesiredCapabilities

.

CHROME

proxy.

add_to_capabilities

(

capabilities

)

driver

=

webdriver.

Chrome

(

desired_capabilities

=

capabilities

)

4. Headless Browser Mode

Headless browser mode means running the browser in the background without displaying a user interface.

This mode is suitable for automated testing and crawler tasks, improving execution efficiency and reducing resource consumption.

4.1 Enabling Headless Mode

In Selenium, headless mode can be enabled by configuring browser options.

Example

from

selenium.

webdriver

.

chrome

.

options

import

Options

chrome_options

=

Options

(

)

chrome_options.

add_argument

(

"--headless"

)

 

Enable headless mode

driver

=

webdriver.

Chrome

(

options

=

chrome_options

)

4.2 Limitations of Headless Mode

Although headless mode can improve efficiency, it also has some limitations:

5. Performance Optimization Techniques

In automated testing, performance optimization is an important consideration. The following are some techniques to improve Selenium script performance.

5.1 Reduce Page Load Time

Disable image loading: Reduce page load time by configuring browser options to disable image loading.

Example

chrome_options

=

Options

(

)

chrome_options.

add_argument

(

"--blink-settings=imagesEnabled=false"

)

Disable JavaScript: In some cases, disabling JavaScript can speed up page loading.

Example

chrome_options

=

Options

(

)

chrome_options.

add_argument

(

"--disable-javascript"

)

5.2 Run Tests in Parallel

Using Selenium Grid or third-party tools (such as pytest-xdist) to run tests in parallel can reduce total execution time.

Example

Run tests in parallel using pytest-xdist

pytest -n

4

 

Run in parallel using 4 processes

5.3 Use Efficient Locator Strategies

Choosing efficient locator strategies can reduce element lookup time. For example, prefer using By.ID or By.NAMErather than By.XPATH.

Example

element

=

driver.

find_element

(

By.

ID

,

"element-id"

)

5.4 Reduce Unnecessary Waits

Avoiding unnecessary waits can improve script execution efficiency. Make sure to use wait mechanisms only when necessary.

Example

Wait only when needed

if

not

element.

is_displayed

(

)

:

    WebDriverWait

(

driver

,

10

)

.

until

(

EC.

visibility_of

(

element

)

)