Selenium Webdriver API(6)

33、右键 context_click 在某个对象上点击右键
#encoding=utf-8
from selenium import webdriver
import unittest
import time
from selenium.webdriver import ActionChains
import win32con
import win32clipboard as w

#设置剪切板
def setText(aString):
    w.OpenClipboard()
    w.EmptyClipboard()
    w.SetClipboardData(win32con.CF_UNICODETEXT,aString)
    w.CloseClipboard()

class visitSogouByIE(unittest.TestCase):
    def setUp(self):
        #启动浏览器
        self.driver = webdriver.Ie(executable_path = "D:\\IEDriverServer")
    def test_rightClick(self):
        url = "http://www.sogou.com"
        self.driver.get(url)
        #找到搜索输入框
        searchBox = self.driver.find_element_by_id("query")
        #将焦点切换到搜索框
        searchBox.click()
        time.sleep(2)
        #在搜索输入框中执行一个鼠标右键点击操作
        ActionChains(self.driver).context_click(searchBox).perform()
        #将某个数据设置到剪切板 “魔兽世界”,相当于执行了复制操作
        setText(u"魔兽世界")
        #发送一个粘贴命令,字符p代替粘贴操作
        ActionChains(self.driver).send_keys("p").perform()
        #点击搜索按钮
        self.driver.find_element_by_id("stb").click()

def tearDown(self):
        self.driver.quit()

if __name__ == "__main__":
    unittest.main()

34、左键长按、释放 click_and_hold release
#encoding=utf-8
import time
import unittest
from selenium import webdriver
from selenium.webdriver import ActionChains

class visitLocalWebByIE(unittest.TestCase):
    def setUp(self):
        #启动浏览器
        self.driver = webdriver.Ie(executable_path = "D:\\IEDriverServer")
    def test_leftClickHoldAndRelease(self):
        url = "http://127.0.0.1:8080/test_mouse.html"
        #访问自定义网页
        self.driver.get(url)
        div = self.driver.find_element_by_id("div1")
        #在id属性值为div1的元素上执行按下鼠标左键并保持
        ActionChains(self.driver).click_and_hold(div).perform()
        time.sleep(2)
        #在id属性值为div1的元素上释放被一直按下的鼠标左键
        ActionChains(self.driver).release(div).perform()
        time.sleep(2)
        ActionChains(self.driver).click_and_hold(div).perform()
        time.sleep(2)
        ActionChains(self.driver).release(div).perform()
    def tearDown(self):
        self.driver.quit()

if __name__ == "__main__":
    unittest.main()

35、鼠标悬停 move_to_element
#encoding=utf-8
import time
import unittest
from selenium import webdriver
from selenium.webdriver import ActionChains

class visitLocalWebByIe(unittest.TestCase):
    def setUp(self):
        #启动浏览器
        self.driver = webdriver.Ie(executable_path = "D:\\IEDriverServer")

def test_mouseHover(self):
        url = "http://127.0.0.1:8080/test_mouse_hover.html"
        #访问自定义网页
        self.driver.get(url)
        #找到页面上的第一个链接元素
        link1 = self.driver.find_element_by_link_text(u"鼠标指过来1")
        #找到页面上的第二个链接元素
        link2 = self.driver.find_element_by_link_text(u"鼠标指过来2")
        #找到页面上的p元素
        p = self.driver.find_element_by_xpath("//p")
        print link1.text,link2.text
        #将鼠标悬浮在第一个链接元素上
        ActionChains(self.driver).move_to_element(link1).perform()
        time.sleep(2)
        #将鼠标从第一个链接元素上移动到p元素
        ActionChains(self.driver).move_to_element(p).perform()
        time.sleep(2)
        #将鼠标悬浮到第二个链接元素上
        ActionChains(self.driver).move_to_element(link2).perform()
        time.sleep(2)
        #将鼠标从第二个链接元素上移到p元素
        ActionChains(self.driver).move_to_element(p).perform()
        time.sleep(2)

def tearDown(self):
        self.driver.quit()

if __name__ == "__main__":
    unittest.main()

36、判断元素是否在页面中 find_element
#encoding=utf-8
import time
import unittest
from selenium import webdriver

class visitSoGouByIe(unittest.TestCase):

def setUp(self):
        #启动浏览器
        self.driver = webdriver.Ie(executable_path = "D:\\IEDriverServer")

def isElementPresent(self,by,value):
        #从selemium.common.exceptions模块导入NoSuchElementExcetion异常类
        from selenium.common.exceptions import NoSuchElementException
        try:
            element = self.driver.find_element(by = by,value = value) #使用某种定位方式及值来定位元素
        except NoSuchElementException,e:
            #打印异常信息
            print e
            #发生NoSuchElementEception异常,说明页面中未找到该元素,则返回False
            return False
        else:
            # 没有发生异常,表示在页面中找到了元素,返回True
            return True

def test_isElementPresent(self):
        url = "http://www.sogou.com"
        #访问搜狗
        self.driver.get(url)
        #判断页面元素id属性值为"query"的页面元素是否存在
        res = self.isElementPresent("id","query")
        if res is True:
            print u"所查找的元素存在于页面上!"
        else:
            print u"页面中未找到所需要的元素!"

def tearDown(self):
        self.driver.quit()

if __name__ == "__main__":
    unittest.main()

原文地址:https://www.cnblogs.com/test-chen/p/10524812.html

时间: 2024-08-30 13:25:15

Selenium Webdriver API(6)的相关文章

Selenium Webdriver API(2)

15.获取元素基本信息 #启动IE浏览器driver = webdriver.Ie(executable_path="IEDriverServer")#打开搜狗driver.get("http://www.sogou.com")#获取"新闻"link = driver.find_element_by_xpath(u"//*[text()='新闻']")#查看link类型type(link) #link为webElement对象

Selenium Webdriver API(4)

27.操作复选框 checkBox#encoding=utf-8import unittestimport timefrom selenium import webdriver class VisitLocalWebByIE(unittest.TestCase):    def setUp(self):        #启动浏览器        self.driver = webdriver.Ie(executable_path = "D:\\IEDriverServer")    d

<译>Selenium Python Bindings 6 - WebDriver API

本章涉及Selenium WebDriver的所有接口. Recommended Import Style 推荐的导入风格如下: from selenium import webdriver 然后,你可以这样访问所有的类: webdriver.Firefox webdriver.FirefoxProfile webdriver.Chrome webdriver.ChromeOptions webdriver.Ie webdriver.Opera webdriver.PhantomJS webdr

[selenium webdriver Java]常用api

1. 获取元素文本 WebElement类的getText()方法返回元素的innerText属性.所以元素里如果有子节点一样也会被返回出来.如下所示 1 public class GetText { 2 @Test 3 public void testGetText(){ 4 //启动driver,打开被测页面 5 System.setProperty("webdriver.chrome.driver", "chromedriver.exe"); 6 WebDri

selenium之WebDriver API

自动化只要掌握四步操作:获取元素,操作元素,获取返回结果,断言(返回结果与期望结果是否一致),最后自动出测试报告,元素定位在这四个环节中是至关重要的,如果说按学习精力分配的话,元素定位占70%:操作元素10%,获取返回结果10%:断言10%.如果一个页面上的元素不能被定位到,那后面的操作就无法继续了.而WebDriver 属于Selenium体系设计出来操作浏览器的一套API,它支持多种语言,Selenium WebDriver只是python的一个第三方框架,也就是说是一个实现web自动化的第

Selenium中WebDriver API的使用(三)

WebDriver API中常用的方法和属性 方法: clear() --->清除一个文本输入框 p:driver.find_element_by_id("kw").clear() send_keys()  -->来输入字符串 p:driver.find_element_by_id("kw").send_keys("Selenium") click() -->点击页面上支持点击的元素 driver.find_element_by

Selenium WebDriver(Python)API

1.通过示例介绍Selenium-WebDriver 一个简单的入门方法就是这个例子,它在Google上搜索术语"Cheese",然后将结果页面的标题输出到控制台. java csharp pythonfrom selenium import webdriverfrom selenium.common.exceptions import TimeoutExceptionfrom selenium.webdriver.support.ui import WebDriverWait # 可

Selenium2(java)selenium常用API 四

WebElement相关方法 1.点击操作 WebElement button = driver.findElement(By.id("login")); button.click(); 由元素对象调用click()方法:   2.清除操作 WebElement username = driver.findElement(By.id("username_input")); username.clear(); 调用之后,会把输入框的内容全部清空:   3.获得元素属性

Selenium 中文API

1.1   下载selenium2.0的lib包 http://code.google.com/p/selenium/downloads/list 官方UserGuide:http://seleniumhq.org/docs/ 1.2   用webdriver打开一个浏览器 我们常用的浏览器有firefox和IE两种,firefox是selenium支持得比较成熟的浏览器.但是做页面的测试,速度通常很慢,严重影响持续集成的速度,这个时候建议使用HtmlUnit,不过HtmlUnitDirver运