我们封装了selenium的一些常用方法到yqsel.py。
# -*- coding: utf-8 -*-
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import NoAlertPresentException
from selenium.webdriver.support import expected_conditions as EC
import time
"we want to package the selenium class to make it easier to use"
class YQSel(object):
def __init__(self, browser='ff'):
if browser == "firefox" or browser == "ff":
# profile = webdriver.FirefoxProfile()
# profile.set_preference('network.proxy.type', 1)
# profile.set_preference('network.proxy.http', '192.168.210.55')
# profile.set_preference('network.proxy.http_port', 51688)
# profile.set_preference('network.proxy.ssl', '192.168.210.55')
# profile.set_preference('network.proxy.ssl_port', 51688)
# profile.update_preferences()
# driver = webdriver.Firefox(profile)
driver = webdriver.Firefox()
elif browser == "chrome":
driver = webdriver.Chrome()
elif browser == "internet explorer" or browser == "ie":
driver = webdriver.Ie()
try:
self.driver = driver
except Exception:
raise NameError("Not found %s browser,You can enter 'ie', 'ff', or 'chrome'." % browser)
def element_wait(self, css, secs=5):
if "=>" not in css:
raise NameError("Positioning syntax errors, lack of '=>'.")
by = css.split("=>")[0]
value = css.split("=>")[1]
if by == "id":
WebDriverWait(self.driver,secs,1).until(EC.presence_of_element_located((By.ID, value)))
elif by == "name":
WebDriverWait(self.driver,secs,1).until(EC.presence_of_element_located((By.NAME, value)))
elif by == "class":
WebDriverWait(self.driver,secs,1).until(EC.presence_of_element_located((By.CLASS_NAME, value)))
elif by == "link_text":
WebDriverWait(self.driver,secs,1).until(EC.presence_of_element_located((By.LINK_TEXT, value)))
elif by == "xpath":
WebDriverWait(self.driver,secs,1).until(EC.presence_of_element_located((By.XPATH, value)))
elif by == "css":
WebDriverWait(self.driver,secs,1).until(EC.presence_of_element_located((By.CSS_SELECTOR, value)))
else:
raise NameError("Please enter the correct targeting elements,'id','name','class','link_text','xpaht','css'.")
def get_element(self,css):
if "=>" not in css:
raise NameError("Positioning syntax errors, lack of '=>'.")
by = css.split("=>")[0]
value = css.split("=>")[1]
if by == "id":
element = self.driver.find_element_by_id(value)
elif by == "name":
element = self.driver.find_element_by_name(value)
elif by == "class":
element = self.driver.find_element_by_class_name(value)
elif by == "link_text":
element = self.driver.find_element_by_link_text(value)
elif by == "xpath":
element = self.driver.find_element_by_xpath(value)
elif by == "css":
element = self.driver.find_element_by_css_selector(value)
else:
raise NameError("Please enter the correct targeting elements,'id','name','class','link_text','xpaht','css'.")
return element
def open(self, url):
'''
open url.
Usage:
driver.open("https://www.baidu.com")
'''
self.driver.get(url)
注意下,上面注释的部分很有意思,我们之前的测试站点访问一个微信公招,在浏览器上需要代理访问。这段code可以处理这个问题。