Selenium series - Basic Practice of Web UI automation

Posted by kurky on Sun, 05 Apr 2020 16:47:40 +0200

If you want to learn Selenium from scratch, you can read this series of articles!

https://www.cnblogs.com/poloyy/category/1680176.html

 

Secondly, if you don't know the basic knowledge of the front-end, you need to supplement it yourself. The blogger hasn't summarized it for the moment (although I can, so I don't need to review the front-end when I study selenium...)

 

Note that the current actual combat is written in the form of daily accounts, which will be combined with the framework + PO mode later

The purpose is to master the Selenium foundation

 

Actual combat topics

1. Visit: http://www.51job.com

2. Enter the search keyword "python" and select "Beijing" for the region (Note: if other regions have been selected for the location, remove it)

3. Search for the latest posts and grab the page information. Get the following formatting information

Python Development Engineer | Hangzhou Napa Technology Co., Ltd. | Hangzhou | 8-16000 / month | 04-27

Python Senior Development Engineer | China zhexin Technology Consulting Co., Ltd. | Hangzhou | 1-15000 / month | 04-27

Senior Python Development Engineer | Hangzhou new thinking Computer Co., Ltd. | Hangzhou West Lake District | 1-15000 / month | 04-27

 

 

 

 

Code thinking

  1. Locate the search box and type python
  2. Click [region]
  3. Explicitly wait to locate all selected cities by default
  4. Uncheck them
  5. Click [Beijing]
  6. Click [OK]
  7. Click search
  8. Locate position list, except the first line
  9. Cycle the position list to obtain the information of each line and store it in the list
  10. Format output

 

Code (operation steps for human test)

# !/usr/bin/env python
# -*- coding: utf-8 -*-

"""
__title__  =
__Time__   = 2020/3/25 17:52
__Author__ = Pineapple test notes
__Blog__   = https://www.cnblogs.com/poloyy/
"""
from time import sleep
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.by import By


# Set element to wait for instance for 10 seconds at most, every 0.5 Seconds to find
def wait_element(driver, by_, element_, timeout=10):
    element = WebDriverWait(driver=driver, timeout=timeout).until(
        lambda x: x.find_element(by=by_, value=element_)
    )
    return element


# Set element to wait for instance for 10 seconds at most, every 0.5 Seconds to find
def wait_elements(driver, by_, element_, timeout=10):
    element = WebDriverWait(driver=driver, timeout=timeout).until(
        lambda x: x.find_elements(by=by_, value=element_)
    )
    return element


# Load driver
driver = webdriver.Chrome("../resources/chromedriver.exe")

# Open web site
driver.get("http://www.51job.com")
driver.maximize_window()
# Search box
wait_element(driver, By.CSS_SELECTOR, "#kwdselectid").send_keys("python")

# Area button
wait_element(driver, By.CSS_SELECTOR, "#work_position_input").click()

# Hot cities list
city_lists = wait_elements(driver, By.CSS_SELECTOR, "div#work_position_click_center_right_list_000000 td em.on")

# Select Beijing and uncheck other cities
for city in city_lists:
    sleep(1)
    city.click()

wait_element(driver, By.CSS_SELECTOR, "em#work_position_click_center_right_list_category_000000_010000").click()

# Confirm button
driver.find_element_by_css_selector("#work_position_click_bottom_save").click()

# Search button click
wait_element(driver, By.CSS_SELECTOR, "div.top_wrap button").click()

# Position list found
lists = wait_elements(driver, By.CSS_SELECTOR, "div#resultList>div.el")[1:]

for data in lists:
    spans = [i.text for i in data.find_elements_by_css_selector("span")]
    print(" | ".join(spans))

sleep(10)
# Exit browser
driver.quit()

Topics: Python Selenium Lambda