This article is participating in Python Theme Month. See the link for details

If you have ideas or techniques you’d like to share, feel free to leave them in the comments section.

Learning to program is about solving problems, and the rest of this series will be presented as a project that opens the door for you.

Do the preparatory work before the test project

  1. Project requirement analysis
  2. Develop a project plan
  3. Develop test cases
  4. To begin testing

Let’s skip the first three steps and go straight from step 4.

Automated testing is the way to search for airline tickets

The corresponding website of this case is: flight.tuniu.com/ it is necessary to complete the automatic writing of the following picture content, and then click search operation. As a seemingly small case study, you will see that Selenium is actually repeating the same action in automation, which is to find elements.

Element by element fetch

Getting the elements of a web page in Selenium is the most important operation, and as long as you can get them, you can do something about them.

Prioritize the origin and destination.

from selenium import webdriver
import time
driver = webdriver.Firefox()
driver.maximize_window()

driver.get('https://flight.tuniu.com/')

# Define the cities of departure and arrival
from_city = "Shijiazhuang"
to_city = "Sanya"

driver.find_element_by_id("J_FormDepartCity").send_keys(from_city)
driver.find_element_by_id("J_FormDestCity").send_keys(to_city)
Copy the code

Next set the time, found some small problems, directly use the following code, cannot set.

from_date = "2021-01-01"

driver.find_element_by_id("J_FormDepartCity").send_keys(from_city)
driver.find_element_by_id("J_FormDestCity").send_keys(to_city)
# Set time
driver.find_element_by_id("J_FormDepartDate").send_keys(from_date)
Copy the code

The problem is that the page parameter of the time control is readonly, that is, read-only, so it cannot be set.

Execute a javascript script to remove the read-only operation.

Delete the read-only property of the time control
driver.execute_script("document.getElementById('J_FormDepartDate').removeAttribute('readonly')")
# Set time
driver.find_element_by_id("J_FormDepartDate").clear()
driver.find_element_by_id("J_FormDepartDate").send_keys(from_date)
Copy the code

The code runs again, the problem is found again, and in actual programming we are constantly on the way to finding and solving problems. Directly through Selenium input program, the system cannot automatically search, need to realize the city search, click operation. As shown in the figure below.

After a couple of attempts at selecting the element (here you repeatedly look for ways to get the tag), you end up with the following code.

# Define the cities of departure and arrival
from_city = "SJZ"
to_city = "SY"
from_date = "2021-01-01"

driver.find_element_by_id("J_FormDepartCity").send_keys(from_city)
time.sleep(2)
driver.find_element_by_xpath("//div[@class='autocomplete-suggestions'][1]/div[1]").click()

driver.find_element_by_id("J_FormDestCity").send_keys(to_city)
time.sleep(2)
driver.find_element_by_xpath("//div[@class='autocomplete-suggestions'][2]/div[2]").click()
Copy the code

Up to now, all the elements have been obtained, the last step click operation, you can realize the query. The complete code for this stage is as follows.

from selenium import webdriver
import time
from selenium.webdriver.common.action_chains import ActionChains
driver = webdriver.Firefox()
driver.maximize_window()

driver.get('https://flight.tuniu.com/')

# Define the cities of departure and arrival
from_city = "SJZ"
to_city = "SY"
from_date = "2021-01-01"

driver.find_element_by_id("J_FormDepartCity").send_keys(from_city)
time.sleep(2)
driver.find_element_by_xpath("//div[@class='autocomplete-suggestions'][1]/div[1]").click()

driver.find_element_by_id("J_FormDestCity").send_keys(to_city)
time.sleep(2)
driver.find_element_by_xpath("//div[@class='autocomplete-suggestions'][2]/div[2]").click()

Delete the read-only property of the time control
driver.execute_script("document.getElementById('J_FormDepartDate').removeAttribute('readonly')")
# Set time
driver.find_element_by_id("J_FormDepartDate").clear()
driver.find_element_by_id("J_FormDepartDate").send_keys(from_date)

# Click on the other location to clear the floating window
ActionChains(driver).move_by_offset(0.10).click().perform()

# Click the search button
time.sleep(1)
driver.find_element_by_id("J_Search").click()
Copy the code

The final result is as follows.

Subsequent content

The above code has actually demonstrated the main process of an automated test project, because the road cow website is not our own development, so after many visits will be restricted access, bypass restrictions belong to the crawler knowledge, this project only show automation related content, other parts are no longer extended.

On subsequent jump pages, fetching elements is the same as the relevant code above. It is the same approach, and there is no fixed solution. Using Selenium is particularly like open-book programming, where everyone may use different code to implement the same function.

This case is a linear code project, executed from top to bottom, that will be modified in several subsequent blogs, a programming term known as refactoring.

Write in the back

Learning is about using and solving problems, so is there a task you want to accomplish? Can you use Selenium to solve the problem?


Blogger ID: Dream eraser, hope you like, comment, favorites.