Python webdriver API- 瀏覽器的操作

來源:互聯網
上載者:User

標籤:font   最大化   bsp   forward   百度   driver   orm   first   mos   

瀏覽器的操作

1)瀏覽器最大化

程式碼範例:

#coding=utf-8


from selenium import webdriver


driver = webdriver.Firefox()


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


print "瀏覽器最大化"


driver.maximize_window() #將瀏覽器最大化顯示


driver.quit()

 

2)設定瀏覽器的寬、高

程式碼範例:

#coding=utf-8


from selenium import webdriver

 

driver = webdriver.Firefox()


driver.get("http://m.mail.10086.cn")


#參數數字為像素點

print "設定瀏覽器寬480、高800顯示"


driver.set_window_size(480, 800)


driver.quit()

 

3)控制瀏覽器前進、後退

程式碼範例:

#coding=utf-8


from selenium import webdriver

import time


driver = webdriver.Firefox()


#訪問百度首頁
first_url= ‘http://www.baidu.com‘
print "now access %s" %(first_url)

driver.get(first_url)


#訪問新聞頁面
second_url=‘http://news.baidu.com‘
print "now access %s" %(second_url)
driver.get(second_url)


#返回(後退)到百度首頁
print "back to %s "%(first_url)
driver.back()


#前進到新聞頁
print "forward to %s"%(second_url)
driver.forward()


driver.quit()

 

python基礎知識補充:

>>> name = ‘huhu‘


>>> age = 26


>>> print "my name is %s" %name


my name is huhu


>>> print "my age is %d" %age


my age is 26


>>> print "my name is %d" %name


Traceback (most recent call last):
File "<pyshell#9>", line 1, in <module>
print "my name is %d" %name
TypeError: %d format: a number is required, not str


>>> print "my name is %s ,age is %d" %(name,age)
my name is huhu ,age is 26

 

在python 2 中使用print 語句進行列印輸出,如果是字元資訊的話需要對列印的資訊加單引號(‘’)
或雙引號(“”),它們本質上沒有任何區別,不過使用引號時必須要成對出現。


上面的例子中我們定義一個字串變數name 和一個資料變數age,要想在print 列印字串中引用這
兩個變數就要用到“格式化字串”的東西,在print 列印字串中指定變數類型,“%s”表示輸出的類
型為字串,“%d”表示輸出類型為整型數字。


name 為字串類型,我們用%d 來指定輸出類型就會報錯。如果我們不確定變數類型的話可以使用%r,
它的含義是“不管什麼都列印出來”。

Python webdriver API- 瀏覽器的操作

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.