Selenium_python 示例代码(1)

来源:互联网 发布:网络游戏编程 编辑:程序博客网 时间:2024/06/01 08:41

代码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()

原创粉丝点击