博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
8. Appendix: Frequently Asked Questions
阅读量:6707 次
发布时间:2019-06-25

本文共 3458 字,大约阅读时间需要 11 分钟。

Another FAQ:

8.1. How to use ChromeDriver ?

Download the latest . Unzip the file:

unzip chromedriver_linux32_x.x.x.x.zip

You should see a chromedriver executable. Now you can create an instance of Chrome WebDriver like this:

driver = webdriver.Chrome(executable_path="/path/to/chromedriver")

The rest of the example should work as given in other documentation.

8.2. Does Selenium 2 support XPath 2.0 ?

Ref:

Selenium delegates XPath queries down to the browser’s own XPath engine, so Selenium support XPath supports whatever the browser supports. In browsers which don’t have native XPath engines (IE 6,7,8), Selenium supports XPath 1.0 only.

8.3. How to scroll down to the bottom of a page ?

Ref:

You can use the execute_script method to execute javascript on the loaded page. So, you can call the JavaScript API to scroll to the bottom or any other position of a page.

Here is an example to scroll to the bottom of a page:

driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")

The object in DOM has a method to scroll to any position of an opened window. The is a common property for all elements. The document.body.scrollHeight will give the height of the entire body of the page.

8.4. How to auto save files using custom Firefox profile ?

Ref:

Ref:

The first step is to identify the type of file you want to auto save.

To identify the content type you want to download automatically, you can use :

curl -I URL | grep "Content-Type"

Another way to find content type is using the module, you can use it like this:

import requestscontent_type = requests.head('http://www.python.org').headers['content-type'] print(content_type)

Once the content type is identified, you can use it to set the firefox profile preference: browser.helperApps.neverAsk.saveToDisk

Here is an example:

import osfrom 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()

In the above example, application/octet-stream is used as the content type.

The browser.download.dir option specify the directory where you want to download the files.

8.5. How to upload files into file inputs ?

Select the <input type="file"> element and call the send_keys() method passing the file path, either the path relative to the test script, or an absolute path. Keep in mind the differences in path names between Windows and Unix systems.

8.6. How to use firebug with Firefox ?

First download the Firebug XPI file, later you call the add_extension method available for the firefox profile:

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 screen browser = webdriver.Firefox(firefox_profile=fp)

8.7. How to take screenshot of the current window ?

Use the save_screenshot method provided by the webdriver:

from selenium import webdriverdriver = webdriver.Firefox() driver.get('http://www.python.org/') driver.save_screenshot('screenshot.png') driver.quit()

转载于:https://www.cnblogs.com/kenfang/articles/5757160.html

你可能感兴趣的文章
【转载】erlang 如何自定义 behaviour
查看>>
apache tomcat 集群 负债均衡 部署
查看>>
一步一步学Ruby(四):Ruby标准类型
查看>>
Node.js + WebSocket 实现的简易聊天室
查看>>
JSTL标签库之fn标签
查看>>
mtu检测
查看>>
在无法改动bs架构的基础上,添加新的功能(2) 浏览器
查看>>
Android 应用程序只运行一个实例
查看>>
代码整洁
查看>>
ffmpeg cmd
查看>>
网络监控
查看>>
java创建多线程的两种方法
查看>>
财务收支问题
查看>>
ADF 客户端代码调用服务器方法
查看>>
C++输入cin详解
查看>>
java与openssl的rsa算法互用
查看>>
Python strip lstrip rstrip使用方法
查看>>
Codeforces Round #268 (Div. 2) c
查看>>
如果后台的Activity由于某原因被系统回收了,如何在被系统回收之前保存当前状态?...
查看>>
postgresql 自动备份
查看>>