【Selenium Python】
2、文档:readthedocs版、github版API。貌似后者只是前者的一个章节。
【Selenium Python WebDriver】
1、设置浏览器窗口大小:WebDriver.set_window_size。
2、获取cookie:WebDriver.get_cookies。
3、保存当前页面:WebDriver.page_source。
4、保存当前页面截图:WebDriver.save_screenshot。
5、元素查找:WebDriver.find_element_by_*。
6、去掉链接的新标签页打开属性(target="_blank"):
1
2
|
aEle
=
Driver.find_elements_by_tag_name(
'a'
):
Driver.execute_script(
"arguments[0].removeAttribute('target')"
, aEle)
|
【Selenium Python WebElement】
1、文本框清空:WebElement.clear。
2、文本框填写:WebElement.send_keys。
3、按钮(元素)点击:WebElement.click。
4、定位元素坐标:WebElement.location。
5、获取元素的宽度和高度:WebElement.size。
6、保存元素截图:element.screenshot。
【Selenium Python Firefox】
1、Firefox驱动:https://github.com/mozilla/geckodriver/releases。
2、禁止firefox加载图片:Disable images in Selenium Python。
3、Firefox设置代理:
1
2
3
4
5
|
profile
=
webdriver.FirefoxProfile()
profile.set_preference(
"network.proxy.type"
,
1
)
profile.set_preference(
"network.proxy.http"
, ProxyAddr)
profile.set_preference(
"network.proxy.http_port"
, ProxyPort)
Driver
=
webdriver.Firefox(profile)
|
4、如果不在环境变量中设置驱动路径,可以用executable_path指定驱动文件路径:
1
|
webdriver.Firefox(executable_path
=
r
'E:\webdriver\geckodriver.exe'
)
|
【Selenium Python Chrome】
1、Chrome驱动:https://sites.google.com/a/chromium.org/chromedriver/home。
2、禁止Chrome加载图片:Python: Disable images in Selenium Google ChromeDriver、selenium设置:浏览器不显示图片。
3、如果不在环境变量(Path)中设置驱动路径,可以用executable_path指定驱动文件路径:
1
|
webdriver.Chrome(executable_path
=
r
'E:\webdriver\chromedriver.exe'
)
|
4、selenium3、chorme55下的一些配置。
1
2
3
4
5
6
7
8
|
chromeOptions
=
webdriver.ChromeOptions()
prefs
=
{}
#禁用图片
prefs[
'profile.managed_default_content_settings.images'
]
=
2
#设置下载目录
prefs[
'download.default_directory'
]
=
'D:/tmp'
chromeOptions.add_experimental_option(
"prefs"
,prefs)
browser
=
webdriver.Chrome(chrome_options
=
chromeOptions)
|
【相关阅读】
2、Python将浏览器cookies共享给requests库
3、数据抓取的艺术(一):Selenium+Phantomjs数据抓取环境配置
4、Skip image loading by selenium
5、Selenium WebDriver go to page without waiting for page load
6、Get HTML Source of WebElement in Selenium WebDriver using Python
7、How to print the contents in the “ui”,“li” tags using selenium web driver
*** walker * Updated 2016-12-2 ***