python selenium webdriver 常見問題FAQ
另一個FAQ: https://code.google.com/p/selenium/wiki/FrequentlyAskedQuestions
怎么用ChromeDriver ?
從這里下載最新的driver版本並解壓
# 好吧,這個命令是給linux or osx用戶准備的 # windows用戶直接手點吧 by 乙醇 unzip chromedriver_linux32_x.x.x.x.zip
你會得到一個 chromedriver
的可執行文件. 現在用下面的代碼就可以了:
driver = webdriver.Chrome(executable_path="/path/to/chromedriver") # windows如果還不行就把chrome driver扔到python的安裝目錄下 by乙醇
Selenium 2支持XPath 2.0嗎?
參考: http://seleniumhq.org/docs/03_webdriver.html#how-xpath-works-in-webdriver
Selenium把xpath處理委托給了瀏覽器的xpath解析引擎。所以瀏覽器支持什么,selenium就支持什么。如果那些奇葩的瀏覽器沒有xpath引擎的話(IE6,7,8),那么在這些大爺上selenium就只支持xpath1.0了。
怎樣才能滾到頁面的底部?
參考: http://blog.varunin.com/2011/08/scrolling-on-pages-using-selenium.html
你可以用 execute_script
方法來處理這個。 調用原生javascript的API,這樣你想滾到哪里就能滾到哪里。
下面的代碼演示了如何滾到頁面的最下面:
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
window <http://www.w3schools.com/jsref/obj_window.asp>
對象
的 scrollTo <http://www.w3schools.com/jsref/met_win_scrollto.asp>
. 方法可以滾到頁面上的任何位置。 scrollHeight <http://www.w3schools.com/jsref/dom_obj_all.asp>
是dom元素的通用屬性。document.body.scrollHeight
會返回body元素的高度,基本上就是頁面的高度了。
如何使用Firefox的profile來自動保存下載的文件
參考: http://stackoverflow.com/questions/1176348/access-to-file-download-dialog-in-firefox
參考: http://blog.codecentric.de/en/2010/07/file-downloads-with-selenium-mission-impossible/
首先,你要保存的的文件類型你造么?
要搞清楚要自動下載的文件類型,用 curl就好了
curl -I URL | grep "Content-Type"
另一種方式是使用 requests <http://python-requests.org>
_ module, 這樣搞:
import requests print requests.head('http://www.python.org').headers['content-type']
當你確定了content type之后,調用browser.helperApps.neverAsk.saveToDisk
來設置firefox的profile就好了。
這是例子:
import os from selenium import webdriver fp = webdriver.FirefoxProfile() fp.set_preference("browser.download.folderList",2) fp.set_preference("browser.download.manager.showWhenStarting",False) fp.set_preference("browser.download.dir", os.getcwd()) fp.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/octet-stream") browser = webdriver.Firefox(firefox_profile=fp) browser.get("http://pypi.python.org/pypi/selenium") browser.find_element_by_partial_link_text("selenium-2").click()
上例中, application/octet-stream
就是content type。
browser.download.dir
指定了文件自動保存的文件夾。
如何在打開Firefox的同時打開firebug ?
首先下載Firebug XPI文件(這個就是friefox的擴展程序文件--by乙醇),然后再調用firefox profile的add_extension
方法。
from selenium import webdriver fp = webdriver.FirefoxProfile() fp.add_extension(extension='firebug-1.8.4.xpi') fp.set_preference("extensions.firebug.currentVersion", "1.8.4") #Avoid startup screen browser = webdriver.Firefox(firefox_profile=fp)
怎么截圖呢?
用webdriver提供的 save_screenshot
方法:
from selenium import webdriver driver = webdriver.Firefox() driver.get('http://www.python.org/') driver.save_screenshot('screenshot.png') driver.quit()
如何使用默認已存在的profile啟動firefox?by 乙醇
參考:http://stackoverflow.com/questions/11095294/using-the-default-firefox-profile-with-selenium-webdriver-in-python
使用已存在profile啟動firefox可以自動登陸已經登陸過的站點。代碼如下:
fp = webdriver.FirefoxProfile('/path/to/your/existing/profile') browser = webdriver.Firefox(fp)
這里在windows上有個坑,就是路徑分隔符在windows上是\
而不是/
,把這個弄明白然后指定對路徑基本就可以了。
PS:這里還有另一個坑。就是如果你使用默認的profile的話,請一定關閉friefox以后再運行代碼,否則會因為profile的文件鎖問題而發生異常。就是說一次只能打開一個firefox實例,如果你使用默認的profile的話。
如何創建一個定制的profile?戳這里:https://support.mozilla.org/en-US/kb/profile-manager-create-and-remove-firefox-profiles