zoukankan      html  css  js  c++  java
  • selenium操作下拉滚动条的几种方法

    数据采集中,经常遇到动态加载的数据,我们经常使用selenium模拟浏览器操作,需要多次下拉刷新页面才能采集到所有的数据,就此总结了几种selenium操作下拉滚动条的几种方法
    我这里演示的是Java版本的,使用chromedriver,当然你可以换成python或其他语言,浏览器用firefox或者phantomjs(无头浏览器),大部分都是适用的,不同浏览器有略微的差异。

    初始化一个浏览器

    首先要允许浏览器运行js脚本

    DesiredCapabilities sCaps = new DesiredCapabilities();
    sCaps.setJavascriptEnabled(true);
    System.getProperties().setProperty("webdriver.chrome.driver", "D:/tool/chromedriver.exe");
    WebDriver webDriver  = new ChromeDriver(sCaps);
    

    1.直接操作页面

    #下拉到页面底部
    ((JavascriptExecutor) webDriver).executeScript("window.scrollTo(0,document.body.scrollHeight)");
    #上拉到页面顶端
    ((JavascriptExecutor) webDriver).executeScript("window.scrollTo(document.body.scrollHeight,0)");
    

    或:

    #下拉到页面1000位置
    ((JavascriptExecutor) webDriver).executeScript("window.scrollTo(0,1000)");
    #上拉到页面顶端 0,0位置
    ((JavascriptExecutor) webDriver).executeScript("window.scrollTo(0,0)");
    

    2.拖动到页面元素位置

    经过测试这种方式最靠谱,在Chrome和phantomjs都测试通过,而其他方式有些网站在Chrome中没问题,但到了phantomjs中就有问题了,页面根本不动。
    不过这几种方式对大部分网站应该还是都可以的。

    比如要把页面拖动到id为test的元素位置,当然findElement方法还支持xpath和css选择器等多种方式

    String script = "return arguments[0].scrollIntoView();";
    WebElement element = webDriver.findElement(By.id("#test"));
    ((JavascriptExecutor) webDriver).executeScript(script, element);
    

    3. 发送PAGE_DOWN、END等键盘事件

    • END:可以让页面直接下拉到底
    • HOME:上拉到顶端
    • PAGE_DOWN:小幅度下拉
    org.openqa.selenium.Keys 模块中常用的变量属性 含义
    • Keys.DOWN, Keys.UP, Keys.LEFT,Keys.RIGHT 键盘箭头键
    • Keys.ENTER, Keys.RETURN 回车和换行键
    • Keys.HOME, Keys.END,
    • Keys.PAGE_DOWN,Keys.PAGE_UP
    • Home 键、End 键、PageUp 键和Page Down 键
    • Keys.ESCAPE, Keys.BACK_SPACE,Keys.DELETE Esc、Backspace 和字母键
    • Keys.F1, Keys.F2, . . . , Keys.F12 键盘顶部的F1 到F12 键
    • Keys.TAB Tab 键
    • ...
    WebElement webElement = webDriver.findElement(By.cssSelector("body"));
                    webElement.click(); // 有的时候必须点击一下,下拉才能生效(有的网站是这样,原因未找到)
    #小幅度下拉
                    webElement.sendKeys(Keys.PAGE_DOWN);
    #或者直接下拉到底
                  webElement.sendKeys(Keys.END);
    

    查看源码可以看出来,selenium封装了键盘上的Key,意思是我们可以通过sendKeys发送键盘事件,比如搜索的时候点击ENTER事件

    public enum Keys implements CharSequence {
        NULL('ue000'),
        CANCEL('ue001'),
        HELP('ue002'),
        BACK_SPACE('ue003'),
        TAB('ue004'),
        CLEAR('ue005'),
        RETURN('ue006'),
        ENTER('ue007'),
        SHIFT('ue008'),
        LEFT_SHIFT(SHIFT),
        CONTROL('ue009'),
        LEFT_CONTROL(CONTROL),
        ALT('ue00a'),
        LEFT_ALT(ALT),
        PAUSE('ue00b'),
        ESCAPE('ue00c'),
        SPACE('ue00d'),
        PAGE_UP('ue00e'),
        PAGE_DOWN('ue00f'),
        END('ue010'),
        HOME('ue011'),
        LEFT('ue012'),
        ARROW_LEFT(LEFT),
        UP('ue013'),
        ARROW_UP(UP),
        RIGHT('ue014'),
        ARROW_RIGHT(RIGHT),
        DOWN('ue015'),
        ARROW_DOWN(DOWN),
        INSERT('ue016'),
        DELETE('ue017'),
        SEMICOLON('ue018'),
        EQUALS('ue019'),
        NUMPAD0('ue01a'),
        NUMPAD1('ue01b'),
        NUMPAD2('ue01c'),
        NUMPAD3('ue01d'),
        NUMPAD4('ue01e'),
        NUMPAD5('ue01f'),
        NUMPAD6('ue020'),
        NUMPAD7('ue021'),
        NUMPAD8('ue022'),
        NUMPAD9('ue023'),
        MULTIPLY('ue024'),
        ADD('ue025'),
        SEPARATOR('ue026'),
        SUBTRACT('ue027'),
        DECIMAL('ue028'),
        DIVIDE('ue029'),
        F1('ue031'),
        F2('ue032'),
        F3('ue033'),
        F4('ue034'),
        F5('ue035'),
        F6('ue036'),
        F7('ue037'),
        F8('ue038'),
        F9('ue039'),
        F10('ue03a'),
        F11('ue03b'),
        F12('ue03c'),
        META('ue03d'),
        COMMAND(META),
        ZENKAKU_HANKAKU('ue040');
    
        private final char keyCode;
    
    ...
    }
    

    一个完整的示例

    public class SeleniumTest {
    
        public static void main(String[] args) throws Exception {
    
            System.setProperty("webdriver.chrome.driver", "D:/tool/chromedriver.exe");
            WebDriver webDriver = new ChromeDriver();
    
            webDriver.get("https://m.weibo.cn/");
            Thread.sleep(1000);
    
            for (int i = 0; i < 10; i++) {
                System.out.println("sleep 1s");
                Thread.sleep(1000);
                ((JavascriptExecutor) webDriver).executeScript("window.scrollTo(0,"+(i * 500)+")");
            }
        }
    }


    作者:LI木水
    链接:https://www.jianshu.com/p/3c6840ccf17d
    来源:简书
    简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。
  • 相关阅读:
    oracle 导入数据时提示只有 DBA 才能导入由其他 DBA 导出的文件
    oracle 常用语句
    android udp 无法收到数据 (模拟器中)
    android DatagramSocket send 发送数据出错
    AtCoder ABC 128E Roadwork
    AtCoder ABC 128D equeue
    AtCoder ABC 127F Absolute Minima
    AtCoder ABC 127E Cell Distance
    CodeForces 1166E The LCMs Must be Large
    CodeForces 1166D Cute Sequences
  • 原文地址:https://www.cnblogs.com/wangcp-2014/p/10907725.html
Copyright © 2011-2022 走看看