pythonweb自動化測試實例_[轉載]python?webdriver自動化測試實例

python webdriver自動化測試初步印象

以下示例演示啟動firefox,瀏覽google.com,搜索Cheese,等待搜索結果,然后打印出搜索結果頁的標題

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()

python webdriver自動化測試通過控件xpath定位元素

有一段html代碼如下:

現在通過xpath來查找到相應的input元素,代碼demo如下:

from selenium.webdriver.common.by import By

#查找所有input元素方法一

inputs = driver.find_elements_by_xpath("//input")

#查找所有input元素方法二

inputs = driver.find_elements(By.XPATH, "//input")

#查找指定的input元素,比如查找name為other的input

inputs =

driver.find_element_by_xpath("//input[@name='other']")

python webdriver自動化測試在window和frame之間切換

from selenium import webdriver

# 啟動firefox初始化webdriver

driver = webdriver.Firefox()

1、切換到指定窗口名的窗口,

例如有一段html代碼如下

driver.switch_to_window("windowName")

2、當然也可以通過句柄來切換,示例如下

for handle in driver.window_handles:

driver.switch_to_window(handle)

上述代碼會遍歷,一個個的切換。

3、通過frame名稱切換到指定的frame

driver.switch_to_frame("frameName")

4、也可以通過frame的索引來切換

driver.switch_to_frame(0) #切換到第一個frame

python使用selenium rc和webdriver啟動不同瀏覽器的方法

Selenium 1 -啟動 Internet Explorer

from selenium import selenium

selenium = selenium("localhost", 4444, "*iexplore",

"http://google.com/")

selenium.start()

Selenium 1 - 啟動Firefox

from selenium import selenium

selenium = selenium("localhost", 4444, "*firefox",

"http://google.com/")

selenium.start()

webdriver - 啟動Firefox

from selenium import webdriver

driver = webdriver.Firefox()

webdriver - 啟動Chrome

from selenium import webdriver

driver = webdriver.Chrome()

webdriver - 啟動Remote

from selenium import webdriver

driver = webdriver.Remote( browser_name="firefox",

platform="any")

webdriver - 啟動IE

from selenium import webdriver

driver = webdriver.Ie()

備注: 除了啟動IE外,webdriver啟動其他瀏覽器均需安裝相應瀏覽器的驅動組件,關于這塊的環境搭建請參見

python webdriver自動化測試通過控件css定位元素

有一段html代碼如下:

milkcheese

現在通過css來查找到相應的span元素,代碼demo如下:

from selenium.webdriver.common.by import By

#查找css為span元素方法一

cheese = driver.find_element_by_css_selector("#food

span.dairy.aged")

#查找css為span元素方法二

cheese = driver.find_element(By.CSS_SELECTOR, "#food

span.dairy.aged")

python webdriver自動化測試通過控件 Partial Link Text定位元素

有一段html代碼如下:

現在通過Partial Link Text來查找到相應的a元素,代碼demo如下:

from selenium.webdriver.common.by import By

#查找Partial Link Text為a元素方法一

cheese =

driver.find_element_by_partial_link_text("cheese")

#查找Partial Link Text為a元素方法二

cheese = driver.find_element(By.PARTIAL_LINK_TEXT,

"cheese")

python webdriver自動化測試通過控件link text定位元素

有一段html代碼如下:

現在通過link text來查找到相應的a元素,代碼demo如下:

from selenium.webdriver.common.by import By

#查找link text為cheese元素方法一

cheese = driver.find_element_by_link_text("cheese")

#查找link text為cheese元素方法二

cheese = driver.find_element(By.LINK_TEXT, "cheese")

python webdriver自動化測試通過控件tag name定位元素

有一段html代碼如下:

現在通過tag name來查找到相應的iframe元素,代碼demo如下:

from selenium.webdriver.common.by import By

#查找tag name為iframe元素方法一

frame = driver.find_element_by_tag_name("iframe")

#查找tag name為iframe元素方法二

frame = driver.find_element(By.TAG_NAME, "iframe")

python webdriver自動化測試通過控件class name定位元素

有一段html代碼如下:

Cheddar

Gouda

現在通過class name來查找到相應的div元素,代碼demo如下:

from selenium.webdriver.common.by import By

#查找第一個class為cheese的div元素

cheeses = driver.find_elements_by_class_name("cheese")

# 查找所有class為cheese的div元素

cheeses = driver.find_elements(By.CLASS_NAME, "cheese")

python webdriver自動化測試通過控件name定位元素

#導入webdriver

from selenium import webdriver

# 啟動firefox初始化webdriver

# ie: driver = webdriver.Ie()

# chrome: driver = webdriver.Chrome()

driver = webdriver.Firefox()

# 訪問baidu官網

driver.get("http://www.baidu.com")

# 定位輸入框

element= driver.find_element_by_name("wd")

# 打印出來看一下

# 會看到一個內存地址,說明已經找到

print element

# 關閉瀏覽器、退出webdriver

driver.quit()

python webdriver自動化測試通過控件id定位元素

#導入webdriver

from selenium import webdriver

# 啟動firefox初始化webdriver

# ie: driver = webdriver.Ie()

# chrome: driver = webdriver.Chrome()

driver = webdriver.Firefox()

# 訪問baidu官網

driver.get("http://www.baidu.com")

# 定位輸入框

element = driver.find_element_by_id("kw")

# 打印出來看一下

# 會看到一個內存地址,說明已經找到

print element

# 關閉瀏覽器、退出webdriver

driver.quit()

python webdriver自動化測試訪問某個網址

python webdriver自動化測試訪問指定網址示例

#導入webdriver

from selenium import webdriver

# 啟動firefox初始化webdriver

# ie: driver = webdriver.Ie()

# chrome: driver = webdriver.Chrome()

driver = webdriver.Firefox()

# 訪問google官網

# 記得網址前最好帶http

driver.get("http://www.google.com")

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/542169.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/542169.shtml
英文地址,請注明出處:http://en.pswp.cn/news/542169.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

repeated_Ruby中帶有示例的Array.repeated_combination()方法

repeatedArray.repeated_combination()方法 (Array.repeated_combination() Method) In this article, we will study about Array.repeated_combination() method. You all must be thinking the method must be doing something which is related to creating combinations o…

ApacheHttpServer修改httpd.conf配置文件

轉自:https://blog.csdn.net/dream1120757048/article/details/77427351 1. 安裝完 Apache HTTP Server 之后,還需要修改一下配置文件。 Apache 的配置文件路徑如下: C:\Program Files\Apache Software Foundation\Apache2.2\conf\httpd.conf…

大學物理實驗電學基本參數的測量實驗報告_大學物理電學實驗報告

技校網專門為您推薦的類似問題答案問題1:怎樣寫大學計算機基礎有關制作個人簡歷的實驗報告一、實驗名稱:個人簡歷的制作 二、實驗目的與要求: 1、熟悉Word 2003的基本操作 2、掌握利用網絡搜索獲得個人簡歷所需的資料 3、培養同學們動手能力和自學能力。…

python 線程模塊_Python線程模塊| main_thread()方法與示例

python 線程模塊Python threading.main_thread()方法 (Python threading.main_thread() Method) main_thread() is an inbuilt method of the threading module in Python. It is used to return the main Thread object. It is the thread from which the Python interpreter …

linux中系統修復

1. 引導文件丟失 (1)引導文件所在路徑 /boot/grub2/grub.cfg 需提前知道根目錄所在分區和內核版本 uname -r 查詢內核版本命令 模擬問題 rm -fr /boot/grub2/grub.cfg 一不小心把這玩意兒給刪了,還reboot了 完了以后機子開不了了就這情況 …

dw相對路徑怎么改_密云ETL怎么收費

密云ETL怎么收費,派客動力,公司依托自有產品,整合行業資源,構建先進的數據管理解決方案,解決企業和組織的核心數據問題以及被影響的業務挑戰。這種工具我都使用過,優點有:圖形界面,開…

python 自動化之路 day 08_2 網絡編程

本節內容 Socket介紹Socket參數介紹基本Socket實例Socket實現多連接處理通過Socket實現簡單SSH通過Socket實現文件傳送作業:開發一個支持多用戶在線的FTP程序1. Socket介紹 概念 A network socket is an endpoint of a connection across a computer network. Today…

查看scala變量數據類型_Scala文字,變量和數據類型| Scala編程教程

查看scala變量數據類型1)Scala數據類型 (1) Scala Data Types) Scala has the same set of data types as in Java. The traditional 14 data types are inherited as it is in Scala. Scala具有與Java中相同的數據類型集。 傳統的14種數據類型在Scala中被繼承。 The Followin…

Elasticsearch過濾與聚合的先后順序java實現

2019獨角獸企業重金招聘Python工程師標準>>> 一、Elasticsearch的聚合 ES的聚合相當于關系型數據庫里面的group by,例如查找在性別字段男女人數的多少并且按照人數的多少進行排序,在使用MySQL的時候,可以使用如下的句子 select se…

js手機號中間四位_11位手機號碼隱藏中間四位數,學會Substitute函數一鍵搞定!...

相信許多朋友都有見過手機號碼被*號隱藏中間四位數的情況。許多地方為了保護個人信息,都會將手機號的中間四位數用星號代替。如上圖所示,我們需要將原來的手機號碼,通過*號的方式變為隱藏后的加密模式。下面我們就來學習一下如何利用substitu…

python 整數最大_Python程序使用floor()方法查找最大整數

python 整數最大The greatest integer function is a function (real numbers function) to itself that is defined as follows: it sends any real number to the largest integer that is less than or equal to it. 最大整數函數是一個對其自身定義的函數(實數函數)&#x…

selinux對ftp的影響

1.啥是selinux 安全增強型Linux(Security-Enhanced Linux)簡稱selinux,它是一個Linux內核模塊,也是Linux的一個安全子系統。 selinux的狀態: Enforcing:強制模式,在selinux運作時,已經開始限制d…

ES6的class方法基本用法

為什么80%的碼農都做不了架構師?>>> 在ES5中我們通常通過構造函數,定義并生成新對象。 例如: function Point(name,age){this.namename;this.ageage;}Point.prototype{Who:function(){return "My name is "this.name",My age…

celery的中文_celery異步任務框架

目錄Celery一、官方二、Celery異步任務框架Celery架構圖消息中間件任務執行單元任務結果存儲三、使用場景四、Celery的安裝配置五、兩種celery任務結構:提倡用包管理,結構更清晰七、Celery執行異步任務包架構封裝八、基本使用celery.py 基本配置tasks.py…

關于linux mv指令機制

最近在mv文件的時候,操作失誤將生產服務器一個1TB的文件夾mv到了/opt/test目錄,因為最后/opt/目錄被沾滿所以1TB的文件夾沒有遷移過來,寫入了30GB數據到了/opt/test目錄,因為系統分區被沾滿,所以把test目錄給刪除了。 …

數據庫的管理

1. 數據庫的簡介 定義:數據庫(Database)就是一種按數據結構來組織,存儲和管理數據的倉庫,其中包含數據挖掘,大數據信息的推送。 mariadb數據庫管理系統是mysql的一個分支,主要由開源社區在維護&…

C#中的Dictionary字典類介紹(轉載)

C#中的Dictionary字典類介紹 關鍵字:C# Dictionary 字典 作者:txw1958原文:http://www.cnblogs.com/txw1958/archive/2012/11/07/csharp-dictionary.html 說明 必須包含名空間System.Collection.Generic Dictionary里面的每一個元素都…

求階乘的第一個非零數字_查找數字階乘中的尾隨零

求階乘的第一個非零數字Problem statement: 問題陳述: Find the number of trailing zeros in n! (Where, n is the given input). 在n中找到尾隨零的數目! (其中, n是給定的輸入)。 Solution: 解: Computing a factorial is o…

高速緩存dns

1. DNS: Domain Name System,域名系統。 萬維網上作為域名和IP地址相互映射的一個分布式數據庫,能夠使用戶更方便的訪問互聯網。他主要負責把域名和IP的相互轉換,DNS運行與TCP|UDP的53端口上。 2. 高速緩存DNS:DNS服務…

python log日志級別_python – 日志記錄:如何為處理程序設置最大日志級別

您可以向文件處理程序添加過濾器.這樣,您可以將特定級別重定向到不同的文件.import loggingclass LevelFilter(logging.Filter):def __init__(self, low, high):self._low lowself._high highlogging.Filter.__init__(self)def filter(self, record):if self._low < recor…