from selenium.webdriver import Chrome
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.chrome.options import Options
import time
# 解决自动化控制问题防止被检测到
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')
time.sleep(3)
web.find_element(By.XPATH,'//*[@id="J-userName"]').send_keys('12345678')
time.sleep(2)
web.find_element(By.XPATH,'//*[@id="J-password"]').send_keys('1223455555')
time.sleep(2)
web.find_element(By.XPATH,'//*[@id="J-login"]').click()
x=1
y=1
# 以某个为基点移动多少
# ActionChains(web).move_to_element_with_offset(,x,y).clock().perform()点击提交
#
# 拖拽验证码
time.sleep(5)
btn=web.find_element(By.XPATH,'/html/body/div[1]/div[4]/div[2]/div[2]/div/div/div[2]/div/div[1]/span')
# 拖拽
ActionChains(web).drag_and_drop_by_offset(btn,300,0).perform()
切换窗口:
# 切换窗口到达视频播放页面
web.switch_to.window(web.window_handles[-1])
通过标签名寻找:
web.find_element(By.TAG_NAME,'button').click()
无头操作:
# 无头操作
opt=Options()
opt.add_argument("--headless")
opt.add_argument('--disbale-gpu')
# 创建浏览器对象
web=Chrome(options=opt)
超级鹰处理验证码
# 找到验证码图片
img=web.find_element(By.XPATH,'/html/body/div[3]/div/div[3]/div[1]/form/div/img').screenshot_as_png
web.find_element(By.XPATH,'/html/body/div[3]/div/div[3]/div[1]/form/p[1]/input').send_keys('账号')
time.sleep(3)
web.find_element(By.XPATH,'/html/body/div[3]/div/div[3]/div[1]/form/p[2]/input').send_keys('密码')
chaojiying = Chaojiying_Client('超级鹰账号', '超级鹰密码', '1111固定的')
dic=chaojiying.PostPic(img, 1902)
version=dic['pic_str']
time.sleep(1)
# 输入验证码
web.find_element(By.XPATH,'/html/body/div[3]/div/div[3]/div[1]/form/p[3]/input').send_keys(version)
time.sleep(3)