zoukankan      html  css  js  c++  java
  • python使用selenium执行JS快速完成超长字符串的输入

    使用selenium的 .send_keys 方法能够满足大多数情况的输入操作,但是在输入内容很多的情况下,使用该方法会消耗很多时间。

    此时可以使用selenium执行js的  .innerHTMLf方法快速输入这些内容。

    但是使用js的方法有一定缺陷性,它对常见  input标签类型的输入框无效,只对大多数富文本框生效。

    非常简单的html界面

    <!DOCTYPE html>
    <html>
    
    <head>
        <meta charset="utf-8" />
        <title>test</title>
        
    </head>
    
    <body bgcolor="burlywood">
        <div>
            <textarea id="kw1" cols="30" rows="10"></textarea>
            <hr>
            <textarea id="kw2" cols="30" rows="10"></textarea>
            <hr>
            <input id="kw3" value="">
        </div>
        
    </body>
    
    </html>

    from selenium import webdriver
    
    driver = webdriver.Chrome()
    driver.get("file:///E:/test.html")
    search_str = "超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超" 
                 "长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长" 
                 "内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容" 
                 "超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长" 
                 "内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内" 
                 "容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容" 
                 "超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超" 
                 "长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长" 
                 "内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内" 
                 "容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容" 
                 "超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容超长内容"
    
    # 使用innerHTML方式输入
    js = f'document.getElementById("kw1").innerHTML="{search_str}";'
    driver.execute_script(js)
    
    # 使用send_keys方式输入
    driver.find_element_by_id('kw2').send_keys(search_str)
    
    # 在input框中使用innerHTML方式输入
    js = f'document.getElementById("kw3").innerHTML="{search_str}";'
    driver.execute_script(js)

  • 相关阅读:
    多文件程序编译时头文件的使用方法
    360浏览器等被金山毒霸网强制霸占问题解决
    surface重装系统后,屏幕亮度不能调节,显示适配器出现黄色三角、windows hello不能正常使用
    Failed to execute "C:learnC程序练习1.exe": Error 0: 操作成功完成。 请按任意键继续. . .问题解决
    递归总结及斐波那契数列的实现
    在SpingBoot中使用Redis对接口进行限流
    在SpringBoot App中使用GoogleReCaptcha3过滤非法的请求
    在springboot中使用Guava基于令牌桶实现限流
    springboot jwt redis实现token刷新
    使用spring-validation和@RequestParam(required = false)字符串默认值的校验问题
  • 原文地址:https://www.cnblogs.com/testlearn/p/12382747.html
Copyright © 2011-2022 走看看