selenium实战

1.多页面跳转

案例描述:百度搜索bing,bing搜索selenium,使用自动化脚本测试,利用闭包实现无线循环,如果错误就输出程序运行失败,不结束程序运行。

import time
from selenium import webdriver
import datetime
from selenium.webdriver.common.keys import Keys
from selenium.webdriver import ActionChains


def func(i):
    try:
        start_time = datetime.datetime.now()
        print("第%d次开始时间:" % i, start_time)
        driver = webdriver.Firefox()
        ActionChains(driver)
        data = driver.get(" http://www.baidu.com")
        element = driver.find_element_by_xpath(".//*[@id='kw']")
        element.click()
        element.send_keys("bing")
        element.send_keys(Keys.ENTER)
        time.sleep(2)
        ele = driver.find_element_by_xpath(".//*[@id='1']/h3/a[1]")
        ActionChains(driver).double_click(ele).perform()
        time.sleep(5)
        handles = driver.window_handles
        driver.switch_to.window(handles[-1])
        aa = driver.find_element_by_css_selector("#sb_form_q")
        aa.send_keys("selenium")
        aa.send_keys(Keys.ENTER)
        abc = driver.find_element_by_xpath(".//*[@id='b_results']/li[3]/div[1]/h2/a")
        ActionChains(driver).double_click(abc).perform()
        time.sleep(5)
        print(5)
        time.sleep(1)
        print(4)
        time.sleep(1)
        print("……")
        time.sleep(3)
        print(1)
        driver.close()
        end_time = datetime.datetime.now()
        print("第%d次结束时间:" % i, end_time)
        time_cha = end_time - start_time
        print("第%d次时间差:" % i, time_cha)
        i += 1
    except Exception as e:
        # raise e
        print("程序运行失败")
    func(i)


func(1)

2. qq 空间登录(frame切换)

from selenium import webdriver
import time

driver = webdriver.Firefox()
driver.get("https://qzone.qq.com/")
time.sleep(2)
login = driver.find_element_by_id("login_frame")
driver.switch_to.frame(login)
zhmadl = driver.find_element_by_id("switcher_plogin")
zhmadl.click()
time.sleep(2)
username = driver.find_element_by_id("u")
username.click()
username.send_keys("XXXXXXX")
time.sleep(2)
password = driver.find_element_by_id("p")
password.send_keys("XXXXXXX")
time.sleep(2)
dl = driver.find_element_by_id("login_button")
dl.click()

3.淘宝京东购物筛选及添加购物车

由于淘宝出现人机验证,出现滑块验证不通过的情况,具体解决方法还有待解决。就拿京东来实现自动化购物的脚本。

from selenium import webdriver
import time

driver = webdriver.Firefox()
driver.get(
    "https://www.jd.com/?cu=true&utm_source=baidu-search&utm_medium=cpc&utm_campaign=t_262767352_baidusearch&utm_term=106807362512_0_ee521dbb631241d6a75682dc3f57a287")
driver.find_element_by_xpath(".//*[@id='J_user']/div/div[1]/div[2]/p/a[1]").click()
time.sleep(5)
headles = driver.window_handles
driver.switch_to.window(headles[-1])
driver.find_element_by_xpath(".//*[@id='content']/div[2]/div[1]/div/div[3]/a").click()
time.sleep(2)
username = driver.find_element_by_id("loginname")
username.clear()
username.send_keys("XXXXX")
password = driver.find_element_by_id("nloginpwd")
password.clear()
password.send_keys("XXXXX")
driver.find_element_by_id("loginsubmit").click()
time.sleep(9)
headles = driver.window_handles
driver.switch_to.window(headles[-1])
ssk = driver.find_element_by_id("key")
ssk.send_keys("避孕套")
# 点击搜索
driver.find_element_by_xpath(".//*[@id='search']/div/div[2]/button").click()
time.sleep(5)
# 选择邦杰士
driver.find_element_by_xpath(".//*[@id='brand-9233']/a").click()
time.sleep(5)
# 选择常规
driver.find_element_by_xpath(".//*[@id='J_selector']/div[1]/div/div[2]/div[1]/ul/li[5]/a").click()
time.sleep(5)
# 选择标准
driver.find_element_by_xpath(".//*[@id='J_selector']/div[1]/div/div[2]/div[1]/ul/li[1]/a").click()
time.sleep(5)
# 选择乳胶
driver.find_element_by_xpath(".//*[@id='J_selector']/div[2]/div/div[2]/div[1]/ul/li[1]/a").click()
time.sleep(5)
# 选择进口
driver.find_element_by_xpath(".//*[@id='J_selector']/div[2]/div/div[2]/div[1]/ul/li[2]/a").click()
time.sleep(5)
# 选择仅显示有货
driver.find_element_by_xpath(".//*[@id='J_feature']/ul/li[3]/a").click()
time.sleep(5)
# 点击图片
driver.find_element_by_xpath(".//*[@id='J_goodsList']/ul/li[1]/div/div[1]/a/img").click()
time.sleep(5)
headles = driver.window_handles
driver.switch_to.window(headles[-1])
# 添加数量
driver.find_element_by_xpath(".//*[@id='choose-btns']/div/div/a[2]").click()
time.sleep(5)
driver.find_element_by_xpath(".//*[@id='choose-btns']/div/div/a[2]").click()
time.sleep(5)
# 加入购物车
driver.find_element_by_xpath(".//*[@id='InitCartUrl']").click()
time.sleep(5)
headles = driver.window_handles
driver.switch_to.window(headles[-1])
# 显示购物车
driver.find_element_by_xpath(".//*[@id='result']/div/div/div[2]/div[3]/a[1]").click()
time.sleep(5)
headles = driver.window_handles
driver.switch_to.window(headles[-1])

4.128邮箱,163邮箱登录

因为128邮箱,163邮箱的id都是随机的,定位起来可能会有点困难,但是问题不大,我们可以使用自己写x_pass表达式定位,避免使用id即可完成操作。其他的不变。

posted @ 2021-06-15 20:07  醉魔君  阅读(268)  评论(0编辑  收藏  举报