• Selenium Webdriver API(2)


    Selenium Webdriver API(2)

    15、获取元素基本信息
    #启动IE浏览器
    driver = webdriver.Ie(executable_path="D:\IEDriverServer")
    #打开搜狗
    driver.get("http://www.sogou.com")
    #获取“新闻”
    link = driver.find_element_by_xpath(u"//*[text()='新闻']")
    #查看link类型
    type(link) #link为webElement对象
    #查看link属性和方法
    dir(link)
    #获取文本值
    print link.text
    #获取元素标签名
    link.tag_name
    #获取元素大小
    link.size
    #获取属性
    link.get_attribute("href")
    #获取元素在页面中的位置
    link.location
    练习:获取百度搜索按钮中的文字“百度一下”
    driver.get("http://www.baidu.com")
    bot = driver.find_element_by_id("su")
    print bot.get_attribute("value")

    16、判断元素是否可见 is_displayed
    driver.get("http://www.sogou.com")
    submit_botton = dirver.find_element_by_id("query")
    submit_botton.is_displayed()

    练习:判断按钮是否可见
    测试代码:test_visible.html (安装Apache后,并配置好后,将文件放到htdocs中,然后在本地浏览器打开)
    http://localhost:8080/test_visible.html

    test_visible.py
    #encoding=utf-8
    import unittest
    from selenium import webdriver

    class VisitByIE(unittest.TestCase):
        def setUp(self):
            # 启动浏览器
            self.driver = webdriver.Ie(executable_path="D:\IEDriverServer")
        def test_visible(self):
            self.driver.get("http://localhost:8080/test_visible.html")
            divs = [self.driver.find_element_by_id("div"+str(i)) for i in range(1,5)]
            b1 = self.driver.find_element_by_id("button1")
            b2 = self.driver.find_element_by_id("button2")
            for div in divs:
                if div.is_displayed():
                    print div.is_displayed()
                else:
                    print div.is_displayed(),"div not visible"
            b1.click()
            b2.click()
            print "++++++++++++++++++++++++++++++++"
            for div in divs:
                if div.is_displayed():
                    print div.is_displayed()
                else:
                    print div.is_displayed(),"div not visible"

        def tearDown(self):

            self.driver.quit()

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

    17、判断元素是否可用 is_enabled
    测试网页:test_enable.html
    driver.get("http://localhost:8080/test_enable.html")
    input1 = driver.find_element_by_id("input1")
    input1.is_enabled()

    18、获取css内容 value_of_css_property
    input1.value_of_css_property("width")
    input1.value_of_css_property("font-family")

    19、清空输入框 clear
    input1.clear()

    20、输入内容 send_keys
    input1.send_keys("test")

    21、点击按钮 click
    driver.get("http://localhost:8080/test_button.html")
    button = driver.find_element_by_id("button")
    button.click()

    22、双击 double_click
    driver.get("http://localhost:8080/test_doubleclick.html")
    inputbox = driver.find_element_by_id("inputBox")
    from selenium.webdriver import ActionChains
    action_chains = ActionChains(driver)
    action_chains.double_click(inputbox).perform()

    测试代码:
    double_click.py
    #encoding=utf-8
    import time
    import unittest
    from selenium import webdriver

    class VisitLocalwebByIE(unittest.TestCase):
        def setUp(self):
            #启动浏览器
            self.driver = webdriver.Ie(executable_path="D:\IEDriverServer")
        def test_doubleClick(self):
            #自定义的测试网页
            url = "http://localhost:8080/test_doubleclick.html"
            #打开网页
            self.driver.get(url)
            #获取页面输入框元素
            inputbox = self.driver.find_element_by_id("inputBox")
            #导入支持双击的包
            from selenium.webdriver import ActionChains
            #实例化ActionChains
            action_chains = ActionChains(self.driver)
            #双击
            action_chains.double_click(inputbox).perform()
            time.sleep(3)

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

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

  • 相关阅读:
    测试平台系列(93) 免费的gitee当oss真的香吗
    C/C++ 结构体内存分布
    c# 委托与事件学习
    unity随记
    unity 对于对象刚体不断抖动的处理
    uniapp .keystore文件生成后不可食用的解决方案
    照片尺寸
    Failed to load module script: Expected a JavaScript module script but the server responded with a MIME type of "text/plain". Strict MIME type checking is enforced for module scripts per HTML spec.
    ConstraintLayout
    Navigation库支持的参数类型
  • 原文地址:https://www.cnblogs.com/test-chen/p/10331350.html
Copyright © 2020-2023  润新知