Python使用selenium(一)

文档路径:https://selenium-python.readthedocs.io/installation.html


以下代码讲解的是在windows系统上的操作

 

1. 使用webdriver打开火狐浏览器
from selenium import webdriver
browser =webdriver.Firefox()
selenium内部有firefox浏览器,所以可以直接打开 且对firefox的支持性最好   

firefox前端工具介绍
fireBug:Firefox浏览器下的一套开发类插件
作用:查看页面上的元素,从而根据其属性进行定位。
需要自行安装,在firefox浏览器中开发者查找安装
该工具里可以直接复制xpath,这比一个个xpath找方便多了。

 

2.使用webdriver打开chrome浏览器
from selenium import webdriver
browser=webdriver.chrome()
如果只安装了chrome浏览器,直接通过webdriver打开会报错
需要安装chrome浏览器webdriver驱动
a.安装chromedriver.exe(去网上找下载,貌似官网上没有了)
b.windows系统需要添加环境变量

chrome浏览器更方便


3.浏览器窗口的一些操作

打开一个浏览器b
b =webdriver.Firefox()

关闭浏览器

b.quit()   

关闭窗口

b.close()  

打开一个网页
url='http://www.baidu.com'
b.get(url)

当前的url
b.current_url

当前页面标题
b.title

 返回到上一页,也就是浏览器操作返回

b.back()

将窗口最大化

b.maximize_window()

 窗口全屏

b.fullscreen_window()

 

4.页面元素的定位及操作

 

通过检查页面查看元素的属性,然后确定使用何种方法查找该元素。

通过id查找到元素ele

ele=b.find_element_by_id('id1')

通过name属性查找元素

ele=b.find_element_by_name('name1')

通过ClassName查找到元素

ele1=b.find_element_by_class_name('classname')

tag name 针对的是标签名,通过tag name 查找元素

ele2=b.find_element_by_tag_name('input')

当页面有很多个同类型的标签,会返回第一个标签

通过link text来查找标签 对于a标签,通过其text

ele3=b.find_element_by_link_text(‘百度链接’)

通过模糊查询,只要text中有搜索的字样就可以查询出来

 ele4=b.find_element_by_partial_link_text(‘百度’)

通过css选择器来定位元素  当有些元素没有id  name 等一些属性可以使用

css路径在Firefox浏览器中通过安装的 fireBug查找元素中复制css路径来获取的

ele5=b.find_element_by_css_selector(‘’css路径‘’)

ele6=b.find_element_by_css_selector(' input [id=\'search \'  ] ')     css还有这种语法

ele7=b.find_element_by_css_selector(' input [type=”text ”  ] ')   可以选择任何属性

ele8=b.find_element_by_css_selector(' img [alt=”水果图片”  ] ')

 其他的css选择器用法可以网上查找,简单的直接使用相关语法,复杂的通过firebug直接拷贝

 通过xpath来查找定位元素

xpath用于在XML文档中通过元素和属性进行导航。是一个w3c标准。

xpath节点类型:

   元素,属性,文本,命名空间,指令处理,注释及文档

ele9=b.find_element_by_xpath(‘/div’)

/html/body/input[1]      绝对路径下的input元素      【1】表示同级多个input时的第一个   

//input   任意路径下的input元素   查找到所有的input元素

ele9=b.find_element_by_xpath(‘/input’)  返回第一个元素

//input[2]

//input/p

//input//p       

ele10=b.find_element_by_xpath(‘/input/..’)  ele10是ele9的父节点

//input[@id]    有id属性的input元素                                                   也可以通过其他属性查找元素

//input [  not(@id)]    没有id属性的input元素 

//input[@name=‘firstname’]     name 属性为firstname的input元素

//input[@id=‘id1’]    id等于id1的input元素

//*    所有元素

//*[ count(input)=2     包含两个input元素的元素

 //*[local-name()="input"]   找到tag为input的元素          找到多个元素时,返回的都是第一个元素

//*[starts-with(local-name(), 'i')]  找到所有tag以i开头,如input   img 标签

//*[starts-with(local-name(), 'i')] [last()]    找到所有tag以i开头,如input   img 标签  最后一个

//*[starts-with(local-name(), 'i')] [last()-1]   倒数第二个

 //title | //input     查找所有的title或者input标签

也可以通过firebug查找元素,然后复制xpath

其实,对于使用者,xpath和css选择器 哪个习惯用哪个。xpath更强大,而css选择器语法更简洁,且效率更高。

xpath性能差点,但是在浏览器中有比较好的插件支持。使用css selector跟xpath不需要安装第三方什么插件。

 

对查找到的元素操作

ele.clear()

元素的属性

ele.size

ele.id

ele.name

 ele.get_attribute('name')    获取元素的name属性的值

 ele.tag_name       元素的标签名

 

posted on 2018-12-04 11:03  yanmay  阅读(19238)  评论(0编辑  收藏  举报

导航