Python版:Selenium2.0之WebDriver学习总结_实例1

Python版:Selenium2.0之WebDriver学习总结_实例1

 

 快来加入群【python爬虫交流群】(群号570070796),发现精彩内容。

 

 

实属转载:本人看的原文地址 :http://blog.sina.com.cn/s/blog_6b1ed4fb0101diuz.html

 (2012-12-10 13:49:53)
标签: 

selenium2

 

webdriver

 

python

 

it

分类: Selenium
例子1:该例子主要是登录网易相册,创建一个私人相册,并进入某相册对照片浏览并评论,删除照片。然在这个例子中,可以找到各种方式定位元素方法的具体实例:
备注:iframe焦点还有问题啊,待解决。
 
# coding:utf-8
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import unittest
 
 
class Test_Photo(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Chrome()
        self.baseUrl = "http://photo.163.com"
        #设置等待时间,防止页面未加载完成脚本提示异常
        self.driver.implicitly_wait(30)
        self.verificationErrors = []
 
    def test_WebDriverTest1(self):
        #打开一个网址
        driver = self.driver
        driver.get(self.baseUrl + "/")
 
        #输入用户名
        #by XPath
        driver.find_element_by_xpath("//input[@name='username']").clear()
        driver.find_element_by_xpath("//input[@name='username']").send_keys("angel-zhe@163.com")
        driver.find_element_by_xpath("//input[@name='username']").send_keys(Keys.ENTER)
 
        #输入密码
        #by name
        driver.find_element_by_name("password").clear()
        driver.find_element_by_name("password").send_keys("xtrrk0707")
 
        #点击登录
        #by id
        driver.find_element_by_id("photo_index_login").click()
 
        #获取页面Title
        #by js
        print driver.title
 
        #进入我的相册
        #by Xpath
        driver.find_element_by_css_selector("span.name").click()
        # driver.find_element_by_xpath("//div[@id='J-loginMod']/div/a[1]/span[1]").click()
 
        #点击  创建相册
        #by linkText
        driver.find_element_by_link_text("创建相册").click()
 
        #输入相册名称
        #by name
        driver.find_element_by_name("fm-0").clear()
        driver.find_element_by_name("fm-0").send_keys("new ablum2")
        #by Xpath
        #driver.find_element_by_xpath("//input[@name='fm-0']").clear()
        #driver.find_element_by_xpath("//input[@name='fm-0']").send_keys("new ablum")
 
        #选择私人相册
        driver.find_element_by_xpath("(//input[@name='fm-2'])[5]").click()
 
        #点击确定 两种方式
        #by css
        driver.find_element_by_css_selector("button.ui-btn.ui-btn-sub0").click()
        driver.implicitly_wait(10)
 
        #点击返回
        #by linkText
        driver.find_element_by_link_text("<<返回").click()
 
        #选择aaa分类
        #by css
        #select = driver.find_element_by_css_selector("select.fc5.bdwa")
        #/选择的三种不同实现
        #select.select_by_value("aaa")
        #select.select_by_index(1)
        #select.select_by_visible_text("aaa")
 
        #以上这个分类操作方法因为界面问题,还没有尝试去验证Select方法,以下经Link_Text方法操作
        #by link_Text
        driver.find_element_by_link_text("分类").click()
        driver.find_element_by_link_text("全部相册").click()
        driver.implicitly_wait(10)
 
        #打开相册
        #by Xpath
        driver.find_element_by_xpath("//img[@title='我的相册 ']").click()
 
        #打开某张照片
        #by Css
        driver.find_element_by_css_selector("div.item.on > div.ln.ln0 > a > img.ztag201008041230345").click()
 
        #下一张浏览照片
        #by Css
        driver.find_element_by_css_selector("div.ibox.right-img > img.ztag201008041230345").click()
 
        #输入评论
        #/因为评论框放在一个iframe中,所以要切换到iframe中,结束后将焦点切换回去
        #by tagName
        driver.switch_to_frame(driver.find_element_by_tag_name("iframe"))
        activeElementid = driver.switch_to_active_element().get_attribute("id")
        driver.find_element_by_id(activeElementid).sendKeys("评论内容!")
        driver.switch_to_default_content()
 
        #点击 发表,两种方式
        #by className
        driver.find_element_by_class_name("ui-btn-main0").click()
        #by Xpath
        driver.find_element_by_xpath("//input[@value='发表']").click()
 
        #上一张浏览照片
        #by Css
        driver.find_element_by_css_selector("div.ibox left-img > img.ztag201008041230345").click()
 
        #点击 删除
        #by linkText
        driver.find_element_by_link_text("删除").click()
 
        #点击 确认
        #by Css
        #driver.find_element_by_css_selector("button.ui-btn.ui-btn-sub0").click()
        driver.find_element_by_link_text("确 定").click()
 
        #点击 返回
        #by xpath
        driver.find_element_by_link_text("<<返回").click()
 
        #退出登录
        #by id
        driver.find_element_by_id("headerLogout").click()
 
        #浏览器后退
        driver.back()
 
    def tearDown(self):
        self.driver.quit()
        self.assertEqual([], self.verificationErrors)
 
if __name__ == "__main__":
    unittest.main()

 

posted @ 2015-10-15 10:55  你居然是华神  阅读(607)  评论(0编辑  收藏  举报