首页 > 技术文章 > py库: Selenium (自动化测试)

qq21270 2017-10-18 23:20 原文

 

http://blog.csdn.net/liujingqiu/article/details/50458553

http://www.cnblogs.com/zhaof/p/6953241.html  python爬虫从入门到放弃(八)之 Selenium库的使用  2017-10-18

https://www.youtube.com/watch?v=roTwhVIHkAo

 

 


 

首先需要一个浏览器供python使用:Chormedriver.exe

http://download.csdn.net/download/r455678/9713526  墙内可以这里下载2.25版本的

  • http://www.seleniumhq.org/download/                                  selenium官网  页面内查找chrome(同理,可以查找其它浏览器的driver)
  • https://sites.google.com/a/chromium.org/chromedriver/  google相关页面
  • https://chromedriver.storage.googleapis.com/index.html?path=2.35/    google下载页,下载:
  • https://chromedriver.storage.googleapis.com/2.35/chromedriver_win32.zip

下载后保存在 C:\Python3\Scripts\Chormedriver.exe

 

先来个例子: 

# -*- coding: utf-8 -*-
from selenium import webdriver
import time

browser = webdriver.Chrome()         # Chrome浏览器
#browser = webdriver.Firefox()       # Firefox浏览器
browser.get("http://www.baidu.com")
browser.find_element_by_id("kw").send_keys("自动化测试")
time.sleep(5)#休息5秒
browser.close()
browser.quit()

  

from selenium import webdriver
import time

browser = webdriver.Chrome()  # Chrome浏览器
# browser = webdriver.Firefox()       # Firefox浏览器
browser.get("https://morvanzhou.github.io")
browser.find_element_by_link_text(u"赞助").click()
time.sleep(2)
browser.find_element_by_link_text("About").click()
time.sleep(2)
browser.find_element_by_link_text(u"教程 ▾").click()
time.sleep(2)
browser.find_element_by_link_text(u"推荐学习顺序").click()
time.sleep(2)
html = browser.page_source
browser.get_screenshot_as_file("zz123.png")

time.sleep(10)  # 休息5秒
browser.close()
browser.quit()

 

 

 


 

常用的查找元素方法:

  • find_element_by_name
  • find_element_by_id
  • find_element_by_xpath
  • find_element_by_link_text
  • find_element_by_partial_link_text
  • find_element_by_tag_name
  • find_element_by_class_name
  • find_element_by_css_selector

 

示例:  其它例子去这里看:http://www.cnblogs.com/zhaof/p/6953241.html

from selenium import webdriver
browser = webdriver.Chrome()
browser.get("http://www.zhihu.com/explore")

#获取元素属性 get_attribute('class')
logo = browser.find_element_by_id('zh-top-link-logo')
print(logo)
print(logo.get_attribute('class'))

#获取文本值 text
input = browser.find_element_by_class_name('zu-top-add-question')
print(input.text)

# 执行js
# browser.execute_script('window.scrollTo(0, document.body.scrollHeight)')
# browser.execute_script('alert("To Bottom")')

#获取:id,  位置:location ,  标签名:tag_name,  size
input = browser.find_element_by_class_name('zu-top-add-question')
print(input.id)
print(input.location)
print(input.tag_name)
print(input.size)

 

 

 ...

 

推荐阅读