Selenium with Python中文翻译(八)
来源:互联网 发布:linux 启动脚本 编辑:程序博客网 时间:2024/05/20 21:21
8.常见问题
其它的常见问题: https://github.com/SeleniumHQ/selenium/wiki/Frequently-Asked-Questions
8.1.如何使用ChromeDriver?
从下载页面下载最新的chromedriver。解压下载的文件:
unzip chromedriver_linux32_x.x.x.x.zip
你应该能看到可执行的chrome driver
。现在你可以像下面那样创建一个Chrome Driver:
driver = webdriver.Chrome(executable_path="/path/to/chromedriver")
其余的例子也能像其它文档给出的那样工作。
8.2.Selenium 2支持XPath 2.0吗?
参考:http://seleniumhq.org/docs/03_webdriver.html#how-xpath-works-in-webdriver
Selenium能否支持XPath取决于浏览器的Xpath引擎,所以Selenium支持所有浏览器所支持的XPath。如果浏览器没有XPath引擎,那么Selenium只会支持XPath1.0。
8.3.如何滚到页面底部?
参考:http://blog.varunin.com/2011/08/scrolling-on-pages-using-selenium.html
你可以用execute_script
方法在加载的页面中使用kavascript。所以,你可以通过调用Javascript API 来滚动到页面底部或者其它地方。
下面是一个滚动到页面底部的例子:
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
窗口对象在DOM里有一个scrollTo方法能够滚动到页面的很多地方。scrollHeight是所有元素的共有属性。document.body.scrollHeight
会给出整个页面的高度。
8.4.如何定制Firefox配置文件来自动存储文件?
参考: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模块来识别:
import requestscontent_type = requests.head('http://www.python.org').headers['content-type']print(content_type)
当文本类型被识别后,你可以设置firefox配置文件首选项:
browser.helperApps.neverAsk.saveToDisk
举个例子:
import osfrom selenium import webdriverfp = 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()
browser.download.dir
指定你要下载的文件的目录。
8.5.如何将文件上传到文件输入?
选择<input type="file">
元素然后调用传输文件路径方法,文件路径为相对于测试脚本的相对路径或者是绝对路径。在windows或者linux系统中要注意路径名的区别。
8.6.如何在Firefox中使用firebug?
首先要先下载Firebug XPI文件,然后为firefox配置文件调用可用的add_extension
方法:
from selenium import webdriverfp = webdriver.FirefoxProfile()fp.add_extension(extension='firebug-1.8.4.xpi')fp.set_preference("extensions.firebug.currentVersion", "1.8.4") #Avoid startup screenbrowser = webdriver.Firefox(firefox_profile=fp)
8.7.如何在当前窗口中使用截图?
利用webdriver提供的save_screenshot
方法:
from selenium import webdriverdriver = webdriver.Firefox()driver.get('http://www.python.org/')driver.save_screenshot('screenshot.png')driver.quit()
如有错误,敬请指正!
为什么没有七呢?因为第七个章节都是API的介绍和用法,太长了,需要的话可以自己看。
- Selenium with Python中文翻译(八)
- Selenium with Python中文翻译(一)
- Selenium with Python中文翻译(二)
- Selenium with Python中文翻译(三)
- Selenium with Python中文翻译(四)
- Selenium with Python中文翻译(五)
- Selenium with Python中文翻译(六)
- selenium-webdriver(python) (八)
- Selenium with Python 文档
- Selenium Documentation 中文翻译
- Selenium-AndroidDriver(python) (八) --- 无法输入中文
- 轻松自动化---selenium-webdriver(python) (八)
- 《Getting Started with D3》中文翻译(第一章)
- Programming Computer Vision with Python (学习笔记八)
- Selenium with Python: Unresolved Import Issues
- Scraping with Python Selenium and PhantomJS
- 《Getting Started with D3》中文翻译(第二章)
- python wsgi pep333 中文翻译
- Search Insert Position
- wget命令参数用法总结
- 2017乌鲁木齐网络赛D题 区间dp
- appium并发测试
- jquery操作复选框的总结
- Selenium with Python中文翻译(八)
- js的call方法和apply方法
- POJ 3259Wormholes
- nohup command 2>&1 &的解释
- Tools(三)——CollsionDetection
- javascript对字符串的操作
- Python matplotlib库scatter函数参数详解
- 第二周 程序的多文件组织
- 算法和数据结构可视化教程