selenium 操控瀏覽器

如果是火狐瀏覽器那就最好啦,因爲不用下載額外控件
#-*-coding:utf-8 -*-
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium import webdriver
import time
import os
import urllib,urllib2

import sys
reload(sys)
sys.setdefaultencoding('utf-8')

#不用下載額外的插件
driver = webdriver.Firefox()
driver.get('http://www.baidu.com')
assert u'百度' in driver.title



elem = driver.find_element_by_name("wd")
elem.send_keys(u"天空之城")
#elem.send_keys(Keys.RETURN)
time.sleep(5)
driver.close()


driver.quit()



如果是谷歌瀏覽器:

#需要下載chromedriver.exe
chromedriver = "C:\Program Files (x86)\Google\Chrome\Application\chromedriver.exe"
os.environ["webdriver.chrome.driver"] = chromedriver


driver = webdriver.Chrome(chromedriver)
driver.get('http://www.baidu.com')
assert u'百度' in driver.title
elem = driver.find_element_by_name("wd")
elem.send_keys("haha")

#elem.send_keys(Keys.RETURN)


driver.close()
driver.quit()

chromedriver.exe下載


如果是PhantomJS(無UI瀏覽器):

下載、安裝地址:http://phantomjs.org/download.html ,要下載一個phantomjs.exe,並設置好環境變量。

下面代碼實現了,用PhantomJS解析JS,獲取驗證碼URL,並下載驗證碼的功能:

# #coding:utf-8  
from PyQt4 import QtGui
from PyQt4 import QtCore
from PyQt4.QtWebKit import*
from PyQt4 import QtCore, QtGui
from PyQt4 import uic
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtWebKit import *
from PyQt4.QtNetwork import *
import time,datetime
from bs4 import BeautifulSoup
import urllib,urllib2,httplib,cookielib
import os,sqlite3,re
import uuid
import hashlib
import sys
import ctypes
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium import webdriver

reload(sys)
sys.setdefaultencoding('utf-8')

driver = webdriver.PhantomJS()

#獲取驗證碼	
myindex = 0
cnt_num = 4
title = u'泰潤'
login_url = "http://52.74.222.142:8212/sscne12313f/user/login.html.auth"
while True:
    driver.get(login_url)
    #坐等加載好
    time.sleep(5)
    try:
        img_src = driver.find_element_by_id('img').find_element_by_tag_name('img').get_attribute('src')
        print img_src
        img_file = urllib2.urlopen(img_src)
        file = './checkcodejpg/checkcode'+str(myindex)+'.jpg'
        print myindex,file
        jpg = open(file,'wb')
        jpg.write(img_file.read())
        jpg.close()
        myindex += 1
    except Exception,e:
        print e
        



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章