|
楼主 |
发表于 2020-8-10 10:54:11
|
显示全部楼层
当然可以 谢谢指点
#!/usr/bin/env python
# coding:utf-8
import requests
import time
import json
import pyautogui
from selenium import webdriver
import selenium.webdriver.support.ui as ui
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
class zhixing(object):
def dianji(leixing,zhi):
print(leixing,zhi)
# 定位到元素
#double_click = browser.find_element_by_id('xx')
if "id" in leixing:
driver.find_element_by_id(zhi).click()
print(leixing,zhi,"点击完毕")
elif "name" in leixing:
driver.find_element_by_name(zhi).click()
print(leixing,zhi,"点击完毕")
elif "xpath" in leixing:
driver.find_element_by_xpath(zhi).click()
print(leixing,zhi,"点击完毕")
elif "class" in leixing:
driver.find_element_by_class_name(zhi).click()
print(leixing,zhi,"点击完毕")
elif "link" in leixing:
driver.find_element_by_link_text(zhi).click()
print(leixing,zhi,"点击完毕")
elif "tag" in leixing:
driver.find_element_by_tag_name(zhi).click()
print(leixing,zhi,"点击完毕")
elif "css" in leixing:
driver.find_element_by_css_selector(zhi).click()
print(leixing,zhi,"点击完毕")
elif "wenzi" in leixing:
driver.find_element_by_link_text(zhi).click()
print(leixing,zhi,"点击完毕")
elif "jpg" in leixing:
jpgxy=pyautogui.locateCenterOnScreen(zhi)
pyautogui.doubleClick(x=jpgxy[0]-1,y=jpgxy[1],button="left")
print("图片点击完毕")
else:
print('请检查,',leixing,zhi,'内容是否正确')
#执行反馈
return
def shuru(leixing,zhi,neirong):
print(leixing,zhi)
if "id" in leixing:
driver.find_element_by_id(zhi).send_keys(neirong)
print("id输入完毕")
elif "name" in leixing:
driver.find_element_by_name(zhi).send_keys(neirong)
print("name输入完毕")
elif 'xpath' in leixing:
driver.find_element_by_xpath(zhi).send_keys(neirong)
print("xpath输入完毕")
elif 'input' in leixing:
driver.find_element_by_tag_name(leixing).send_keys(zhi)
print("xpath输入完毕")
else:
print('请检查,',shuru,'内容是否正确')
#执行反馈
return
def qqqiyehao(www,username,password):
driver.get(www)#打开网址
try:
with open('anquan.txt', 'r', encoding='utf8') as f:
listCookies = json.loads(f.read())
print('%%%%%%%%%%%%%%%%%', listCookies)
for cookie in listCookies:
driver.add_cookie(cookie)
driver.get(www)
# 读取完cookie刷新页面
driver.refresh()
print("调用cookie成功,尝试登录,直至登录成功")
except:
driverok=(driver.page_source)#获取源码
print(driverok)
if u"公众平台帐号登录" in driverok:
zhixing.dianji("xpath",'//*[@id="header"]/div[2]/div/div/div[2]/a')
# 输入账号密码
zhixing.shuru("name","account",username)
zhixing.shuru("name","password",password)
# 模拟点击登录
zhixing.dianji("xpath",'//*[@id="header"]/div[2]/div/div/div[1]/form/div[4]/a')
# 等待3秒
time.sleep(20)
#获取网站cookie
dictCookies = driver.get_cookies()
jsonCookies = json.dumps(dictCookies)
# print(jsonCookies)
with open('anquan.txt', 'w') as f:
f.write(jsonCookies)
print("保存cookie成功")
# 将cookies保存
else:
print('登录不成功')
link = zhixing.dianji("xpath","/html/body/div[2]/div/div[2]/div/div/div[3]/div[2]/div[3]/div[2]/div/div[1]")
time.sleep(3)
#执行到这里出问题了 因为弹出了新窗口
all_hand = driver.window_handles
print(all_hand)
driver.switch_to_window(all_hand[-1])
#driver.quit()
return
#执行反馈
if __name__ == '__main__':
desired_capabilities = DesiredCapabilities.FIREFOX
desired_capabilities["pageLoadStrategy"] = "eager"#normal 正常 eager 等解析完成 none当html下载完成
driver = webdriver.Firefox(executable_path='geckodriver.exe')
www=("https://mp.weixin.qq.com/")
username=("*******")
password=("*******")
zhixing.qqqiyehao(www,username,password)
|
|