准备

  1. from selenium import webdriver
  2. from selenium.webdriver import ActionChains
  3. from selenium.webdriver.common.by import By
  4. from selenium.webdriver.support.ui import WebDriverWait
  5. from selenium.webdriver.support import expected_conditions as ec
  6. from selenium.webdriver.chrome.options import Options
  7. class my_selenium(object):
  8. """selenim封装"""
  9. def __init__(self):
  10. chrome_options = Options()
  11. chrome_options.add_argument('--headless')
  12. chrome_options.add_argument('--disable-gpu')
  13. self.browser = webdriver.Chrome(chrome_options=chrome_options)
  14. self.wait = WebDriverWait(self.browser, 20)
  15. def get_browser(self):
  16. """返回browser"""
  17. return self.browser
  18. # region 元素操作
  19. def get_element_by_select(self, value, wait=True):
  20. """根据css选择器获取元素
  21. :param value str
  22. """
  23. if wait:
  24. return self.wait.until(ec.element_to_be_clickable((By.CSS_SELECTOR,
  25. value)))
  26. else:
  27. return By.CSS_SELECTOR(value)
  28. def get_element_by_xpath(self, value, wait=True):
  29. """根据css选择器获取元素
  30. :param value str
  31. """
  32. if wait:
  33. return self.wait.until(ec.element_to_be_clickable((By.XPATH,
  34. value)))
  35. else:
  36. return By.CSS_SELECTOR(value)
  37. def send_key(self, ele, value):
  38. """发送value"""
  39. ele.send_keys(value)
  40. def clear_key(self, ele):
  41. """清空value"""
  42. ele.clear()
  43. def get_attr(self, ele, attr_str):
  44. """获取属性值"""
  45. return ele.get_attribute(attr_str)
  46. def get_txt(self, ele):
  47. """获取属性值"""
  48. return ele.text
  49. def click(self, ele):
  50. """点击"""
  51. ele.click()
  52. def mouse_over(self, ele):
  53. """鼠标划过事件"""
  54. ActionChains(self.browser).move_to_element(ele).perform()
  55. # endregion
  56. # region 浏览器操作,前进、后退、打开、关闭
  57. def forward(self):
  58. self.browser.forward()
  59. def back(self):
  60. self.browser.back()
  61. def open_url(self, url):
  62. """打开url"""
  63. self.browser.maximize_window()
  64. self.browser.get(url)
  65. def close(self):
  66. """关闭browser"""
  67. self.browser.close()
  68. def execute_script(self, value):
  69. """执行js代码"""
  70. self.browser.execute_script(value)
  71. def get_cookies(self):
  72. """获取浏览器cookies"""
  73. return self.browser.get_cookies()
  74. # endregion
  75. if __name__ == '__main__':
  76. se = my_selenium(r'D:/document/chromedriver')
  77. se.open_url('http://www.baidu.com')
  78. hao123 = se.get_element_by_select("#u1>a:nth-child(2)")
  79. se.click(hao123)

使用示例

  1. """
  2. 设置每天自动签到
  3. jq插件:http://www.jq22.com/regokwc.aspx
  4. """
  5. from my_selenim import my_selenium
  6. # region 公共区域
  7. url = ""
  8. headers = {
  9. 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:54.0) '
  10. 'Gecko/20100101 Firefox/54.0'}
  11. browser_url = ""
  12. # region jq插件:http://www.jq22.com/regokwc.aspx
  13. def jq_login():
  14. url = "http://www.jq22.com"
  15. se = my_selenium()
  16. se.open_url(url)
  17. # with open('html.txt','w',encoding='utf-8') as f:
  18. # f.write(se.browser.page_source)
  19. log_btn = se.get_element_by_select(".fa-user")
  20. print(log_btn)
  21. se.click(log_btn)
  22. print(se.browser.title)
  23. name = se.get_element_by_select("#ema")
  24. se.send_key(name, "mr.birdofparadise@gmail.com")
  25. passwd = se.get_element_by_select("#pw")
  26. se.send_key(passwd, "JQ7tianyun")
  27. # se.browser.save_screenshot('log1.png')
  28. submit = se.get_element_by_select(".btn.btn-success")
  29. se.click(submit)
  30. se.browser.execute_script(r"window.open()")
  31. se.browser.switch_to_window(se.browser.window_handles[1])
  32. se.open_url("http://www.jq22.com/myhome")
  33. se.browser.save_screenshot('home.png')
  34. se.click(se.get_element_by_select('.fa.fa-pencil-square-o'))
  35. se.browser.save_screenshot('home2.png')
  36. se.browser.switch_to_frame(se.get_element_by_select('[src="signIn.aspx"]'))
  37. print('ok')
  38. se.click(se.get_element_by_select('#Button1'))
  39. se.close()
  40. jq_login()
  41. # endregion