deno使selenium调用edge浏览器(Window)

1. 获取驱动 msedgedriver.exe

  • 查看edge浏览器版本,链接: edge://settings/help
  • 下载对应的版本驱动:https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/
  • msedgedriver.exe 放到系统环境的PATH中

2、代码部分

//导入 selenium 库
import { Builder, By } from 'npm:selenium-webdriver'
const driver = new Builder().forBrowser('MicrosoftEdge').build()

try {
  await driver.get("http://www.baidu.com/"); //想要打开的网址
  // ...
  driver.quit(); // 表示关闭浏览器
} catch (err) {}


页面下拉滚动到底功能

/** 页面滚动 */
const STEP = 1000
async function scrollDonw(edge: any) {
  const getHeight = 'return document.documentElement.scrollTop || window.pageYOffset || document.body.scrollTop;'
  let currH, pageH = 0
  do {
    await sleep(1000)
    currH = await driver.executeScript(getHeight)
    pageH = await driver.executeScript('return document.body.scrollHeight')
    console.log(`${pageH} - ${currH} = ${pageH - currH}`)
    const jsexec = `window.scrollTo(${currH}, ${currH + STEP})`
    driver.executeScript(jsexec)
  } while (pageH - currH > STEP)
}

你可能感兴趣的:(爬虫,selenium,爬虫,deno)