多测师是一家拥有先进的教学理念,强大的师资团队,业内好评甚多的接口自动化测试培训机构!

17727591462

联系电话

您现在所在位置:接口自动化测试培训 > 新闻资讯

接口自动化测试-Selenium webdriver滚动加载页面的爬取

更新时间:2021-09-28 02:10:44 作者:admin 浏览:504

接口自动化测试-Selenium webdriver滚动加载页面的爬取

  场景:滚动滚动条页面的元素在加载,有时候会出现一个加载更多的按钮,点击后继续加载(googleplay应用市场app信息页面)

  具体代码实现:

接口自动化测试-Selenium webdriver滚动加载页面的爬取

  /**

  * 滚动页面到指定位置

  * @param context

  * @throws Exception

  */

  private void crawlApps(TaskContext context){

  WebDriver webDriver = context.getWebDriver();

  WebElement element;

  JavascriptExecutor jsExecutor=(JavascriptExecutor) webDriver;

  boolean flag = true;

  Actions actions = new Actions(webDriver);

  long checkHeight = (Long) jsExecutor.executeScript("return document.body.scrollHeight;");

  while (flag){

  //每次滚动固定像素

  jsExecutor.executeScript("window.scrollBy(0,document.body.scrollHeight)");

  long nextHeight = (Long)jsExecutor.executeScript("return document.body.scrollHeight;");

  if (nextHeight > checkHeight){

  checkHeight = (Long)jsExecutor.executeScript("return document.body.scrollHeight;");

  //查找是否含有加载更多的按钮

  if (expectBy(context,By.xpath(XPATH_SHOW_MORE),5)) {

  element = webDriver.findElement(By.xpath(XPATH_SHOW_MORE));

  actions.moveToElement(element).build().perform();

  click(context,By.xpath(XPATH_SHOW_MORE));

  }

  }

  else{

  flag = false;

  }

  //actions.sendKeys(Keys.PAGE_DOWN).perform();

  }

  }

  /**

  * 期望出现的元素

  * @param context

  * @param by

  * @param seconds

  * @return 元素是否出现

  */

  protected boolean expectBy(TaskContext context, By by, long seconds) {

  try {

  (new WebDriverWait(context.getWebDriver(), seconds)).until(ExpectedConditions.elementToBeClickable(by));

  return true;

  } catch (Exception e) {

  return false;

  }

  }

  以上是关于Selenium webdriver滚动加载页面的爬取介绍,由多测师亲自撰写。https://www.aichudan.com/

联系电话

17727591462

返回顶部