首页
登录 | 注册

(4)python+selenium-自动化测试——实战

1.登录豆瓣

登录代码

##登录豆瓣界面
from selenium import webdriver

class LoginTest():
    def test_vaildLogin(self):
        baseURL="https://www.douban.com/"
        driver=webdriver.Firefox()
        driver.maximize_window()
        driver.get(baseURL)

        email=driver.find_element_by_id("form_email")
        email.send_keys("123456")
        password=driver.find_element_by_id("form_password")
        password.send_keys("******")
        loginbnt=driver.find_element_by_class_name("bn-submit")
        loginbnt.click()
        
        username=driver.find_element_by_xpath('//*[@id="db-global-nav"]/div/div[1]/ul/li[2]')
        if username is not None:
            print("login successful")
            driver.quit()
        else:
            print("login failed")
            driver.quit()

ff=LoginTest()
ff.test_vaildLogin()
    

2.登录12306买票

#-*-coding:utf8-*-
import time
from selenium import webdriver

#运行chrome,打开浏览器
driver =webdriver.Firefox()
#设置浏览器窗口
driver.set_window_size(1080,800)

#设置全局操作时间
driver.implicitly_wait(10)

#打开网址
driver.get('https://kyfw.12306.cn/otn')
#睡五秒自动选择验证码
time.sleep(5)

driver.find_element_by_id("username").click()
driver.find_element_by_id("username").send_keys("522268397@qq.com")
driver.find_element_by_id("password").send_keys("***")
print("continue")
time.sleep(15)

driver.find_element_by_link_text("车票预订").click()
time.sleep(5)
#出发地选择
driver.find_element_by_id("fromStationText").click()
# setTimeout(function(){debugger;}, 5000)
driver.find_element_by_css_selector(u"[title=广州]").click()

time.sleep(5)
#目的地选择
driver.find_element_by_id("toStationText").click()
driver.find_element_by_css_selector(u"[title=北京]").click()
time.sleep(5)

#出发日期选择
driver.find_element_by_id("train_date").click()
driver.find_element_by_css_selector("body > div.cal-wrap > div:nth-child(1) > div.cal-cm > div:nth-child(24) > div").click()
time.sleep(5)

#车型选择
driver.find_element_by_css_selector("#_ul_station_train_code > li:nth-child(1) > label").click()
time.sleep(5)

while True:
    try:
        driver.find_element_by_id("query_ticket").click()
        e=driver.find_element_by_id("SWZ_6i00000G720F")
        e.click()
        if e.text in [u'无','--']:
            print ("nono")
            time.sleep(1)
        else:
            print("yes")
            # 购票
            driver.find_element_by_link_text("预订").click()
            # 乘车人选择
            driver.find_element_by_css_selector("#normal_passenger_id > li:nth-child(1) > label").click()
            # 学生票确认(不是学生可去掉)
            driver.find_element_by_link_text("确认").click()
            # 提交订单
            driver.find_element_by_link_text("提交订单").click()
            driver.find_element_by_link_text("确认").click()
    except:
        pass

3.自动爬取yemi页面中的邮箱

import re
from selenium import webdriver

driver=webdriver.Firefox()
driver.get("http://home.baidu.com/home/index/contact_us")
doc=driver.page_source  #查看页面的源代码
emails=re.findall(r'[\w]+@[\w\.-]+',doc) #正则表达式匹配邮箱(字符串前加r表示原生字符串,\w表示匹配字母数字及下划线,findall返回的是一个匹配字符串的列表 )
#循环打印匹配的邮箱
for email in emails:
    print(email)


2020 jeepxie.net webmaster#jeepxie.net
10 q. 0.009 s.
京ICP备10005923号