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

Selenium_python 示例代码(1)

2013-08-23 14:44 609 查看
代码1:

#-*- coding:utf-8 -*-

from selenium import webdriver

from selenium.common.exceptions import NoSuchElementException

from selenium.webdriver.common.keys import Keys

import time

browser = webdriver.Firefox() # Get local session of firefox

browser.get("http://www.baidu.com") # Load page

assert "百度一下,你就知道" in browser.title

elem = browser.find_element_by_id("kw") # Find the query box

elem.send_keys("seleniumhq" + Keys.RETURN)

time.sleep(0.2) # Let the page load, will be added to the API

try:

browser.find_element_by_xpath("//a[contains(@href,'http://seleniumhq.org')]")

except NoSuchElementException:

assert 0, "can't find seleniumhq"

browser.close()
代码2:

#-*- coding:utf-8 -*-

from selenium import webdriver

from selenium.common.exceptions import NoSuchElementException

from selenium.webdriver.common.keys import Keys

import time

browser = webdriver.Chrome() # Get local session of firefox

browser.get("http://www.baidu.com") # Load page

assert "百度一下,你就知道" in browser.title

elem = browser.find_element_by_id("kw") # Find the query box

elem.send_keys("seleniumhq" )

elem1=browser.find_element_by_id("su")

elem1.click()

time.sleep(0.2) # Let the page load, will be added to the API

try:

browser.find_element_by_xpath("//div[@class='floatTip']")

except NoSuchElementException:

assert 0, "can't find seleniumhq"

browser.close()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: