1.selenium 在打开firefox后,发现程序‘死’那里了,不动了,后面的代码不执行,最后抛出异常说超时。
about:config,设置xpinstall.signatures.required为true,同样也无法生效。
#coding=utf-8
#运行环境配置
#主要配置firefox的profile文件是否可用
import os
import sys
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
gourl=\'http://www.baidu.com/\'
#获得webdriver函数
def get_webdriver():
#定制firefox的profile文件
profileDir = r\"d:\\xiaoshuo\\profile\"
profile1 = webdriver.FirefoxProfile(profileDir)
#亲们重点关注这句就好,其他更多的不用关心
br=webdriver.Firefox(profile1)
br.set_window_size(600,600)
return br
br=get_webdriver()
br.get(gourl)
2.在使用find_element_by_xxxx()查找元素时,如果元素找不到,不会返回None,而是抛出异常,你必须得自己捕获异常
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
br=webdriver.Firefox()
gourl=\'http://www.baidu.com/\'
br.get(gourl)
try:
xiaoyiye=br.find_element_by_link_text(u\'下一页\')
#找到要做的事情
except NoSuchElementException:
#找不到异常处理
print \"no next page\"
3.selenium启动firefox,如果不指定profile文件,将只能使用firefox默认配置,无法进行浏览器定制,比如不显示图片,启动广告插件等,你必须得自己配置profile,让selenium用指定配置启动
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
profileDir = r\"G:\\myproject\\python\\xiaoshuo\\profile\"
profile1 = webdriver.FirefoxProfile(profileDir)
time.sleep(1)
br=webdriver.Firefox(profile1)
gourl=\'http://www.baidu.com\'
br.get(gourl)
4.在使用firefox的 profile文件后,你会发现很多选项虽然在浏览器中进行了设置但是在通过selenium启动firefox的时候很多设置没有生效,所以你还得必须会通过代码进行配置设置来关闭图片
profileDir = r\"G:\\myproject\\python\\xiaoshuo\\profile\"
profile1 = webdriver.FirefoxProfile(profileDir)
profile1.set_preference(\'permissions.default.stylesheet\', 2)
profile1.set_preference(\'permissions.default.image\', 2)
profile1.set_preference(\'dom.ipc.plugins.enabled.libflashplayer.so\', \'false\')
br=webdriver.Firefox(profile1)
gourl=\'http://www.duzheba.cc/\'
br.get(gourl)
5. 用标签页代替弹出窗口无法设置成功
profile1.set_preference(\'browser.link.open_newwindow\',3)来搞定,那么你会发现你启动的窗口永远browser.link.open_newwindow的值永远等于2,也就是下图中的“需要新建窗口时以新建标签页代替”选项永远是没有选中的,除非手动点击一下。
C:\\Python27\\Lib\\site-packages\\selenium\\webdriver\\firefox,大家可以根据自己的机器情况进行调整webdriver_prefs.json文件,将browser.link.open_newwindow的值修改为3。
6.在firefox中,如果通过Tab page方式打开了多个页面,这时使用driver.window_handles来获得窗口句柄,你会发现永远都只有一个handle,完全无法通过driver.switch_to_window(handle)来切换Tab页面
driver.window_handles获得的句柄永远是1.而同样,如果是在chrome中,将可以获得多个句柄。#coding=utf-8
#tab页面切换测试
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
driver = webdriver.Firefox()
try:
driver.get(\"http://www.baidu.com\")
print driver.title
#新建TAB页面
driver.find_element_by_tag_name(\'body\').send_keys(Keys.CONTROL + \'t\')
driver.get(\"http://www.qq.com\")
print driver.title
#要关闭当前Tab页用的快捷键
#driver.find_element_by_tag_name(\'body\').send_keys(Keys.CONTROL + \'w\')
#多Tab页面切换
driver.find_element_by_tag_name(\'body\').send_keys(Keys.CONTROL + Keys.TAB)
print driver.title
#driver.close()
except Exception as e:
print e
下一篇:最简便的爬虫效率提升方法