• python调用chrome实现网页自动操作


    一. 内容简介

    python调用chrome实现网页自动操作。

    二. 软件环境

    2.1vsCode

    2.2Anaconda

    version: conda 22.9.0

    2.3代码

    链接:

    三.主要流程

    3.1 下载驱动和插件

    调用谷歌浏览器,需要下载浏览器驱动(https://registry.npmmirror.com/binary.html?path=chromedriver/),下载对应的版本,最新的里面没有,网上找一下就可以了,谷歌或者csdn,
    安装一下插件,不能用了在下载驱动就好了,测试好像不需要驱动

    然后安装 selenium

    ! pip install selenium
    ! pip install pyautogui
    
    • 1
    • 2

    安装成功
    在这里插入图片描述

    3.2 调用谷歌浏览器

    这个驱动没有路径,好像也可以运行,我只指定了网址,浏览的exe路径,就没了

    url = 'https://www.wjx.cn/vm/ev6IfcA.aspx'
    options = webdriver.ChromeOptions()
    options.add_experimental_option('excludeSwitches', ['enable-automation'])
    options.add_experimental_option('useAutomationExtension', False)
    # 谷歌浏览器exe位置
    options.binary_location = r"C:\Program Files\Google\Chrome\Application\chrome.exe"
     # 是否要启动页面
            # options.add_argument("--headless")  # 启用无头模式
    # GPU加速有时候会出bug
    options.add_argument("--disable-gpu")  # 禁用GPU加速
    options.add_argument("--disable-blink-features=AutomationControlled")
    driver = webdriver.Chrome(options=options)
    driver.execute_cdp_cmd('Page.addScriptToEvaluateOnNewDocument',
                                {'source': 'Object.defineProperty(navigator, "webdriver", {get: () => undefined})'})
     # 启动要填写的地址,这就启动浏览器
    driver.get(url)
    # 这是关闭浏览器
    # 等待页面加载,可以根据实际情况调整等待时间
    driver.implicitly_wait(10)
    
    # 获取完整页面结构
    full_page_content = driver.page_source
    
    # 关闭浏览器
    driver.quit()
    
    • 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

    启动成功
    在这里插入图片描述

    3.3 进行网页操作

    需要注意的就是,然后就是按自己的需求写就好了,有啥其他用到的,以后再补充,现在就用到这一个

    # 两个功能一致,使用这个代码找到对应的代码,click()即可,和js基本类似,只是部分语法不一样
    # python里面的写
    sinPro = driver.find_elements(By.CSS_SELECTOR, f'.jqradio')
    # js里面的
    document.querySelectorAll(".jqradio")
    
    • 1
    • 2
    • 3
    • 4
    • 5
    # 引入相关模块
    from selenium.webdriver.common.by import By
    from selenium import webdriver
    #随机数产生
    import random 
    #延时
    import time
    import pyautogui
    
      
    #单选题
    def single(driver):
    # 假设有10个单选题
        for j in range(1, 18):
            # 每个单选题所在的位置
            sinPro = driver.find_elements(By.CSS_SELECTOR, f'#div{j}')
            # 每个单选题的答案进行遍历
            for answer in sinPro:
                # 对应每个单选题的选项组合
                ansItem = answer.find_elements(By.CSS_SELECTOR, '.ui-radio') 
                if ansItem:
                    random.choice(ansItem).click()
                else:
                    ansItem = answer.find_elements(By.CSS_SELECTOR, '.ui-checkbox') 
                    selected_items = random.sample(ansItem, random.randint(2, 4))  # 选择两个不重复的元素
                    for item in selected_items:
                        item.click()
                # 答题时间间隔
                time.sleep(random.randint(0, 1)/2)
    
    #脚本执行方法
    def launch(nums):
        for i in range(0, nums):
            url_survey = 'https://www.wjx.cn/vm/ev6IfcA.aspx'
            options = webdriver.ChromeOptions()
            options.add_experimental_option('excludeSwitches', ['enable-automation'])
            options.add_experimental_option('useAutomationExtension', False)
            options.binary_location = r"C:\Program Files\Google\Chrome\Application\chrome.exe"
            # options.add_argument("--headless")  # 启用无头模式
            options.add_argument("--disable-gpu")  # 禁用GPU加速
            options.add_argument("--disable-blink-features=AutomationControlled")
            driver = webdriver.Chrome(options=options)
            driver.execute_cdp_cmd('Page.addScriptToEvaluateOnNewDocument',
                                {'source': 'Object.defineProperty(navigator, "webdriver", {get: () => undefined})'})
    
            # 启动要填写的地址
            driver.get(url_survey)
            # 填写选择题
            # single(driver)
            # # 提交按钮
            # end = driver.find_elements(By.CSS_SELECTOR, f'#ctlNext')
            # end[0].click()
            #提交按钮
            time.sleep(4)
            print('已经提交了{}次问卷'.format(int(i) + int(1)))
            driver.quit()#停止
    if __name__ == "__main__":
        #填写问卷次数
        launch(4000)
    
    
    • 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
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60

    结果如图
    在这里插入图片描述
    在这里插入图片描述

    四.参考

    【python】自动填写问卷星问卷及提交 http://t.csdnimg.cn/aifYa
    还有一个找不到了,看到了,可以联系我

  • 相关阅读:
    指导与管理项目执行
    单例模式定义及其基础示例
    Scss-混入和继承且如何正确使用
    C/C++ 实现动态资源文件释放
    C# 通过回调获取多线程中的结果
    正则表达式
    Vue2前端权限控制实战
    命令行中引导用户指定选择文档
    这可能是2022年把微服务讲的最全了:SpringBoot+Cloud+Docker
    java 校园失物 小程序的设计与实现
  • 原文地址:https://blog.csdn.net/qq_45179361/article/details/134328082