在 NodeJS 中无法点击 Selenium 中的“我同意”按钮

问题描述 投票:0回答:1

我正在尝试使用 NodeJS 在 Selenium 中编写自动化测试,但遇到了一些问题。

首先,我的代码:

const {Builder, By, Key, until} = require("selenium-webdriver");
async function example() {
    let driver = await new Builder().forBrowser('chrome').build();
    await driver.get('http://www.google.com');
    await driver.findElement(By.name('q')).sendKeys('webdriver', Key.RETURN);
    await driver.wait(until.titleIs('webdriver - Google Search'), 1000);
  
}

example();

每次我使用“节点索引”启动应用程序时,都会打开一个新的 Chrome/Firefox 应用程序并询问我是否同意 cookie。我尝试进入设置或连接我的帐户以允许 cookie,但如果我再次启动我的应用程序,我的设置就会消失。

我认为解决方案是使用我的代码手动单击“我同意”按钮,但我不知道该怎么做。

我应该如何编写代码来单击同意按钮,或者我还应该尝试什么?

javascript node.js selenium google-chrome
1个回答
0
投票

要单击

I agree
,请使用以下
xpath
来识别元素

await driver.findElement(By.xpth('//div[text()="I agree"]')).click()

您的代码将是

const {Builder, By, Key, until} = require("selenium-webdriver");
async function example() {
    let driver = await new Builder().forBrowser('chrome').build();
    await driver.get('http://www.google.com');
    await driver.findElement(By.xpth('//div[text()="I agree"]')).click();
    await driver.findElement(By.name('q')).sendKeys('webdriver', Key.RETURN);
    await driver.wait(until.titleIs('webdriver - Google Search'), 1000);
  
}

example();
© www.soinside.com 2019 - 2024. All rights reserved.