selenium模拟鼠标滚动

#滚动页面直到显示“语料”,保证能显示所有语料

    test = driver.find_element_by_xpath('//span[text() = "语料"]')

    print('滚动页面...')

    driver.execute_script("arguments[0].scrollIntoView();", test)   #使页面的test元素显示在窗口最上方

这个方法可以模拟鼠标滚动到需要显示的元素位置,使用场景:浏览器窗口小,页面过大内容显示不全,元素可以定位但不可见。

或者

    self._current_browser().execute_script("arguments[0].focus();", element)


    import org.openqa.selenium.interactions.Actions;

    Actions action = new Actions(driver);

    action.sendKeys(Keys.END).perform();//下拉到底部

    action.sendKeys(Keys.HOME).perform();//上拉到顶部

    action.sendKeys(Keys.PAGE_DOWN).perform();//向下滚动

    action.sendKeys(Keys.PAGE_UP).perform();//向上滚动

你可能感兴趣的:(selenium模拟鼠标滚动)