import random标签:word,url,driver,element,wjuan,password,find,browser From: https://www.cnblogs.com/QQ-77Ly/p/17658351.html
import time
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.proxy import Proxy, ProxyType
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
word_lianjie_download = 'div/id=divMsg'
url = 'https://www.wjx.cn/login.aspx'
s = Service("chromdriver.exe")
# driver = webdriver.Firefox() # 初始化一个火狐浏览器实例:driver
driver = webdriver.Chrome()
driver.maximize_window() # 最大化浏览器
driver.get(url) # 通过get()方法,打开一个url站点
username = driver.find_element(By.NAME, "UserName")
username.send_keys(' ')
password = driver.find_element(By.NAME, 'Password')
password.send_keys(' ')
submit_button = driver.find_element(By.NAME, 'LoginButton')
try:
drag_element = driver.find_element(By.CLASS_NAME, "nc_iconfont btn_slide")
action = ActionChains(driver)
action.click_and_hold(drag_element)
action.move_by_offset(125, 0)
action.release().perform()
except:
pass
submit_button.click()
def out_word(driver, url_wordx):
driver.get(url_wordx)
word_table = driver.find_element(By.ID, 'tbQuestion')
save_word = driver.find_element(By.NAME,'ctl04')
save_word.click()
# word_link = driver.find_element(By.ID,'divMsg')
time.sleep(15)
aa = driver.find_element(By.CLASS_NAME,'wjx_alink')
aa.click()
print(f"-------- 已生成第 {m} 个文件!")
time.sleep(3)
for n in range(1,52):
ls=[]
m=1
url_n = f'https://www.wjx.cn/NewWJX/manage/MyQuestionnaires.aspx?pageindex={n}&sortbn=t1'
driver.get(url_n)
wins = driver.window_handles
driver.switch_to.window(wins[-1])
id_w = driver.find_elements(By.XPATH,
'/html/body/div[1]/div[2]/form/div[4]/div/div/div[2]/div/div[2]//dl/dd/div[1]/dl[1]/dd/a')
print("=" * 10 + f'第{n}页' + "=" * 10)
time.sleep(random.random())
for i in id_w:
url_word = i.get_attribute('href')
url_words = url_word.replace('designstart', 'printq')
print(url_words)
ls.append(url_words)
for url_words in ls:
out_word(driver,url_words)
m+=1
m=1
print('--------- 程序完成! ---------')
# driver.quit()
# while browser.current_url != expected_url:
# time.sleep(1)
#
# if 'Login successful' in browser.page_source:
# print('Login successful')
# else:
# print('Login failed')
#
def yanzheng():
from selenium import webdriver
import time
import re
from PIL import Image
import pytesseract
# 获取动态密码
def get_dynamic_password():
# TODO: 实现获取动态密码的方法
pass
# 打开浏览器,并访问登录页面
browser = webdriver.Chrome()
browser.get('https://www.example.com/login')
# 输入用户名和密码
username = browser.find_element_by_name('username')
username.send_keys('your username')
password = browser.find_element_by_name('password')
password.send_keys('your password')
# 处理验证码
while True:
if 'captcha' in browser.page_source:
captcha_image = browser.find_element_by_xpath('//img[@alt="captcha"]')
captcha_image.screenshot('captcha.png')
captcha_input = browser.find_element_by_name('captcha')
captcha_input.clear()
image = Image.open('captcha.png')
code = pytesseract.image_to_string(image)
code = re.findall('\d+', code)[0]
captcha_input.send_keys(code)
submit_button = browser.find_element_by_id('login-submit')
submit_button.click()
time.sleep(3)
else:
break
# 处理动态密码
while True:
if 'dynamic-password' in browser.page_source:
dynamic_password = get_dynamic_password()
dynamic_password_input = browser.find_element_by_name('dynamic-password')
dynamic_password_input.send_keys(dynamic_password)
submit_button = browser.find_element_by_id('login-submit')
submit_button.click()
time.sleep(3)
else:
break
# 等待跳转到目标页面,检查是否成功登录
expected_url = 'https://www.example.com/home'
while browser.current_url != expected_url:
time.sleep(1)
if 'Login successful' in browser.page_source:
print('Login successful')
else:
print('Login failed')