qileilove

          blog已經(jīng)轉(zhuǎn)移至github,大家請(qǐng)?jiān)L問 http://qaseven.github.io/

          Selenium對(duì)瀏覽器的各種操作

           第一步就是安裝Selenium這個(gè)模塊,當(dāng)然,前提是你的python已經(jīng)安裝好了
            直接在dos窗口輸入
            pip install selenium完成一鍵安裝
            然后就可以新建一個(gè)py文件,在里面輸入
          from selenium import webdriver
          from selenium.common.exceptions import TimeoutException
          from selenium.webdriver.support.ui import WebDriverWait # available since 2.4.0
          from selenium.webdriver.support import expected_conditions as EC # available since 2.26.0
          # Create a new instance of the Firefox driver
          driver = webdriver.Firefox()
          # go to the google home page
          driver.get("http://www.google.com")
          # find the element that's name attribute is q (the google search box)
          inputElement = driver.find_element_by_name("q")
          # type in the search
          inputElement.send_keys("cheese!")
          # submit the form (although google automatically searches now without submitting)
          inputElement.submit()
          # the page is ajaxy so the title is originally this:
          print driver.title
          try:
          # we have to wait for the page to refresh, the last thing that seems to be updated is the title
          WebDriverWait(driver, 10).until(EC.title_contains("cheese!"))
          # You should see "cheese! - Google Search"
          print driver.title
          finally:
          driver.quit()
            這樣就打開google進(jìn)行查找cheese!后打印標(biāo)題并關(guān)閉瀏覽器
            下面介紹各種獲取瀏覽器內(nèi)的各種元素的方法
          By ID
          <divid="coolestWidgetEvah">...</div>
          element=driver.find_element_by_id("coolestWidgetEvah")
          By Class Name
          <divclass="cheese"><span>Cheddar</span></div><divclass="cheese"><span>Gouda</span></div>
          cheeses=driver.find_elements_by_class_name("cheese")
          By Tag Name
          <iframesrc="..."></iframe>
          frame=driver.find_element_by_tag_name("iframe")
          By Name
          <inputname="cheese"type="text"/>
          cheese=driver.find_element_by_name("cheese")
          By Link Text
          <a >cheese</a>>
          cheese=driver.find_element_by_link_text("cheese")
          By Partial Link Text
          <a >search for cheese</a>
          cheese=driver.find_element_by_partial_link_text("cheese")
          By CSS
          <divid="food"><spanclass="dairy">milk</span><spanclass="dairy aged">cheese</span></div>
          cheese=driver.find_element_by_css_selector("#food span.dairy.aged")
          By XPATH
          <inputtype="text"name="example"/><INPUTtype="text"name="other"/>
          inputs=driver.find_elements_by_xpath("http://input")
            (相關(guān)的XPATH教程具體可以參考W3C的教程進(jìn)行學(xué)習(xí))
            Using JavaScript
            這么這是兩個(gè)例子,例子一需要提前加載jqury的支持
            element=driver.execute_script("return $('.cheese')[0]")
            labels=driver.find_elements_by_tag_name("label")inputs=driver.execute_script("var labels = arguments[0], inputs = []; for (var i=0; i < labels.length; i++){"+"inputs.push(document.getElementById(labels[i].getAttribute('for'))); } return inputs;",labels)
            User Input - Filling In Forms
            select=driver.find_element_by_tag_name("select")allOptions=select.find_elements_by_tag_name("option")foroptioninallOptions:print"Value is: "+option.get_attribute("value")option.click()
            有些選擇提取的元素是需要進(jìn)行篩選的如
            # available since 2.12fromselenium.webdriver.support.uiimportSelectselect=Select(driver.find_element_by_tag_name("select"))select.deselect_all()select.select_by_visible_text("Edam")
            那么這些form進(jìn)行如何提交呢?
            driver.find_element_by_id("submit").click()
            element.submit()
            Moving Between Windows and Frames
            <a href="somewhere.html"target="windowName">Click here to open a new window</a>
            driver.switch_to_window("windowName")
            driver.switch_to_frame("frameName")
            driver.switch_to_frame("frameName.0.child")
            切換一些彈出來的比如登錄框等
            Popup Dialogs
            這類的有alerts, confirms, and prompts等
            alert=driver.switch_to_alert()
            返回的是一個(gè)彈窗的窗體對(duì)象
            Cookies
            # Go to the correct domaindriver.get("http://www.example.com")# Now set the cookie. Here's one for the entire domain# the cookie name here is 'key' and its value is 'value'driver.add_cookie({'name':'key','value':'value','path':'/'})# additional keys that can be passed in are:# 'domain' -> String,# 'secure' -> Boolean,# 'expiry' -> Milliseconds since the Epoch it should expire.# And now output all the available cookies for the current URLforcookieindriver.get_cookies():print"%s -> %s"%(cookie['name'],cookie['value'])# You can delete cookies in 2 ways# By namedriver.delete_cookie("CookieName")# Or all of themdriver.delete_all_cookies()
            Changing the User Agent
            profile=webdriver.FirefoxProfile()profile.set_preference("general.useragent.override","some UA string")driver=webdriver.Firefox(profile)
            Drag And Drop
          fromselenium.webdriver.common.action_chainsimportActionChainselement=driver.find_element_by_name("source")target=driver.find_element_by_name("target")ActionChains(driver).drag_and_drop(element,target).perform()
            具體的詳細(xì)用法可以參考官方文檔:http://docs.seleniumhq.org/docs/03_webdriver.jsp

          posted on 2014-07-31 09:55 順其自然EVO 閱讀(1226) 評(píng)論(0)  編輯  收藏 所屬分類: 測(cè)試學(xué)習(xí)專欄selenium and watir webdrivers 自動(dòng)化測(cè)試學(xué)習(xí)

          <2014年7月>
          293012345
          6789101112
          13141516171819
          20212223242526
          272829303112
          3456789

          導(dǎo)航

          統(tǒng)計(jì)

          常用鏈接

          留言簿(55)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 元阳县| 凌云县| 临江市| 天等县| 益阳市| 瓦房店市| 喀什市| 行唐县| 鱼台县| 张掖市| 新巴尔虎左旗| 高青县| 长垣县| 百色市| 沾化县| 龙岩市| 普兰店市| 晋中市| 灵台县| 即墨市| 凤城市| 遵化市| 定结县| 始兴县| 曲沃县| 靖边县| 建平县| 望城县| 朝阳市| 布拖县| 泊头市| 象山县| 思南县| 丹凤县| 大同市| 武邑县| 苗栗县| 门源| 彭阳县| 吐鲁番市| 渭源县|