python selenium webdriver 常见问题FAQ
怎么用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吗?
Selenium把xpath处理委托给了浏览器的xpath解析引擎。所以浏览器支持什么,selenium就支持什么。如果那些奇葩的浏览器没有xpath引擎的话(IE6,7,8),那么在这些大爷上selenium就只支持xpath1.0了。
怎样才能滚到页面的底部?
你可以用 execute_script方法来处理这个。 调用原生javascript的API,这样你想滚到哪里就能滚到哪里。
下面的代码演示了如何滚到页面的最下面:
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
window 对象
的 scrollTo . 方法可以滚到页面上的任何位置。 scrollHeight 是dom元素的通用属性。document.body.scrollHeight 会返回body元素的高度,基本上就是页面的高度了。
如何使用Firefox的profile来自动保存下载的文件
首先,你要保存的的文件类型你造么?
要搞清楚要自动下载的文件类型,用 curl就好了
curl -I URL | grep "Content-Type"
另一种方式是使用 requests _ module, 这样搞:
importrequestsprint requests.head('http://www.python.org').headers['content-type']
当你确定了content type之后,调用browser.helperApps.neverAsk.saveToDisk来设置firefox的profile就好了。
这是例子:
importosfrom selenium importwebdriver
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 importwebdriver
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 importwebdriver
driver=webdriver.Firefox()
driver.get('http://www.python.org/')
driver.save_screenshot('screenshot.png')
driver.quit()
如何使用默认已存在的profile启动firefox?by 乙醇
fp = webdriver.FirefoxProfile('/path/to/your/existing/profile')
browser= webdriver.Firefox(fp)
这里在windows上有个坑,就是路径分隔符在windows上是\而不是/,把这个弄明白然后指定对路径基本就可以了。
PS:这里还有另一个坑。就是如果你使用默认的profile的话,请一定关闭friefox以后再运行代码,否则会因为profile的文件锁问题而发生异常。就是说一次只能打开一个firefox实例,如果你使用默认的profile的话。