首页 > 其他分享 >ui自动化selenium

ui自动化selenium

时间:2022-10-14 16:35:09浏览次数:61  
标签:selenium driver element switch ui 自动化 id find

1:环境搭建   pip install -i 加速器镜像地址 包名 # 使用加速器镜像地址下载安装第3方包   pip install -i https://pypi.tuna.tsinghua.edu.cn/simple selenium 2:不同浏览器对应的驱动: (1)Chrome http://npm.taobao.org/mirrors/chromedriver (2)Firefox https://github.com/mozilla/geckodriver/releases/ (3)IE http://selenium-release.storage.googleapis.com/index.html (4)Edge https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver 3:八大元素定位: dr.find_element_by_name("name属性") —— 根据标签上的name属性 dr.find_element_by_id("id属性") —— 根据标签上的id属性 dr.find_element_by_class_name("类样式名") —— 根据标签上的class属性中的类样式 名 dr.find_element_by_tag_name("标签名") —— 根据标签名 dr.find_element_by_link_text("链接文本") —— 根据超链接的文本 只对超链接标签 <a> 有用 dr = WebDriver()dr.find_element_by_partial_link_text("部分链接文本") —— 根据超链接的部分文本 只对超链接标签 <a> 有用 dr.find_element_by_css_selector("CSS选择器") —— 根据CSS选择器 dr.find_element_by_xpath("XPATH表达式") —— 根据XPATH表达式

 

 

 4:鼠标控制代码示例:

from selenium.webdriver.common.action_chains import ActionChains

# 定位搜索按钮
button = driver.find_element_by_xpath('//*[@id="toolbar-search-button"]/span')
# 右键搜索按钮
ActionChains(driver).context_click(button).perform()

窗口切换

在 selenium 操作页面的时候,可能会因为点击某个链接而跳转到一个新的页面(打开了一个新标签页),这时候 selenium 实际还是处于上一个页面的,需要我们进行切换才能够定位最新页面上的元素。

  相关语句:current_window)handle==>当前窗口句柄

          window_handles==>所有窗口句柄,返回一个列表

          switch_to.window(i)去某个窗口

示例代码:  

from selenium import webdriver

handles = []
driver = webdriver.Chrome()
driver.get('https://blog.csdn.net/')
# 设置隐式等待
driver.implicitly_wait(3)
# 获取当前窗口的句柄
handles.append(driver.current_window_handle)
# 点击 python,进入分类页面
driver.find_element_by_xpath('//*[@id="mainContent"]/aside/div[1]/div').click()
# 切换窗口
driver.switch_to.window(driver.window_handles[-1])
# 获取当前窗口的句柄
handles.append(driver.current_window_handle)

print(handles)
print(driver.window_handles)

表单切换

很多页面也会用带 frame/iframe 表单嵌套,对于这种内嵌的页面 selenium 是无法直接定位的,需要使用 switch_to.frame() 方法将当前操作的对象切换成 frame/iframe 内嵌的页面。

switch_to.frame() 默认可以用的 id 或 name 属性直接定位,但如果 iframe 没有 id 或 name ,这时就需要使用 xpath 进行定位。下面先写一个包含 iframe 的页面做测试用。

switch_to.frame(属性)==》可以为id和name

switch_to.default_contest()==>去默认框架

 

实例代码:

from selenium import webdriver
from pathlib import Path


driver = webdriver.Chrome()
# 读取本地html文件
driver.get('file:///' + str(Path(Path.cwd(), 'iframe测试.html')))

# 1.通过id定位
driver.switch_to.frame('CSDN_info')
# 2.通过name定位
# driver.switch_to.frame('Dream丶Killer')
# 通过xpath定位
# 3.iframe_label = driver.find_element_by_xpath('/html/body/iframe')
# driver.switch_to.frame(iframe_label)

driver.find_element_by_xpath('//*[@id="csdn-toolbar"]/div/div/div[1]/div/a/img').click()

弹窗处理

JavaScript 有三种弹窗 alert(确认)、confirm(确认、取消)、prompt(文本框、确认、取消)。

处理方式:先定位(switch_to.alert自动获取当前弹窗),再使用 text、accept、dismiss、send_keys 等方法进行操作

实例代码:

from selenium import webdriver
from pathlib import Path
from time import sleep


driver = webdriver.Firefox()
driver.get('file:///' + str(Path(Path.cwd(), '弹窗.html')))

sleep(2)

# 点击alert按钮
driver.find_element_by_xpath('//*[@id="alert"]').click()
sleep(1)
alert = driver.switch_to.alert
# 打印alert弹窗的文本
print(alert.text)
# 确认
alert.accept()

sleep(2)

# 点击confirm按钮
driver.find_element_by_xpath('//*[@id="confirm"]').click()
sleep(1)
confirm = driver.switch_to.alert
print(confirm.text)
# 取消
confirm.dismiss()

sleep(2)

# 点击prompt按钮
driver.find_element_by_xpath('//*[@id="prompt"]').click()
sleep(1)
prompt = driver.switch_to.alert
print(prompt.text)
# 向prompt的输入框中传入文本
prompt.send_keys("Dream丶Killer")
sleep(2)
prompt.accept()

 

标签:selenium,driver,element,switch,ui,自动化,id,find
From: https://www.cnblogs.com/lwcx/p/16791987.html

相关文章

  • Playwright 和Selenium
    背景想知道如何在Playwright和Selenium之间进行选择以实现测试自动化?继续阅读以查看两种流行的测试自动化工具之间的比较。在Web测试自动化方面,Selenium多年来一......
  • Python数据分析,办公自动化,数据选取
    一张表中通常会包含很多字段,造成数据冗余,在做数据分析时,我们仅需要提取数据分析所需要的字段,这里就需要用到数据选取的知识点。本文构建数据表做数据索引,然后对数据内容进行......
  • Python数据分析,办公自动化,数据拼接与连接
    日常数据分析中,需要将多个表进行合并,包括表的纵向拼接以及表的横向连接,将多个小表组合成为一张大表,使用的方法有Excel中的Vlookup函数法,SQL中UNION和JOIN的方法,以及Pandas模......
  • Python数据分析,办公自动化,数据分组与透视
    数据分组是对相同类别的数据进行汇总,而数据透视表是通过对行或列的不同组合对数据进行汇总,所使用的汇总方法有求和、计数、平均值、标准差等,本文使用Python对数据进行数据分......
  • Python数据分析,办公自动化,数据预处理
    数据分析时,首先应对数据进行清洗,这里将数据清洗分为重复值处理、缺失值处理、异常值处理三个部分,重复值处理可删除重复的字段,缺失值处理可以用线性插值、填充为0或用均值填......
  • selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executabl
    在使用selenium模块操作浏览器时,出现下面的错误提示:selenium.common.exceptions.WebDriverException:Message:‘chromedriver’executableneedstobeinPATH.Please......
  • UIHINT属性的用途是什么?
    MVC中UIHint属性的用途。我们为什么需要这个。以及何时和如何使用?UIHintAttribute指定动态数据用来显示数据字段的模板或用户控件。这是UIHintAttribute的MSDN描述。......
  • Unity 在模型上展示标签UI
    voidUpdate(){this.transform.LookAt(Camera.main.transform);}   在主摄像机下设置一个子摄像机,主摄像机CullingMask去掉显示UI。子摄像机Cul......
  • 从零开始配置vim(26)——LSP UI 美化
    之前我们通过几个实例演示如何配置其他语言的lsp服务,相信各位小伙伴碰到其他的编程语言也能熟练的配置它对应的lsp服务。本篇讲作为一个补充,我们来优化一下LSP相关的显示......
  • docker + xrfb-run + vnc:Docker里开虚拟屏幕运行带GUI程序
    进入一个平平无奇的ubuntu容器dockerrun-it--rm-p5900:5900ubunut:20.04安装X组件aptinstall-yxserver-xorgaptinstallxvfbaptinstallx11vnc以上,就安装完成......