百度搜索(python):
from selenium import webdriver
driver=webdriver.Chrome()
driver.get("http://www.baidu.com")
driver.find_element_by_id("kw").send_keys("cz9025")
driver.find_element_by_id("su").click()
driver.quit()
百度搜索(ruby):
require 'rubygems'
require "watir-webdriver"
b = Watir::Browser.new :chrome
b.goto "http://www.baidu.com"
b.text_field(:id => 'kw').set "cz9025"
b.button(:id => "su").click
sleep 3
b.quit
两者对比,使用方式大同小异。
ruby定位元素:
标签 | 说明 |
---|---|
button | input tags with type=button, submit, image or reset |
radio | input tags with the type=radio; known as radio buttons |
check_box | input tags with type=checkbox |
text_field | input tags with the type=text (single-line), type=textarea (multi-line), and type=password |
hidden | input tags with type=hidden |
select | select tags, known as drop-downs or drop-down lists |
label | label tags (including “for” attribute) |
span | span tags |
div | div tags |
p | p (paragraph) tags |
link | a (anchor) tags |
table | table tags, including row and cell methods for accessing nested elements. |
image | img tags |
form | form tags |
frame | frames, including both the frame elements and the corresponding pages. |
map | map tags |
area | area tags |
li | li tags |
部分使用示例:
b.element(:id => "su").click
b.button(:id => "su").click
set是设置值,value是取值
b.text_field(:id => 'kw').set "cz9025"
b.select(:name => "NR").select "每页显示20条"
ruby+watir-webdriver,支持的浏览器有Firefox、Chrome 、IE等大部分浏览器。
定位:支持所有的HTML元素
定位方式:支持id、name、class、xpath等常用的方式