尝试在 Chrome 浏览器中使用 JavaScript 中的 Selenium WebDriver 打开特定网站时遇到问题。下面的代码创建浏览器驱动程序,但当我使用 setChromeOptions(options) 时它不会打开该网站。如果我删除此行,该网站将正常打开。 Chrome 选项配置似乎有问题。此外,我使用了 for 循环,因为我需要驱动程序来打开所有 4 个浏览器配置文件。
代码:
const { Builder, Browser, By } = require("selenium-webdriver");
const chrome = require('selenium-webdriver/chrome');
const DEFAULT_DIR = "user-data-dir=C:\\Users\\allan\\AppData\\Local\\Google\\Chrome\\User Data";
const PROFILES = ["profile-directory=Profile 3", "profile-directory=Profile 4", "profile-directory=Profile 5", "profile-directory=Profile 7"];
//GOMES = Profile 3 / MODAS = Profile 4 / ROCCO = Profile 5 / STORE = Profile 7
async function run(){
//for (let i = 0; i < PROFILES.length; i++){
var options = new chrome.Options();
options.addArguments("user-data-dir=C:\\Users\\allan\\AppData\\Local\\Google\\Chrome\\User Data");
options.addArguments(PROFILES[1]);
let navegador = await new Builder()
.forBrowser(Browser.CHROME)
//.setChromeOptions(options)
.build();
await navegador.get("https://sellercentral.amazon.com.br/home");
//}
}
run();
有问题的行似乎是 setChromeOptions(options)。如果我注释掉这一行,浏览器将正确打开该网站。我传递的 Chrome 选项有问题吗?
如有任何帮助,我们将不胜感激!
循环当前已禁用,因为我正在尝试首先解决配置设置。
我不认识这种格式,
"profile-directory=Profile 3"
根据文档,它应该看起来像
options.addArguments("user-data-dir=/path/to/your/custom/profile");
要确认这是问题所在,您可以一次注释掉每一行
.addArguments()
并查看它是否加载。我假设它可以与注释掉的第二行一起使用。