打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
【Selenium03篇】python+selenium实现Web自动化:元素三类等待,多窗口切换,警告框处理,下拉框选择

一、前言

最近问我自动化的人确实有点多,个人突发奇想:想从0开始讲解python+selenium实现Web自动化测试,请关注博客持续更新!

这是python+selenium实现Web自动化第三篇博文

二、Selenium成套博文地址,总有你需要的:

【Selenium篇01】python+selenium实现Web自动化:搭建环境,Selenium原理,定位元素以及浏览器常规操作!

http://www.360doc.com/showweb/0/0/906255588.aspx

【Selenium02篇】python+selenium实现Web自动化:鼠标操作和键盘操作!

http://www.360doc.com/showweb/0/0/906258176.aspx

【Selenium03篇】python+selenium实现Web自动化:元素三类等待,多窗口切换,警告框处理,下拉框选择

http://www.360doc.com/showweb/0/0/906262739.aspx

【Selenium04篇】python+selenium实现Web自动化:文件上传,Cookie操作,调用 JavaScript,窗口截图

http://www.360doc.com/content/20/0415/19/69336923_906263765.shtml

【Selenium05篇】python+selenium实现Web自动化:读取ini配置文件,元素封装,代码封装,异常处理,兼容多浏览器执行

http://www.360doc.com/showweb/0/0/906618131.aspx

【Selenium06篇】python+selenium实现Web自动化:日志处理

http://www.360doc.com/showweb/0/0/906618633.aspx

【Selenium07篇】python+selenium实现Web自动化:PO模型,PageObject模式!

http://www.360doc.com/showweb/0/0/906619184.aspx

【Selenium08篇】python+selenium实现Web自动化:数据驱动框架,ddt,关键字驱动

http://www.360doc.com/showweb/0/0/906619813.aspx

三、Selenium之-元素等待

参考博文:自动化测试中的三类等待深入剥析

https://blog.csdn.net/pengjiangchun/article/details/105509318

四、Selenium之-多窗口切换

在页面操作过程中有时候点击某个链接会弹出新的窗口,这时就需要主机切换到新打开的窗口上进行操作。webdriver 中的 switch_to.window() 方法,可以实现在不同窗口之间切换。

current_window_handle:获得当前窗口句柄
window_handles:返回所有窗口的句柄到当前会话
switc_to.window():用于切换到相应的窗口

#!/usr/bin/env python

# -*- encoding: utf-8 -*-

"""

@Time    :   2020/4/15

@License :   (C)Copyright 2017-2019, Micro-Circle

@Desc    :   None

"""

from selenium import webdriver

import time

base_url = 'https://www.baidu.com/'

browser = webdriver.Chrome('../tools/chromedriver.exe')

# 隐式等待10秒

browser.implicitly_wait(10)

browser.get(base_url)

# 获得搜索窗口的句柄

search_windows = browser.current_window_handle

browser.find_element_by_link_text('登录').click()

browser.find_element_by_link_text('立即注册').click()

# 活得当前打开窗口的句柄

all_handles = browser.window_handles

# 进入注册窗口

for handle in all_handles:

    if handle != search_windows:

        browser.switch_to.window(handle)

        print('now register window!')

        browser.find_element_by_name('account').send_keys('username')

        browser.find_element_by_name('password').send_keys('password')

        time.sleep(2)

browser.quit()

五、Selenium之-警告框处理

在 webdriver 中处理 JavaScript 所生成的 alert、confirm 以及 prompt 十分简单,具体做法是使用 switch_to.alert 方法定位到 alert/confirm/prompt,然后使用 text/accept/dismiss/ send_keys 等方法进行操作。

text:返回 alert/confirm/prompt 中的文字信息。
accept():接受现有警告框。
dismiss():解散现有警告框。
send_keys(keysToSend):发送文本至警告框。keysToSend:将文本发送至警告框。

#!/usr/bin/env python

# -*- encoding: utf-8 -*-

"""

@Time    :   2020/4/15

@License :   (C)Copyright 2017-2019, Micro-Circle

@Desc    :   None

"""

from selenium import webdriver

from selenium.webdriver.common.action_chains import ActionChains

import time

base_url = 'https://www.baidu.com/'

driver = webdriver.Chrome('../tools/chromedriver.exe')

driver.implicitly_wait(10)

driver.get(base_url)

# 鼠标悬停至 “设置” 链接

link = driver.find_element_by_link_text('设置')

ActionChains(driver).move_to_element(link).perform()

# 打开搜索设置

driver.find_element_by_link_text('搜索设置').click()

time.sleep(3)

# 点击 “搜索设置”

driver.find_element_by_class_name('prefpanelgo').click()

time.sleep(3)

# 接受警告框prefpanelgo

driver.switch_to.alert.accept()

time.sleep(3)

driver.quit()

六、Selenium之-下拉框处理

?webdriver 提供了 Select 类来处理下拉框。

#!/usr/bin/env python

# -*- encoding: utf-8 -*-

"""

@Time    :   2020/4/15

@License :   (C)Copyright 2017-2019, Micro-Circle

@Desc    :   None

"""

from selenium import webdriver

from selenium.webdriver.support.select import Select

from time import sleep

base_url = 'https://www.baidu.com/'

driver = webdriver.Chrome('../tools/chromedriver.exe')

driver.implicitly_wait(10)

driver.get(base_url)

# 鼠标悬停至“设置”链接

driver.find_element_by_name('设置').click()

sleep(2)

# 打开 “搜索设置”

driver.find_element_by_name('搜索设置').click()

sleep(2)

# 搜索结果显示条数

# Select类用于定位select标签。

sel = driver.find_element_by_xpath("//select[@id='nr']")

# select_by_value() 方法用于定位下接选项中的value值。

Select(sel).select_by_value('50')

driver.quit()

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
python+selenium+chrome实现自动登录百度
Selenium 怎么实现一次登陆,在不关闭浏览器的情况下多次调试呢?
Selenium2+python自动化69-PhantomJS使用
Selenium2+python自动化23
Selenium2 python自动化33-文件上传(send_keys)
聊聊 PC 端自动化最佳方案: WinAppDriver
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服