Selenium Webdirver API???1???

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Selenium Webdirver API???1???相关的知识,希望对你有一定的参考价值。

???????????????   com   path   3.x   html   clear   ??????   link   ??????   

Selenium Webdirver API

???????????????webdriver???
from selenium import webdriver

1????????????????????????
driver = webdriver.Ie(executable_path="D:\IEDriverServer")#?????????????????????????????????????????????????????????webdriver.Chrome() webdriver.Firefox()

2??????????????? get
driver.get("http://www.sogou.com") #??????????????????

3?????????????????????????????? back forward
driver.back() #?????????????????????
driver.forward() #????????????????????????

4?????????????????????????????? refresh
driver.refresh() #????????????????????????????????????????????????????????????

5??????????????? set_page_load_timeout
driver.set_page_load_timeout(3) #????????????3????????? ???????????????
#???????????????????????????????????????????????????
try:
    driver.get("http://www.sogou.com)
except Exception,e:
    print e

6?????????????????? maximize_window
driver.maximize_window() #???????????????

7??????????????????????????? get_window_position
driver.get_window_position() #?????????????????????
#????????????key-value?????????????????????
driver.get_window_position()["x"]
driver.get_window_position()["y"]

8??????????????????????????? set_window_position
driver.set_window_position(100,150)

9??????????????????????????? get_window_size()
driver.get_window_size() #????????????????????????
#?????????key_value?????????????????????
driver.get_window_size()["width"]
driver.get_window_size()["height"]

10??????????????????????????? set_window_size
driver.set_window_size(100,200)

11????????????????????????
driver.title #?????????????????????????????????
driver.get("http://www.sohu.com")
assert u"??????" == driver.title

12???????????????????????????
driver.page_source #????????????????????????????????????unicode
#????????????????????????????????????????????????
driver.page_source.encode("gbk","ignore")

13?????????????????????url??????
driver.current_url #????????????url

14??????????????????????????????????????????/????????? switch_to.window
#????????????????????????
driver.current_window_handle
#????????????????????????????????????
driver.window_handles
#????????????
driver.switch_to.window(handle)

?????????
#encoding=utf-8
import unittest
import time
from selenium import webdriver

class VisitSogouByIE(unittest.TestCase):
    def setUp(self):
        #??????IE?????????
        self.driver = webdriver.Ie(executable_path="D:\IEDriverServer")
    def test_operateWindowHandle(self):
        url = "http://www.baidu.com"
        #????????????
        self.driver.get(url)
        #????????????????????????
        now_handle = self.driver.current_window_handle
        #?????????????????????????????????
        print now_handle
        #?????????????????????????????????w3cschool???
        self.driver.find_element_by_id("kw").send_keys("w3cschool")
        #??????????????????
        self.driver.find_element_by_id("su").click()
        #??????3??????????????????????????????
        time.sleep(3)
        #??????w3cschool??????????????????
        self.driver.find_element_by_xpath("//div[@id=???1???]/h3/a").click()
        time.sleep(5)
        #????????????????????????
        all_handles = self.driver.window_handles
        print "+++" ,self.driver.window_handles[-1]
        #???????????????????????????????????????????????????????????????????????????
        for handle in all_handles:
            if handle != now_handle:
            #??????????????????????????????
            #self.driver.switch_to_window(handle) #selenium3.x??????????????????????????????
            # ????????????
            self.driver.switch_to.window(handle) #?????????
            #??????html5??????
            self.driver.find_element_by_link_text("HTML5").click()
            time.sleep(3)
            #??????????????????
            self.driver.close()
        time.sleep(3)
        #?????????????????????
        print now_handle
        #???????????????
        self.driver.switch_to.window(now_handle)
        time.sleep(2)
        self.driver.find_element_by_id("kw").clear()
        self.driver.find_element_by_id("kw").send_keys("wow")
        self.driver.find_element_by_id("su").click()
        time.sleep(5)
    def tearDown(self):
        self.driver.quit()

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

 











以上是关于Selenium Webdirver API???1???的主要内容,如果未能解决你的问题,请参考以下文章

webdirver.Chrom() selenium webdirver调用谷歌浏览器的问题解决

selenium webdirver测试环境搭建

自动化测试工具selenium webdirver

webdirver实例1--查找元素

Python + Selenium常用控制方法

webdirver初识_元素定位_03