SoFunction
Updated on 2024-11-18

Getting Started with Selenium+Python Automation Testing

Introduction to Selenium

Selenium is a collection of tools and libraries that support web browser automation.At the heart of Selenium is the WebDriver., which is an interface for writing instruction sets that run interchangeably in many browsers.

Environment Setup

Python Installation

The version of Python that comes with the mac system is 2.7 (a low version), so you can install it directly with the command brew install python3

View the installation location via which python3

Run python3 in the terminal and see the following screen, it means that Python has been installed successfully!

Install Pycharm

  • existPycharm official websitedownloading
  • Editor creates folders (e.g. selecode)
  • Pycharm opens the selecode folder with the following settings

selenium installation

pip install

1.1 Download pip

curl /pip/ -o 

1.2 Installation

Install selenium via pip

pip install selenium

View selenium

pip show selenium

WebDriver Installation

Check the browser version, the WebDriver version needs to be the same as the browser version.

Download the WebDriver that corresponds to your browser version.

Adding Environment Variables

vim ~/.profile
export PATH="$PATH:/usr/local/chromedriver"
source ~/.profile

3.1 The mac also needs to add trust, in the webdriver directory, execute the

chromedriver % xattr -d  chromedriver

3.2 Test on the console to see if it works.

Basic use of selemium

Creating a python file

Email Run the demo code below to see the effect

positioning of elements

ID location

xpath positioning (direct Copy full Xpath)

multi-window

In selenium, a window is uniquely identified by a randomly generated string (uuid)

Execute js: open new window

new = '("/sso/Account/Login?ReturnUrl=%2Fsso%2Fconnect%2Fauthorize' \
  '%2Fcallback%3Fclient_id%3Dhpa-ctm-vendor-portal%26redirect_uri%3Dhttps%253A%252F%' \
  '%252Fcallback%26response_type%3Dtoken%2520id_token%26scope%3Dopenid%2520profile%2520vendor-api%2520foundation' \
  '-api%26state%3D5a6d7d6d84264b828f02081ac9fb1c3a%26nonce%3D7cb02e1da5214cd0828d4373b29e5b66"); '
  
driver.execute_script(new)

Getting the handler
3.1 Get all: driver.window_handlers
3.2 Get a single: driver.current_window_handle
3.3 Switching: driver.switch_to_window(handle)

concluding remarks

You can make some simple demos by doing the above, but how to produce reports? How to engineer? And how to deploy iterations? Still need to learn more

To this article on Selenium + Python automation testing to get started on this article, more related Selenium automation testing content please search for my previous articles or continue to browse the following related articles I hope you will support me in the future!