准备
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.chrome.options import Options
class my_selenium(object):
"""selenim封装"""
def __init__(self):
chrome_options = Options()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-gpu')
self.browser = webdriver.Chrome(chrome_options=chrome_options)
self.wait = WebDriverWait(self.browser, 20)
def get_browser(self):
"""返回browser"""
return self.browser
# region 元素操作
def get_element_by_select(self, value, wait=True):
"""根据css选择器获取元素
:param value str
"""
if wait:
return self.wait.until(ec.element_to_be_clickable((By.CSS_SELECTOR,
value)))
else:
return By.CSS_SELECTOR(value)
def get_element_by_xpath(self, value, wait=True):
"""根据css选择器获取元素
:param value str
"""
if wait:
return self.wait.until(ec.element_to_be_clickable((By.XPATH,
value)))
else:
return By.CSS_SELECTOR(value)
def send_key(self, ele, value):
"""发送value"""
ele.send_keys(value)
def clear_key(self, ele):
"""清空value"""
ele.clear()
def get_attr(self, ele, attr_str):
"""获取属性值"""
return ele.get_attribute(attr_str)
def get_txt(self, ele):
"""获取属性值"""
return ele.text
def click(self, ele):
"""点击"""
ele.click()
def mouse_over(self, ele):
"""鼠标划过事件"""
ActionChains(self.browser).move_to_element(ele).perform()
# endregion
# region 浏览器操作,前进、后退、打开、关闭
def forward(self):
self.browser.forward()
def back(self):
self.browser.back()
def open_url(self, url):
"""打开url"""
self.browser.maximize_window()
self.browser.get(url)
def close(self):
"""关闭browser"""
self.browser.close()
def execute_script(self, value):
"""执行js代码"""
self.browser.execute_script(value)
def get_cookies(self):
"""获取浏览器cookies"""
return self.browser.get_cookies()
# endregion
if __name__ == '__main__':
se = my_selenium(r'D:/document/chromedriver')
se.open_url('http://www.baidu.com')
hao123 = se.get_element_by_select("#u1>a:nth-child(2)")
se.click(hao123)
使用示例
"""
设置每天自动签到
jq插件:http://www.jq22.com/regokwc.aspx
"""
from my_selenim import my_selenium
# region 公共区域
url = ""
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:54.0) '
'Gecko/20100101 Firefox/54.0'}
browser_url = ""
# region jq插件:http://www.jq22.com/regokwc.aspx
def jq_login():
url = "http://www.jq22.com"
se = my_selenium()
se.open_url(url)
# with open('html.txt','w',encoding='utf-8') as f:
# f.write(se.browser.page_source)
log_btn = se.get_element_by_select(".fa-user")
print(log_btn)
se.click(log_btn)
print(se.browser.title)
name = se.get_element_by_select("#ema")
se.send_key(name, "mr.birdofparadise@gmail.com")
passwd = se.get_element_by_select("#pw")
se.send_key(passwd, "JQ7tianyun")
# se.browser.save_screenshot('log1.png')
submit = se.get_element_by_select(".btn.btn-success")
se.click(submit)
se.browser.execute_script(r"window.open()")
se.browser.switch_to_window(se.browser.window_handles[1])
se.open_url("http://www.jq22.com/myhome")
se.browser.save_screenshot('home.png')
se.click(se.get_element_by_select('.fa.fa-pencil-square-o'))
se.browser.save_screenshot('home2.png')
se.browser.switch_to_frame(se.get_element_by_select('[src="signIn.aspx"]'))
print('ok')
se.click(se.get_element_by_select('#Button1'))
se.close()
jq_login()
# endregion