```python
from selenium import webdriver
from selenium.webdriver import ChromeOptions
from selenium.webdriver import ActionChains
import time
#@strattime:2021/11/26 18:49
#@endtime:2021/11/26 21:30
def login():
option = ChromeOptions()
option.add_experimental_option('excludeSwitches', ['enable-automation'])
# 解决浏览器自动闪退的问题
option.add_experimental_option("detach", True)
bro = webdriver.Chrome(options=option)
bro.get('https://kyfw.12306.cn/otn/resources/login.html')
# 改动的地方,就加了这两行代码,成功了
# 注意: 这种修改该属性值的办法只在当前页面有效,当浏览器打开新标签或新窗口时需要重新执行改变navigator.webdriver值的JavaScript代码。
script = 'Object.defineProperty(navigator,"webdriver",{get:()=>undefined,});'
bro.execute_script(script)
# 获取输入框
account=bro.find_element(by='id',value='J-userName')
#账号
account.send_keys('*******')
password=bro.find_element(by='id',value='J-password')
loginbutton=bro.find_element(by='id',value='J-login')
#密码
password.send_keys('*****')
# 登陆
loginbutton.click()
# 睡一会才能得到slirame,加载需要时间
time.sleep(1)
sliframe=bro.find_element(by='id',value='nc_1_n1z')
# 实例化动作链对象
action=ActionChains(bro)
action.click_and_hold(sliframe).perform()
# 按钮大小为40*34 ,框的大小为340*34
for i in range(5):
action.move_by_offset(70,0).perform()
time.sleep(0.3)
action.release()
time.sleep(5)
if __name__ == '__main__':
login()
献给还在12306模拟登陆挣扎的同学们,我先往后走了。
附上一篇好文:https://pea328.blog.csdn.net/article/details/105602629
|