您的位置:首页 > 编程语言 > Python开发

15. python+selenium-webdriver2实战练习

2014-07-23 14:49 295 查看
借鉴张飞同学的HTML来进行的python-webdriver实战,文件下载路径:http://files.cnblogs.com/hugh007/demo.zip



代码如下:

from selenium import webdriver
from time import sleep
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.alert import Alert
from selenium.webdriver.support.ui import WebDriverWait
#from selenium.webdriver.support import expected_conditions as EC
#from selenium.webdriver.common.by import By

import os

class Demo(object):

def __init__(self):
self.dr = webdriver.Chrome()

#Goto
def test_goto(self,url):
self.dr.get(url)

#Quit
def test_quit(self):
self.dr.quit()

#Input Text
def test_input(self,value):
input_element = self.dr.find_element_by_id('user')

input_element.send_keys(value)
sleep(2)

input_element.clear()
sleep(2)

input_element.send_keys(value)

#get the attribute value
text = input_element.get_attribute("value");
print text

#Link
def test_link(self):

link_element = self.dr.find_element_by_link_text('baidu')

#get the link address
href = link_element.get_attribute("href")
print href

#get the link value
text = link_element.text
print text

link_element.click()
self.dr.back()

#Single Select
def test_select(self):
select = Select(self.dr.find_element_by_name('select'))

select.select_by_index(1)
sleep(2)

select.select_by_visible_text("Audi")
sleep(2)

select.select_by_value('opel')

    # 选择下拉菜单中的最后一项

    dr.find_element_by_tag_name('select').find_elements_by_tag_name('option')[-1].click()

#RadioBox
def test_radio(self):
radioboxes = self.dr.find_elements_by_name('identity')

for radiobox in radioboxes:
radiobox.click()
sleep(2)

#CheckBox
def test_checkbox(self):

checkboxes = self.dr.find_elements_by_css_selector('input[type=checkbox]')
for checkbox in checkboxes:
checkbox.click()
sleep(2)
self.dr.find_elements_by_css_selector('input[type=checkbox]').pop().click()

# local single Radiobox and CheckBox
#dr.find_element_by_xpath("//tr[4]/td[2]/div/input[3]").click()
#dr.find_element_by_xpath("//input[@type='checkbox'][@name='checkbox1']").click()

# button
def test_button(self):
button_element = self.dr.find_element_by_class_name("button")
button_element.click
button = button_element.is_enabled()
print button

#ActionChains
def test_action_chains(self):
"""
eg
menu = driver.find_element_by_css_selector(".nav")
hidden_submenu = driver.find_element_by_css_selector(".nav #submenu1")

actions = ActionChains(driver)
actions.move_to_element(menu)
actions.click(hidden_submenu)
actions.perform()
"""
alert_element = self.dr.find_element_by_class_name("alert")
actions = ActionChains(self.dr)
actions.click(alert_element)
actions.perform()
sleep(2)

#popped up Alert
def test_alert(self):
alerts = Alert(self.dr)
print alerts.text
alerts.accept()
#alerts.dismiss()

#upload
def test_upload(self, filepath):
#JavascriptExecutor j= (JavascriptExecutor)driver;
#j.executeScript("document.findElementById('load').style.display='block'; ");
upload_element = self.dr.find_element_by_id("load")
upload_element.send_keys(filepath)

def test_multiple_windows(self):
element = self.dr.find_element_by_class_name('open')
element.click()

def test_action(self):
action_element= self.dr.find_element_by_class_name("over")
actions = ActionChains(self.dr)
actions.move_to_element(action_element)
actions.perform()
text = self.dr.find_element_by_class_name("over").text
print text

def test_wait(self):
wait_element = self.dr.find_element_by_class_name("wait")
wait_element.click()
waits = WebDriverWait(self.dr, 10)
element = waits.until(lambda s: s.find_element_by_class_name("red"))

#print waits.until(lambda s: s.find_element_by_class_name("red").is_displayed())

if element.is_displayed():
print element.text
else:
print "Failed to display!"

file_path = 'file:///' + os.path.abspath('demo.html')
a = Demo()
a.test_goto(file_path)
#a.test_input("This is python script")
#a.test_link()
#a.test_select()
#a.test_radio()
#a.test_checkbox()
#a.test_button()
#a.test_action_chains()
#a.test_alert()
#a.test_upload("c:\\test.txt")
#a.test_multiple_windows()
#a.test_action()
#a.test_wait()
#a.test_quit()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: