selenium的webDriver核心属性和方法

1.核心属性
常用属性如下所示:在这里插入图片描述
代码如下:

from selenium import webdriver
from time import sleep


class TestCase01(object):
    def __init__(self):
        self.driver = webdriver.Chrome()
        self.driver.get('http://www.baidu.com')
        self.driver.maximize_window()
    def test_prop(self):
        print(self.driver.name)# 浏览器名称
        print(self.driver.current_url) # url
        print(self.driver.title)
        print(self.driver.window_handles) #可根据句柄来切换窗口
        print(self.driver.page_source) #页面源码
        self.driver.quit()
if __name__ == '__main__':
    case = TestCase01()
     case.test_prop() 

运行的结果如下:由于只打开了一个窗口,所以只有一个句柄
在这里插入图片描述

2.核心方法
核心方法如下所示:
在这里插入图片描述
代码如下:

from selenium import webdriver
from time import sleep


class TestCase02(object):
    def __init__(self):
        self.driver = webdriver.Chrome()
        self.driver.get('http://www.baidu.com')
        self.driver.maximize_window()
    def test_prop(self):
        print(self.driver.name)# 浏览器名称
        print(self.driver.current_url) # url
        print(self.driver.title)
        print(self.driver.window_handles)
        print(self.driver.page_source)
        self.driver.quit()

    def test_method(self):
        self.driver.find_element_by_id('kw').send_keys('selenium')
        self.driver.find_element_by_id('su').click()
        sleep(2)
        self.driver.back() #显示的页面为百度一下
        sleep(2)
        self.driver.refresh()#对页面进行刷新
        sleep(2)
        self.driver.forward()#返回到查询selenium的页面

        self.driver.close() # 只关闭当前tab
        self.driver.quit() # 关闭浏览器

if __name__ == '__main__':
    case = TestCase02()
    # case.test_prop()
    case.test_method()

3.使用句柄实现窗口的来回切换
代码实现中是打开了两个页面,使用switch_to.window方法实现窗口的切换

from selenium import webdriver
from time import sleep


class TestCase03(object):
    def __init__(self):
        self.driver = webdriver.Chrome()
        self.driver.get('http://www.baidu.com')
        self.driver.maximize_window()

    def test_windows(self): #窗口的来回切换
        self.driver.find_element_by_link_text('新闻').click()
        windows = self.driver.window_handles

        while 1:
            for w in windows:
                self.driver.switch_to.window(w)
                sleep(2)
                
if __name__ == '__main__':
    case = TestCase03()
    case.test_windows()
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
amazon 3星+ 好评 Key Features Covers basic to advanced concepts of WebDriver Learn how to design a more effective automation framework Explores all of the APIs within WebDriver Acquire an in-depth understanding of each concept through practical code examples Book Description Selenium WebDriver is an open source web UI automation tool implemented through a browser-specific browser driver, which sends commands to a browser and retrieves results. Selenium WebDriver Practical Guide will guide you through the various APIs of WebDriver which should be used in automation tests, followed by a discussion of the various WebDriver implementations available. This guide will support you by offering you access to source code fi les, including the essential HTML fi les, that allow you to work with jQuery and other examples throughout the book. Finally, you will receive an in-depth explanation of how to deal with the latest features of WebDriver through step-by-step practical tutorials. What you will Learn Learn the basic design of Selenium WebDriver and understand how it overcomes the limitations of its predecessor Set up and use Selenium Grid Discover the various mechanisms used to locate web elements Perform advanced actions such as drag-and-drop, double-click, right-click, and action builders on web pages Explore the advanced features of WebDriver such as handling cookies, taking screenshotsmanaging timeouts, and browser navigations Acquire an understanding of the various WebDriver implementations such as Firefox Driver, IE Driver, and Chrome Driver Explore WebDriver EventListeners Design effective automation frameworks using the PageObject pattern Learn how to deal with the latest features of WebDriver About the Author Satya Avasarala has rich experience in Java development and automation testing. He is an engineer in computer science. He has used WebDriver for many years now and has created several good automation frameworks. He has worked at various large software enterprises such as Oracle Corp, Yahoo! Inc., VMware Inc., and the REA Group. Table of Contents Introducing WebDriver and WebElements Exploring Advanced Interactions of WebDriver Exploring the Features of WebDriver Different Available WebDrivers Understanding WebDriver Events Dealing with I/O Exploring RemoteWebDriver and WebDriverBackedSelenium Understanding Selenium Grid Understanding PageObject Pattern Testing iOS and Android Apps

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

永远不要矫情

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值