python_selenium(三)
来源:互联网 发布:网络黑白txt百度云盘 编辑:程序博客网 时间:2024/05/19 01:09
实现思路
- 需要引入ActionChains类
- 然后定位相关元素
- 在ActionChains().调用相关鼠标操作方法
from selenium import webdriverfrom selenium.webdriver.common.action_chains import ActionChainsfrom time import sleepdriver = webdriver.Chrome()driver.get("http://www.baidu.com")element_id_kw = driver.find_element_by_id("kw")element_id_kw.send_keys("python")sleep(2)#双击告诉那个电脑那个文本框双击ActionChains(driver).double_click(element_id_kw).perform()#右键ActionChains(driver).context_click(element_id_kw).perform()sleep(2)#鼠标悬停element_name_t = driver.find_element_by_name("tj_settingicon")ActionChains(driver).move_to_element(element_name_t).perform()
键盘操作
案例:在百度搜索关键词“Python” 然后将关键词复制或剪切到搜狗搜索框进行搜索
from selenium import webdriverfrom selenium.webdriver.common.keys import Keysfrom time import sleepdriver = webdriver.Chrome()driver.get("http://www.baidu.com")#在输入框输入内容driver.find_element_by_css_selector("#kw").send_keys("把内容剪切")sleep(1)#启用键盘ctrl+adriver.find_element_by_css_selector("#kw").send_keys(Keys.CONTROL,"a")sleep(1)#启用键盘ctrl+cdriver.find_element_by_css_selector("#kw").send_keys(Keys.CONTROL,"x")sleep(1)#启用键盘打开搜狗ctrl+v 黏贴内容driver.get("http://www.sogou.com/")driver.find_element_by_css_selector(".sec-input").send_keys(Keys.CONTROL,"v")
显示等待
案例:检测百度页面搜索按钮是否存在,存在就输入关键词“自学网 Selenium” 然后点击搜索
from selenium import webdriverfrom selenium.webdriver.support.ui import WebDriverWaitfrom selenium.webdriver.support import expected_conditions as ESfrom selenium.webdriver.common.by import Byfrom time import sleepdriver = webdriver.Chrome()driver.get("http://www.baidu.com")#输入实验driver.find_element_by_id("kw").send_keys("实验")#等待五秒,直到搜索到ID 为su 属性element = WebDriverWait(driver,5,0.5).until(ES.presence_of_element_located((By.ID,'su')))element.click()
隐式等待:
from selenium import webdriverfrom selenium.common.exceptions import NoSuchElementExceptionfrom time import sleep,ctimedriver =webdriver.Chrome()driver.get("http://www.baidu.com")sleep(2)driver.implicitly_wait(5)try: print(ctime()) driver.find_element_by_css_selector("#kw").send_keys("Python") driver.find_element_by_css_selector("#su").click()except NoSuchElementException as msg : print(msg)finally: print(ctime())
阅读全文
0 0
- python_selenium(三)
- python_selenium
- python_selenium(二)
- python_selenium(sn)
- python_selenium(五)
- python_selenium笔记
- python_selenium单元测试+自动生成测试结果
- 使用python_selenium爬取动态网页时要注意的问题
- (三)
- (三)
- 三消游戏(三)
- IT之禅(三三)
- 三 序列式容器(三)deque
- 第九周项目三(三)
- 第十五周项目三:OJ(三)
- 三、runtime之消息(三)
- Hibernate(三):三种实例状态
- 三栏布局(三列布局)
- 关于springmvc+freemarker问题的整理
- Spring aop 一些问题
- PAT甲级 1033
- 《Android源码设计模式》之单例模式
- S3C6410核心初始化
- python_selenium(三)
- 第10章 Executor框架
- PAT甲级 1034 -- 没有注释
- PAT(Basic Level)_1002_写出这个数
- (专业写BUG系列)Foreach的只读性和List<T>的引用传递
- 获得随机数
- python求解LeetCode习题Find Peak Element in Given num_list
- 无线网卡在ubuntu 14.04 下频繁掉线 信号弱 解决办法
- Ubuntu 16.04+ROS+ORB-SLAM2配置以及相关问题