主要参考:https://www.gitbook.com/book/easonhan007/selenium-webdriver/details

初始化环境

  1. 安装python
  2. selenium webdriver: pip install selenium

简单操作

打开浏览器:

参考:https://www.jianshu.com/p/e137031bc7db

1). 根据chrome版本选择对应的chrome driver selenium之 chromedriver与chrome版本映射表(更新至v2.29)

from selenium import webdriver
#webdriver.Chrome() mac环境报错:`“'chromedriver' executable needs to be available in the path”`需要指定路径 /Applications/Google Chrome.app

2). chromedriver文件放在“/usr/local/bin”目录下,然后运行下面的代码

from selenium import webdriver

driver = webdriver.Chrome()
base_url = 'https://www.baidu.com'
driver.get(base_url)

关闭浏览器

可以使用close、或quit方法

close方法关闭当前的浏览器窗口,quit方法不仅关闭窗口,还会彻底的退出webdriver,释放与driver server之间的连接

from selenium import webdriver
import  time

driver = webdriver.Chrome()
base_url = 'https://www.baidu.com'
driver.get(base_url)

print('browse will be closed in 2s')
time.sleep(2)
driver.quit() #or  .close()

调节浏览器窗口大小

driver.maximize_window()
driver.set_window_size(240,320)

访问链接

driver = webdriver.Chrome()
base_url = 'https://www.baidu.com'
driver.get(base_url)

获取title或url

from selenium import webdriver
import  time

driver = webdriver.Chrome()
base_url = 'https://www.baidu.com'
driver.get(base_url)
print("title of current page is %s" %(driver.title))
print("url of current page is %s" %(driver.current_url))
#title of current page is 百度一下,你就知道
#url of current page is https://www.baidu.com/

前进后退

dr.get(first_url) dr.get(second_url) dr.back()

对象定位

简单的对象定位

测试对象的定位和操作是webdriver的核心内容,其中操作又是建立在定位的基础之上,因此对象定位就越发显得重要了。 定位对象的目的一般有下面几种 操作对象 获得对象的属性,如获得测试对象的class属性,name属性等等 获得对象的text 获得对象的数量 webdriver提供了一系列的对象定位方法,常用的有以下几种

  • id
  • name
  • class name
  • link text
  • partial link text
  • tag name
  • xpath
  • css selector
<html>
    <head>
      <meta http-equiv="content-type" content="text/html;charset=utf-8" />
      <title>Form</title>
      <script type="text/javascript" async="" src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
      <link href="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-combined.min.css" rel="stylesheet" />
      <script src="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/js/bootstrap.min.js"></script>
    </head>
    <body>
      <h3>simple login form</h3>
      <form class="form-horizontal">
        <div class="control-group">
          <label class="control-label" for="inputEmail">Email</label>
          <div class="controls">
            <input type="text" id="inputEmail" placeholder="Email" name="email">
          </div>
        </div>
        <div class="control-group">
          <label class="control-label" for="inputPassword">Password</label>
          <div class="controls">
            <input type="password" id="inputPassword" placeholder="Password" name="password">
          </div>
        </div>
        <div class="control-group">
          <div class="controls">
            <label class="checkbox">
              <input type="checkbox"> Remember me
            </label>
            <button type="submit" class="btn">Sign in</button>
            <a href="#">register</a>
          </div>
        </div>
      </form>
    </body>
  </html>

py脚本(python2.x):

from selenium import webdriver
from time import sleep
import os
if 'HTTP_PROXY'in os.environ: del os.environ['HTTP_PROXY']

dr = webdriver.Chrome()
file_path = 'file:///' + os.path.abspath('form.html')
print file_path

dr.get(file_path)

# by id
dr.find_element_by_id('inputEmail').click()

# by name
dr.find_element_by_name('password').click()

# by tagname
print dr.find_element_by_tag_name('form').get_attribute('class')

# by class_name
e = dr.find_element_by_class_name('controls')
dr.execute_script('$(arguments[0]).fadeOut().fadeIn()', e)
sleep(1)

# by link text
link = dr.find_element_by_link_text('register')
dr.execute_script('$(arguments[0]).fadeOut().fadeIn()', link)
sleep(1)

# by partial link text
link = dr.find_element_by_partial_link_text('reg')
dr.execute_script('$(arguments[0]).fadeOut().fadeIn()', link)
sleep(1)

# by css selector
div = dr.find_element_by_css_selector('.controls')
dr.execute_script('$(arguments[0]).fadeOut().fadeIn()', div)
sleep(1)

# by xpath
dr.find_element_by_xpath('/html/body/form/div[3]/div/label/input').click()

sleep(2)
dr.quit()

上面例子里由于html文件中引用了jquery,所以在执行js时可以使用jquery的$()及fadeIn()等方法。如果你测试的页面没用包含jquery的话,这些方法是无效的。

定位一组对象

<!DOCTYPE html>
<html>
        <head>
            <meta http-equiv="content-type" content="text/html;charset=utf-8" />
            <title>Checkbox</title>
            <script type="text/javascript" async="" src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
            <link href="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-combined.min.css" rel="stylesheet" />
            <script src="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/js/bootstrap.min.js"></script>
        </head>
        <body>
            <h3>checkbox</h3>
            <div class="well">
                <form class="form-horizontal">
                    <div class="control-group">
                        <label class="control-label" for="c1">checkbox1</label>
                        <div class="controls">
                            <input type="checkbox" id="c1" />
                        </div>
                    </div>
                    <div class="control-group">
                        <label class="control-label" for="c2">checkbox2</label>
                        <div class="controls">
                            <input type="checkbox" id="c2" />
                        </div>
                    </div>
                    <div class="control-group">
                        <label class="control-label" for="c3">checkbox3</label>
                        <div class="controls">
                            <input type="checkbox" id="c3" />
                        </div>
                    </div>
                    <div class="control-group">
                        <label class="control-label" for="r">radio</label>
                        <div class="controls">
                            <input type="radio" id="r" />
                        </div>
                    </div>
                </form>
            </div>
        </body>
    </html>
from selenium import webdriver
import  time
import os

driver = webdriver.Chrome()
file_path =  'file:///' + os.path.abspath('new_file.html')

driver.get(file_path)

#找到所有的checkbox并勾选
checkboxes = driver.find_elements_by_css_selector('input[type=checkbox]')

for checkbox in checkboxes:
    checkbox.click()
time.sleep(3)
driver.quit()