#!/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. AngularJs(Part 4)--Modules depending on other Modules

    Angular does an excellent job of managing object dependencies. it can even take care of module depen ...

  2. Oracle tns 协议

    下面是翻译国外的一篇博客,原文连接如下: https://thesprawl.org/research/oracle-tns-protocol/ 简介 TNS(Transparent Network ...

  3. hdu 3037 费马小定理+逆元除法取模+Lucas定理

    组合数学推推推最后,推得要求C(n+m,m)%p 其中n,m小于10^9,p小于1^5 用Lucas定理求(Lucas定理求nm较大时的组合数) 因为p数据较小可以直接阶乘打表求逆元 求逆元时,由费马 ...

  4. Halcon - 图像随 HWindowControl 控件缩放的同时,保持图像的长宽比例不变

    背景 通常情况下,图像是填充满 HWindowControl 控件,并随其缩放的.此时只需要将 set_part 的参数设置成图像的大小即可. 不过,有时候,在一些测量任务中,我们对原始图像的长宽比敏 ...

  5. sqlserver2012——逻辑运算符

    ALL 如果一组的比较都为TRUE,则结果为true ANY如果玉足比较中任何一个为true,则结果为true AND 两个boll都为TRUE,则结果为TRUE OR 两个BOLL任何一个TRUE, ...

  6. WP之样式

    1.定义资源 <Window.Resources> <!--下面用样式--> <Style x:Key="BigFontButtonStyle"> ...

  7. ApplicationContext的三个常用实现类:

    ClassPathXmlApplicationContext 它可以加载类路径下的配置文件,要求配置文件必须在类路径下,不在的话加载不了 (java中获取类路径下资源的方式) FileSystemXm ...

  8. 洛谷P2905 [USACO08OPEN]农场危机Crisis on the Farm

    P2905 [USACO08OPEN]农场危机Crisis on the Farm 题目描述 约翰和他的奶牛组建了一只乐队“后街奶牛”,现在他们正在牧场里排练.奶牛们分成一堆 一堆,共1000)堆.每 ...

  9. [Xcode 实际操作]七、文件与数据-(14)数据持久化存储框架CoreData的使用:删除CoreData中的数据

    目录:[Swift]Xcode实际操作 本文将演示如何删除数据持久化对象. 在项目导航区,打开视图控制器的代码文件[ViewController.swift] import UIKit //引入数据持 ...

  10. 【工具篇】Sublime Text 2/3 安装汉化破解、插件包安装教程详解

    Sublime Text概述: Sublime Text是一个代码编辑器,也是HTML和散文先进的文本编辑器. 漂亮的用户界面和非凡的功能,例如:迷你地图,多选择,Python插件,代码段等等. 完全 ...