代码拉取完成,页面将自动刷新
from module import *
from selenium import webdriver
import logging, re
from lxml import etree
# # 加启动配置
# option = webdriver.ChromeOptions()
# # 关闭“chrome正受到自动测试软件的控制”
# option.add_experimental_option('useAutomationExtension', False)
# option.add_experimental_option('excludeSwitches', ['enable-automation'])
# # 不自动关闭浏览器
# option.add_experimental_option("detach", True)
# driver 必须写在全局函数中,如果写在局部函数内,调用函数执行完毕后,函数内的变量自动清除,
# 浏览器会关闭
#加启动配置
option = webdriver.ChromeOptions()
option.add_experimental_option('w3c', False)
option.add_experimental_option('useAutomationExtension', False)
option.add_experimental_option('excludeSwitches', ['enable-automation'])
#不自动关闭浏览器
option.add_experimental_option("detach", True)
driver = webdriver.Chrome(options=option)
def first_open(url):
open_web(driver, url)
elements = find_element(driver)
action_login_nocode(driver, elements)
action_login_code(driver, elements)
n = 1
while 1:
time.sleep(1)
try:
driver.implicitly_wait(1) # 隐式等待10秒
record = driver.find_element_by_class_name('recordItem')
print('登录成功')
break
except:
n += 1
print(f'登录失败,第{n}次重试...')
code = input('请输入验证码:')
action_login(driver, elements, code)
if n > 5:
print('重试超过5次,登录失败,请手动输入。。。')
# input('waiting..')
WebDriverWait(driver, 10).until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, '.recordItem')))
def get_info(driver):
# print('hello')
html = driver.find_element_by_xpath("//*").get_attribute("outerHTML")
with open('1.html', 'w', encoding='utf-8') as fp:
fp.write(html)
# 不要用 driver.page_source,那样得到的页面源码不标准
titles = re.findall(r'<div.*?class="van-cell__value"><span.*?>(.*?)</span></div>', html, re.DOTALL)
print(titles)
name = titles[0]
manager = titles[3]
phone = titles[4]
print(name, manager, phone)
# logging.info(titles)
def go_detail(driver):
# 点击检查按钮
tabItems = driver.find_elements_by_xpath("//li[@class='tab-item']")
check = tabItems[1]
check.click()
time.sleep(1)
# 获取tab菜单项
uncheck = driver.find_elements_by_xpath('//span[@class="van-ellipsis"]')
# 单击第一项
uncheck[0].click()
# 获取搜索栏
input_tag = driver.find_element_by_xpath('//input[@class="van-field__control"]')
# 单击搜索栏
input_tag.click()
# 清空搜索栏
input_tag.clear()
print('已清空文字')
# 输入新文字
input_tag.send_keys('hello2')
time.sleep(1)
# print(uncheck[1].get_attribute('outerHTML'))
# 点击tab第二项,未检查
uncheck[1].click()
time.sleep(1)
# 获取tab详情列表项(第一项为主页,为未检查单位列表,第二项为已检查未录入单位列表)
item = driver.find_elements_by_xpath('//div[@class="van-list"]')
print(item[1].get_attribute('outerHTML'))
# 单击第二项,第一个已检查但未录入的单位
item[1].click()
time.sleep(2)
# 获取检查录入按钮
check_button = driver.find_elements_by_xpath('//span[@class="van-actionsheet__name"]')
# 单击检查按钮
check_button[0].click()
if __name__ == '__main__':
flag = True
# 如果已经打开
first_open(URL)
# exit()
# 获得打开后的网页driver
driver = get_web()
go_detail(driver)
time.sleep(5)
# get_info(driver)
# get_info(driver)
# inputs = driver.find_elements_by_xpath("//input[@class='van-field__control']")
# inputs[0].clear().send_keys('万习')
# inputs[1].clear().send_keys('12344555')
# print('hello')
# input('waiting.')
# name, phone, manager = write_info(driver)
# click_all(driver, name)
#
# driver.find_elements_by_xpath('//div[@class="saveBtn"]')[0].click()
input("waiting....")
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。