~玉米糊~
慢慢来,也会很快。 非宁静无以志学,学什么都一样,慢慢打基础,找规律、认真、坚持,其余的交给时间。
随笔 - 117,  文章 - 17,  评论 - 1,  阅读 - 82072

一些登录页面会有滑块验证,如下图:

 

首先尝试用selenium按住滑块,脚本如下:

el1 = driver.find_element_by_xpath('//*[@id="nc_1_n1z"]')  # 按住滑块
ActionChains(driver).click_and_hold(on_element=el1).perform()
ActionChains(driver).move_to_element_with_offset(to_element=el1, xoffset=400, yoffset=0).perform()  # 滑动滑块

xoffset表示向右移动,向左则为负,yoffset表示向下移动

 

但是发现还不行,滑块对自动化程序作了限制,那么只有使用js改window.navigator.webdriver为false了

script = 'Object.defineProperty(navigator,"webdriver",{get:() => false,});'
driver.execute_script(script)  # 运行Javascript

 

完整代码如下:

复制代码
复制代码
def onwork_login(**param_dict):
    driver = webdriver.Chrome()
    driver.get("要导航到的网址")  # 导航到onwork页面
    driver.find_element_by_xpath('//*[@id="details-button"]').click()  # 点击高级按钮
    driver.find_element_by_xpath('//*[@id="proceed-link"]').click()  # 点击继续前往
    driver.find_element_by_xpath('//*[@id="app"]/div/div[1]/div/div/a[1]').click()  # 点击右上方登录
    driver.find_element_by_xpath('//*[@id="pane-first"]/div/form/div[1]/div/div/input').send_keys(
        param_dict['username'])  # 输入用户名
    driver.find_element_by_xpath('//*[@id="pane-first"]/div/form/div[2]/div/div[1]/input').send_keys(
        param_dict['password'])  # 输入密码
    ''' js改window.navigator.webdriver属性为false'''
    script = 'Object.defineProperty(navigator,"webdriver",{get:() => false,});'
    driver.execute_script(script)  # 运行Javascript
    el1 = driver.find_element_by_xpath('//*[@id="nc_1_n1z"]')  # 按住滑块
    ActionChains(driver).click_and_hold(on_element=el1).perform()
    ActionChains(driver).move_to_element_with_offset(to_element=el1, xoffset=400, yoffset=0).perform()  # 滑动滑块
    time.sleep(1)
    driver.find_element_by_xpath('//*[@id="pane-first"]/div/button/span').click()  # 点击登录
复制代码
复制代码

 

参考:

https://www.cnblogs.com/carlvine/p/15134307.html

https://blog.csdn.net/qq_41338249/article/details/107622186

https://blog.csdn.net/weixin_39861627/article/details/111018765?utm_medium=distribute.pc_relevant.none-task-blog-2~default~baidujs_title~default-4.control&spm=1001.2101.3001.4242

posted on   yuminhu  阅读(241)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示