【第03节】Selenium4 页面下拉框与鼠标操作实战(Python Web自动化测试)

1、下拉框元素定位实战

重点:定位下拉框元素方法有3种:value 、 text 、index 方法。如何区分页面 visible_text 、index 、value 属性值。

在这里插入图片描述

1.1 使用 value 属性定位下拉框

from selenium import webdriver
import time
from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select

# 打开Chrome浏览器
driver = webdriver.Chrome()
# 请求微软注册界面
driver.get(
    'https://signup.live.com/signup?lcid=1033&wa=wsignin1.0&rpsnv=13&ct=1667977589&rver=7.0.6737.0&wp=MBI_SSL&wreply=https%3a%2f%2foutlook.live.com%2fowa%2f%3fnlp%3d1%26signup%3d1%26RpsCsrfState%3d03e6f63b-7b44-e3a7-1cec-67445341a6de&id=292841&CBCXT=out&lw=1&fl=dob%2cflname%2cwld&cobrandid=90015&lic=1&uaid=5cd6f6e2004b4d5f96bcb3f20e670a07')
#  等待时间,让浏览器加载一会
time.sleep(3)
driver.find_element(By.ID, 'iSignupAction').click()
driver.find_element(By.ID, 'MemberName').send_keys('testing10086')
# 获取下拉列,并搜索value值填数据
# Select( driver.find_element(By.ID,'LiveDomainBoxList')).select_by_value('hotmail.com')
# 为了看清效果 在操作一次下拉框其他的值
# Select(driver.find_element(By.ID ,'LiveDomainBoxList')).select_by_value('outlook.com')

"""
上面代码适合调试使用,项目中常用下面写法,为了减少重复代码。
"""
elemnet = driver.find_element(By.ID, 'LiveDomainBoxList')
# 使用  value 属性定位下拉框
Select(elemnet).select_by_value('hotmail.com')
time.sleep(1)
Select(elemnet).select_by_value('outlook.com')
time.sleep(3)
# driver.close()关闭驱动,driver.quit()退出驱动,开发中一般建议使用driver.quit()
driver.quit()

1.2 使用 index 属性定位下拉框

from selenium import webdriver
import time
from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select

# 打开Chrome浏览器
driver = webdriver.Chrome()
# 请求微软注册界面
driver.get(
    'https://signup.live.com/signup?lcid=1033&wa=wsignin1.0&rpsnv=13&ct=1667977589&rver=7.0.6737.0&wp=MBI_SSL&wreply=https%3a%2f%2foutlook.live.com%2fowa%2f%3fnlp%3d1%26signup%3d1%26RpsCsrfState%3d03e6f63b-7b44-e3a7-1cec-67445341a6de&id=292841&CBCXT=out&lw=1&fl=dob%2cflname%2cwld&cobrandid=90015&lic=1&uaid=5cd6f6e2004b4d5f96bcb3f20e670a07')
#  等待时间,让浏览器加载一会
time.sleep(3)
driver.find_element(By.ID, 'iSignupAction').click()
driver.find_element(By.ID, 'MemberName').send_keys('testing10086')

"""
项目中常用写法,为了减少重复代码。
"""
elemnet = driver.find_element(By.ID, 'LiveDomainBoxList')
# 使用  index 属性定位下拉框
Select(elemnet).select_by_index(1)
time.sleep(2)
Select(elemnet).select_by_index(0)
time.sleep(3)
# driver.close()关闭驱动,driver.quit()退出驱动,开发中一般建议使用driver.quit()
driver.quit()

划重点:index 属性定位,是使用的索引,索引默认是从0开始的。请从1开始,否则会导致数据不正确。

1.3 使用 visible_text 属性定位下拉框

from selenium import webdriver
import time
from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select

# 打开Chrome浏览器
driver = webdriver.Chrome()
# 请求微软注册界面
driver.get(
    'https://signup.live.com/signup?lcid=1033&wa=wsignin1.0&rpsnv=13&ct=1667977589&rver=7.0.6737.0&wp=MBI_SSL&wreply=https%3a%2f%2foutlook.live.com%2fowa%2f%3fnlp%3d1%26signup%3d1%26RpsCsrfState%3d03e6f63b-7b44-e3a7-1cec-67445341a6de&id=292841&CBCXT=out&lw=1&fl=dob%2cflname%2cwld&cobrandid=90015&lic=1&uaid=5cd6f6e2004b4d5f96bcb3f20e670a07')
#  等待时间,让浏览器加载一会
time.sleep(3)
driver.find_element(By.ID, 'iSignupAction').click()
driver.find_element(By.ID, 'MemberName').send_keys('testing10086')

"""
项目中常用写法,为了减少重复代码。
"""
elemnet = driver.find_element(By.ID, 'LiveDomainBoxList')
# 使用  visible_text 属性定位下拉框
Select(elemnet).select_by_visible_text('hotmail.com')
time.sleep(2)
Select(elemnet).select_by_visible_text('outlook.com')
time.sleep(3)
# driver.close()关闭驱动,driver.quit()退出驱动,开发中一般建议使用driver.quit()
driver.quit()

2、鼠标操作实战

2.1 鼠标指针悬停实战

引入 ActionChains 包,使用 move_to_element 方法

from selenium import webdriver
import time
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains

# 打开Chrome浏览器
driver = webdriver.Chrome()
driver.maximize_window()
driver.implicitly_wait(10)
# 请求 CSDN
driver.get("http://csdn.net/")
# 使用 link_text 定位
link= driver.find_element(By.LINK_TEXT,'移动开发')
ActionChains(driver).move_to_element(link).perform()
time.sleep(2)
# 使用 link_text 定位
driver.find_element(By.LINK_TEXT,'测试').click()
time.sleep(3)
# driver.close()关闭驱动,driver.quit()退出驱动,开发中一般建议使用driver.quit()
driver.quit()

2.2 鼠标右键实战

引入 ActionChains 包,使用 context_click 方法

from selenium import webdriver
import time
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains

# 打开Chrome浏览器
driver = webdriver.Chrome()
driver.maximize_window()
driver.implicitly_wait(10)
# 请求 百度
driver.get("https://www.baidu.com")
# 获取搜索按钮元素
context = driver.find_element(By.ID,'su')
# 点击鼠标右击
ActionChains(driver).context_click(context).perform()
# driver.close()关闭驱动,driver.quit()退出驱动,开发中一般建议使用driver.quit()
time.sleep(3)
driver.quit()

2.3 鼠标左键实战

引入 ActionChains 包,使用 double_click 方法

from selenium import webdriver
from time import sleep
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains

# 打开Chrome浏览器
driver = webdriver.Chrome()
driver.maximize_window()
driver.implicitly_wait(10)
# 请求 百度
driver.get("https://www.baidu.com")
# 输入需要搜索的数据
driver.find_element(By.ID,'kw').send_keys("Python Web自动化测试")
# 获取百度一下按钮元素
sleep(3)
driver.find_element(By.ID,'kw').send_keys("追加数据看点击效果!")
double= driver.find_element(By.ID,'su')
# 点击双击鼠标
ActionChains(driver).double_click(double).perform()
# driver.close()关闭驱动,driver.quit()退出驱动,开发中一般建议使用driver.quit()
sleep(3)
driver.quit()

注意:鼠标事假有很多种,我们常用的是,鼠标的点击与悬浮事件较多。

3、附加:定位知识点(元素二次与多次定位)

元素二次与多次定位方法是用来干什么的,是为了解决元素的精准定位问题。

from selenium import webdriver
import time
from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select

# 打开Chrome浏览器
driver = webdriver.Chrome()
# 请求微软注册界面
driver.get(
    'https://signup.live.com/signup?lcid=1033&wa=wsignin1.0&rpsnv=13&ct=1667977589&rver=7.0.6737.0&wp=MBI_SSL&wreply=https%3a%2f%2foutlook.live.com%2fowa%2f%3fnlp%3d1%26signup%3d1%26RpsCsrfState%3d03e6f63b-7b44-e3a7-1cec-67445341a6de&id=292841&CBCXT=out&lw=1&fl=dob%2cflname%2cwld&cobrandid=90015&lic=1&uaid=5cd6f6e2004b4d5f96bcb3f20e670a07')
#  等待时间,让浏览器加载一会
time.sleep(3)
driver.find_element(By.ID, 'iSignupAction').click()
driver.find_element(By.ID, 'MemberName').send_keys('testing10086')

"""
元素二次与多次定位 
elemnet = driver.find_element(By.ID, 'LiveDomainBoxList').find_element(By.XPATH,"//*[@name=‘LiveDomainBoxList’]").find_element(By.NAME,'xxxx').find_elements(By.CSS_SELECTOR,'xxxx')
"""
elemnet = driver.find_element(By.ID, 'LiveDomainBoxList').find_element(By.XPATH,"//*[@name=‘LiveDomainBoxList’]")
Select(elemnet).select_by_visible_text('hotmail.com')
# driver.close()关闭驱动,driver.quit()退出驱动,开发中一般建议使用driver.quit()
driver.quit()
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一禅(OneZen)

你的鼓励将是我创作的最大动力哦

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值