#!/usr/bin/env python
# -*- coding:utf-8 -*- from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.select import Select import time,os
import random # about:addons 火狐浏览器安装组件,访问的地址 # <input id="kw" name="wd" class="s_ipt" value="" maxlength="255" autocomplete="off">
#id
keys="测试部落"
delay=3
url="https://www.baidu.com/"
# url="https://mail.163.com/"
driver=webdriver.Firefox() driver.get(url)
#按关键字搜索后,发现结果页面有共同元素<div class="result c-container "
# <div class="result c-container " id="5" srcid="1599" tpl="se_com_default"
# data-click="{&quot;rsv_bdr&quot;:&quot;0&quot;,&quot;p5&quot;:5}">
# <h3 class="t">
# <a data-click="{
# 'F':'778317EA',
# 'F1':'9D73F1E4',
# 'F2':'4CA6DE6B',
# 'F3':'54E5343F',
# 'T':'1543897223',
# 'y':'777DD28E'
#
# }"href="http://www.baidu.com/link?url=...." target="_blank"><em>selenium自动化</em>测试实战 - Trinity - CSDN博客</a>
# </h3> driver.find_element_by_id("kw").send_keys(keys) #点击搜索按钮提交
driver.find_element_by_id("su").click() #获取符合条件的结果的超链接
# href_list=driver.find_elements_by_css_selector("div[tpl='se_com_default']>h3>a")
href_list=driver.find_elements_by_css_selector("div[srcid='1599']>h3>a") #获取元素属性信息 网页标题 标签名 ID 网页尺寸 位置和尺寸 位置
print(href_list[1].text)
print(href_list[1].tag_name)
print(href_list[1].id)
print(href_list[1].size)
print(href_list[1].rect)
print(href_list[1].location) #结果连接个数
urllen=len(href_list)
print(urllen) #随机值
rand=random.randint(0,urllen) url=href_list[1].get_attribute('href')
print(url)
driver.get(url) #随机点击查询出的超连接页面
href_list[rand].click() #iframe切换
# <iframe name="" id="x-URS-iframe1543906125221.7507" scrolling="no"
# style="width: 100%; height: 100%; border: medium none; background: rgba(0, 0, 0, 0) none repeat scroll 0% 0%;"
# src="https://dl.reg.163.com/webzj/v1.0.1/pub/index_dl2_new.html?cd=https%3A%2F%2Fmimg.127.net%2Findex%2F163%2Fscripts%2F2017%2Fpc%2Fcss%2F&amp;" \
# "cf=urs.7ac8b88e.css&amp;MGID=1543906125221.7507&amp;wdaId=&amp;pkid=CvViHzl&amp;product=mail163" frameborder="0"></iframe> #最长30秒
driver.implicitly_wait(2) #默认可通过iframe的ID name定位 由于iframe的id后面有附加随机数,所以不成功,但大致处理方式就是这样
driver.switch_to.frame("x-URS-iframe")
driver.find_element_by_name("name").send_keys("test")
driver.find_element_by_name("password").send_keys("123456") #回到主页面上
driver.switch_to.default_content() #百度-设置-搜索页面-全部语言-每页多少条
# <select name="NR" id="nr">
# <option value="10" selected="">每页显示10条</option>
# <option value="20">每页显示20条</option>
# <option value="50">每页显示50条</option>
# </select> #下拉框操作
mouse=driver.find_element_by_link_text("设置")
ActionChains(driver).move_to_element(mouse).perform()
driver.find_element_by_link_text("搜索设置").click() #分步骤定位
select=driver.find_element_by_id("nr")
select.find_element_by_xpath("//option[@value='20']").click() #直接定位
driver.find_element_by_xpath(".//*[@id='nr']/option[2]").click() #通过索引定位 从0开始
Select(select).select_by_index(1) #通过value定位
Select(select).select_by_value('20') #通过选项的内容定位
Select(select).select_by_visible_text('每页显示20条') #第一个选项
Select(select).first_selected_option #所有选项
Select(select).all_selected_options #取消所有选项的选择
Select(select).deselect_all() time.sleep(delay)
driver.quit()

Python3+Selenium3+webdriver学习笔记7(选择多链接的结果、iframe、下拉框)的更多相关文章

  1. Python3+Selenium3+webdriver学习笔记9(发送富文本信息及上传文件处理)

    #!/usr/bin/env python# -*- coding:utf-8 -*-'''Selenium3+webdriver学习笔记9(发送富文本信息及上传文件处理)'''from seleni ...

  2. Python3+Selenium3+webdriver学习笔记8(单选、复选框、弹窗处理)

    #!/usr/bin/env python# -*- coding:utf-8 -*-'''Selenium3+webdriver学习笔记8(单选.复选框.弹窗处理)''' from selenium ...

  3. Python3+Selenium3+webdriver学习笔记14(等待判断 鼠标事件 )

    !/usr/bin/env python -*- coding:utf-8 -*-'''Selenium3+webdriver学习笔记14(等待判断 鼠标事件 )'''from selenium im ...

  4. Python3+Selenium3+webdriver学习笔记13(js操作应用:弹出框无效如何处理)

    #!/usr/bin/env python# -*- coding:utf-8 -*-'''Selenium3+webdriver学习笔记13(js操作应用:弹出框无效如何处理)'''from sel ...

  5. Python3+Selenium3+webdriver学习笔记12(js操作应用:滚动条 日历 内嵌div)

    #!/usr/bin/env python# -*- coding:utf-8 -*-'''Selenium3+webdriver学习笔记12(js操作应用:滚动条 日历 内嵌div)'''from ...

  6. Python3+Selenium3+webdriver学习笔记11(cookie处理)

    #!/usr/bin/env python# -*- coding:utf-8 -*-'''Selenium3+webdriver学习笔记11(cookie处理)'''from selenium im ...

  7. Python3+Selenium3+webdriver学习笔记10(元素属性、页面源码)

    #!/usr/bin/env python# -*- coding:utf-8 -*-'''Selenium3+webdriver学习笔记10(元素属性.页面源码)'''from selenium i ...

  8. Python3+Selenium3+webdriver学习笔记6(多窗口切换处理)

    #!/usr/bin/env python# -*- coding:utf-8 -*- from selenium import webdriverfrom selenium.webdriver.co ...

  9. Python3+Selenium3+webdriver学习笔记5(模拟常用键盘和鼠标事件)

    #!/usr/bin/env python# -*- coding:utf-8 -*- from selenium import webdriverfrom selenium.webdriver.co ...

随机推荐

  1. struts2+jquery+easyui+datagrid+j…

    一.概述 struts2提供了针对json的插件支持.常规来讲我们将如何将对象数组转成json对象在客户端直接调用呢?尤其和jquery的easyui插件配合使用,这个可能会有很多的问题需要我们解决. ...

  2. Laravel框架接入短信平台进行用户注册短信验证

    今天刚接触了一个短信接口平台,云通讯第三方短信提供服务商.http://www.yuntongxun.com/ 然后介绍一下怎么使用该短信平台来接入到自己的项目中. 首先你的去注册一个账号,然后根据提 ...

  3. MVC4 @helper辅助方法

    Razor提供了一种很方便的语法,可以将view页面中部分内容或部分代码抽取出来,变成一个独立的辅助方法.   eg1: @foreach(var item in Model){ <标签tr&g ...

  4. 【Java】NIO中Channel的注册源码分析

    Channel的注册是在SelectableChannel中定义的: public abstract SelectionKey register(Selector sel, int ops, Obje ...

  5. How to use unity CreateExternalTexture on Android?

    http://stackoverflow.com/questions/33324753/how-to-use-unity-createexternaltexture-on-android Can so ...

  6. php如何判断文件是否存在,包括本地和远程文件

    当检查的文件是本地时用PHP自带的file_exists检查就行了,而此函数只能检查本地的函数是否存在, 所以如果要检查远程的文件是否存在只能用其它的方法了. 如果所服务器中php的配置开启了“all ...

  7. 洛谷P1976 鸡蛋饼(Catalan数)

    P1976 鸡蛋饼 题目背景 Czyzoiers 都想知道小 x 为什么对鸡蛋饼情有独钟.经过一番逼问,小 x 道出 了实情:因为他喜欢圆. 题目描述 最近小 x 又发现了一个关于圆的有趣的问题:在圆 ...

  8. SpringBoot2.0 基础案例(14):基于Yml配置方式,实现文件上传逻辑

    本文源码 GitHub地址:知了一笑 https://github.com/cicadasmile/spring-boot-base 一.文件上传 文件上传是项目开发中一个很常用的功能,常见的如头像上 ...

  9. JS高级学习历程-11

    [继承] 在php,一个类去继承另一个类,本类实例化出来的对象,既可以调用本身类的成员,也可以调用父类的成员. 在javascript继承主要通过原型实现,构造函数继承一个对象,构造函数的实例会拥有被 ...

  10. [设计模式]JDK中的设计模式

    转载自:http://blog.csdn.net/gtuu0123/article/details/6114197 本文主要是归纳了JDK中所包含的设计模式,包括作用和其设计类图. 首先来个总结,具体 ...