首页 > 解决方案 > 在 selenium 中进行仿真,节点不工作

问题描述

问题
此代码应该在 chrome 中打开移动仿真,但由于某种原因它不起作用我正在使用 Selenium 和 express。Chrome 可打开但未处于仿真模式且未出现错误

 const express = require('express')
var webdriver = require('selenium-webdriver');
const app = express()
const port = 5000
var capabilities = {
  browserName: 'chrome',
  chromeOptions: {
      mobileEmulation: {
          deviceName: 'Apple iPhone 6'
      }
  }
};
async function google() {
  var driver = await new webdriver
    .Builder()
    .withCapabilities(capabilities)
    .build();
  await driver.get('https://www.whatismybrowser.com/detect/what-is-my-user-agent')
  
}

app.get('/', (req, res) => {
  google()
})

app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`)
})

标签: javascriptnode.jsseleniumgoogle-chromeselenium-webdriver

解决方案


var capabilities = {
  browserName: 'chrome',
  goo:chromeOptions: {
      mobileEmulation: {
          deviceName: 'Apple iPhone 6'
      }
  }
};

你应该使用 goo:chromeOptions https://chromedriver.storage.googleapis.com/2.31/notes.txt

您还可以将 chrome 选项用作:

 let options = new chrome.Options().setMobileEmulation(
     {deviceName: 'Google Nexus 5'});

 let driver = chrome.Driver.createSession(options);

https://www.selenium.dev/selenium/docs/api/javascript/module/selenium-webdriver/chrome_exports_Options.html


推荐阅读