selenium自动化测试小练

伪代码展示(吉林大学自动健康填报实现)

from selenium import webdriver
import time
chrome_driver=r"D:\Python\Lib\site-packages\selenium\webdriver\chrome\chromedriver.exe"
driver=webdriver.Chrome(chrome_driver)
if __name__=="__main__":
    driver.get(r"https://ehall.jlu.edu.cn")
    driver.find_element_by_xpath("//*[@id='username']").send_keys("这里写vpns的用户名") 
    driver.find_element_by_xpath("//*[@id='password']").send_keys("这里写vpns的密码") 
    driver.find_element_by_xpath("//*[@id='login-submit']").click()
    driver.find_element_by_xpath("/html/body/div[1]/div[4]/ul/li[1]/a").click()
    driver.find_element_by_xpath("/html/body/div[1]/div/div[3]/div/ul[1]/li").click()
    driver.switch_to.window(driver.window_handles[-1])
    driver.find_element_by_xpath("//*[@id='V1_CTRL82']").click()
    driver.find_element_by_xpath("//*[@id='form_command_bar']/li[1]/a").click()
    time.sleep(3)
    driver.find_element_by_xpath("//*[@class='dialog display']/div[2]/button[1]").click()
    time.sleep(3)
    driver.find_element_by_xpath("//*[@class='dialog display']/div[2]/button").click()
    driver.quit()

基本思路及所用知识

该程序必须安装selelnium和chromedriver.exe才能运行,首先用selenium启动谷歌浏览器,然后进入登录页面,填用户名密码后点击登录,然后运用谷歌浏览器的开发者工具可以很轻易得到你想得到的节点的xpath,通过xpath定位元素,然后填信息或者点击即可模拟人为操作,需要注意的是其中用到了浏览器当前网页切换和等待页面更新再操作的小知识点

发布了84 篇原创文章 · 获赞 0 · 访问量 2014

猜你喜欢

转载自blog.csdn.net/qq_41985293/article/details/104561760