python selenium 常见问题列表

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

posted @ 2014-04-04 09:39  乙醇  阅读(3553)  评论(0编辑  收藏  举报

友情链接 虫师的blog  测试教程网  重定向科技  省略