from selenium.webdriver import Chrome
from selenium.webdriver.common.keys import Keys
#显示等待
from selenium.webdriver.support.wait import WebDriverWait
from time import sleep
# 加载下拉框用到的模块
from selenium.webdriver.support.select import Select
# 无头导包 (当浏览器版本大于88的时候也可用这个模块来去掉webdriver)
from selenium.webdriver.chrome.options import Options
# 事件链(鼠标移动)
from selenium.webdriver.common.action_chains import ActionChains
# web=Chrome()
# web.maximize_window()
# web.get(‘http://lagou.com‘)
# # click() 点击事件
# web.find_element_by_xpath(‘//*[@id="changeCityBox"]/p[1]/a‘).click()
# #元素定位 Keys.ENTER回车
# sleep(1)
# el= web.find_element_by_xpath(‘//*[@id="search_input"]‘).send_keys(‘python‘,Keys.ENTER)
# sleep(1)
# # find_elements_by_xpath 获取所有的li
# # list_li = web.find_elements_by_xpath(‘//*[@id="s_position_list"]/ul/li‘)
# web.find_element_by_xpath(‘//*[@id="s_position_list"]/ul/li[1]/div[1]/div[1]/div[1]/a/h3‘).click()
# # for li in list_li:
# # title=li.find_element_by_tag_name(‘h3‘).text
# # money = li.find_element_by_xpath("div[1]/div[1]/div[2]/div/span").text
# # print(title,money)
# # 切换窗口
# web.switch_to.window(web.window_handles[-1])
# sleep(1)
# # 在新窗口拿东西
# cont = web.find_element_by_xpath(‘//*[@id="job_detail"]/dd[2]/div‘).text
# print(cont)
# # 关闭子窗口
# web.close()
# # 回到原来的窗口
# web.switch_to.window(web.window_handles[0])
#########################################################
# web=Chrome()
# web.get("https://www.91kanju.com/vod-play/17873-1-1.html")
# 拿属性值
# iframe = web.find_element_by_xpath(‘//*[@id="cms_player"]/div[2]/video‘)
# a=iframe.get_attribute(‘src‘)
# https://www.91kanju.com/vod-play/1658-1-1.html
# web=Chrome()
# web.get("https://www.91kanju.com/vod-play/1658-1-1.html")
# 处理iframe 需要先切换进去然后在出来拿数据
# 先定位iframe 然后进去
# iframe = web.find_element_by_xpath(‘//*[@id="player_iframe"]‘)
# web.switch_to.frame(iframe)
# a=web.find_element_by_xpath(‘/html/head/meta[3]‘).get_attribute(‘content‘)
# # 切回iframe
# # web.switch_to.default_content()
#
# #print(a)
################## 无头浏览器 ###############################
# 准备参数
# chrome_options = Options()
# #后面的两个是固定写法 必须这么写
# chrome_options.add_argument(‘--headless‘)
# chrome_options.add_argument(‘--disable-gpu‘)
# # web = Chrome(chrome_options=chrome_options) # 把参数配置设置到浏览器
# web = Chrome(options=chrome_options) # 新版把参数配置设置到浏览器
# web.get(‘https://www.endata.com.cn/BoxOffice/BO/Year/index.html‘)
# # 定位到下拉列表
# sel = web.find_element_by_xpath(‘//*[@id="OptionDate"]‘)
# #对元素进行包装,包装成下拉菜单
# sel = Select(sel)
# 调整选效
# for i in range(len(sel.options)): # i就是每个下拉框的索引
# sel.select_by_index(i) # 按照索引取值
# sleep(2)
# title=web.find_element_by_xpath(‘//*[@id="TableList"]/table/tbody/tr[1]/td[2]/a/p‘).text
# print(title)
#
# web.close()
# 拿到页面源代码(经过数据加载以及js执行之后的结果的html内容)
# t=web.page_source
# # print(t)
# # web.close()
from chaojiying import Chaojiying_Client
# web=Chrome()
# web.get(‘https://www.chaojiying.com/user/login/‘)
# sleep(1)
# img = web.find_element_by_xpath(‘/html/body/div[3]/div/div[3]/div[1]/form/div/img‘).screenshot_as_png
# WebDriverWait(web, 20)
# chaojiying = Chaojiying_Client(‘18223455555678‘, ‘m123330‘, ‘915459‘) # 用户中心>>软件ID 生成一个替换 96001
# p = chaojiying.PostPic(img, 1004)
# code = p[‘pic_str‘]
# web.find_element_by_xpath(‘/html/body/div[3]/div/div[3]/div[1]/form/p[1]/input‘).send_keys(‘2345678‘)
# sleep(1)
# web.find_element_by_xpath(‘/html/body/div[3]/div/div[3]/div[1]/form/p[2]/input‘).send_keys(‘234567890‘)
# sleep(1)
# web.find_element_by_xpath(‘/html/body/div[3]/div/div[3]/div[1]/form/p[3]/input‘).send_keys(code)
# sleep(1)
# web.find_element_by_xpath(‘/html/body/div[3]/div/div[3]/div[1]/form/p[4]/input‘).click()
# sleep(5)
# web.close()
######################## 12306 ######################
#浏览器版本低于88的时候
# web .execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {
# "source": """
# Object.defineProperty(navigator, ‘webdriver‘, {
# get: () => undefined
# });
# """
# })
# 浏览器版本大于等于88使用如下去掉werbdriver
option = Options()
option.add_argument("--disable-blink-features=AutomationControlled")
web = Chrome(options=option)
# web = Chrome()
web.get(‘https://kyfw.12306.cn/otn/resources/login.html‘)
WebDriverWait(web,10)
web.find_element_by_xpath(‘/html/body/div[2]/div[2]/ul/li[2]/a‘).click()
WebDriverWait(web,10)
# web.find_element_by_xpath(‘//*[@id="J-userName"]‘).send_keys(‘1812345‘)
# web.find_element_by_xpath(‘//*[@id="J-password"]‘).send_keys(‘mj12345678‘)
WebDriverWait(web,10)
img = web.find_element_by_xpath(‘//*[@id="J-loginImg"]‘)
chaojiying = Chaojiying_Client(‘18212345‘, ‘mj12345‘, ‘915459‘) # 用户中心>>软件ID 生成一个替换 96001
p = chaojiying.PostPic(img.screenshot_as_png, 9102)
code = p[‘pic_str‘] #x1,y1 | x2,y2
code_list = code.split(‘|‘)
print(code)
# z1008bo
for i in code_list:
p_temp = i.split(",")
print(type(p_temp))
x = int(p_temp[0])
y = int(p_temp[1])
# 带着偏移量移动
sleep(1)
ActionChains(web).move_to_element_with_offset(img,x,y).click().perform()
web.find_element_by_xpath(‘//*[@id="J-userName"]‘).send_keys(‘123343585‘)
web.find_element_by_xpath(‘//*[@id="J-password"]‘).send_keys(‘324324109392‘)
web.find_element_by_xpath(‘//*[@id="J-login"]‘).click()
# window.navigator.webdriwer : 为True那么就是自动化控制,Flase人工操作
# 如果你的浏览器被识别了你是程序在跑怎么办
# 1,Chrom的版本号低于88 在你的浏览器启动的时候(此时没有加载任何网页内容),向网页嵌入js代码,去掉webdriver
# 2,大于等于88
# 拖拽
sleep(1)
btn = web.find_element_by_xpath(‘//*[@id="nc_1_n1z"]‘)
ActionChains(web).drag_and_drop_by_offset(btn,300,0).perform()
sleep(1)
原文:https://www.cnblogs.com/thaimj1314520/p/14672410.html