Automated test script
Test case
Open the Baidu homepage, search for Selenium, and then check the search list to see if there is a link to the Selenium official website
Test steps:
1) Start the browser
2) Open Baidu homepage: http://www.baidu.com
3) Locate the search input box and record the xpath expression of the input box element: //*[@id='kw']
4) Locate the search submit button (click on Baidu) to get the xpath expression: //[@id='su']
5) Enter in the search input box: Selenium, click this button on Baidu
6) In the search result list to determine whether there is a link to the official website of Zhihu
7) Exit the browser and end the test
Test script:
import time
from selenium import webdriver
//Open chrome, if you use Firefox, replace it with webdriver.Firefox()
driver = webdriver.Chrome()
//Maximize the browser window
driver.maximize_window()
//Set the implicit time to wait
driver.implicitly_wait(8)
//Enter the Baidu address in the address bar
driver.get(“https://www.baidu.com”)
//Enter Selenium in the search input box
driver.find_element_by_id(“kw”).send_keys(“selenium”)
或者driver.find_element_by_xpath("//*[@id=‘kw’]").send_keys(“selenium”)
//Click the Baidu button
driver.find_element_by_id(“su”).click()
或者driver.find_element_by_xpath("//*[@id=‘su’]").click()
//Import the time module and wait for 2 seconds
time.sleep(2)
Here, the element XPath expression is used to determine that the element is displayed in the result list, so as to determine that the link of Selenium official website is displayed in the result list.
Define the variable ele_string to determine whether it is the value that needs to be displayed after the search
ele_string=driver.find_element_by_xpath("//*[@id=‘1’]/h3/a").text
if(ele_string ==“Selenium - Web Browser Automation”):
print(“测试成功”)
driver.quit()