繁体   English   中英

无法使用 NodeJS 单击 Selenium 中的“我同意”按钮

[英]Can't click "I am agree" button in Selenium with NodeJS

我尝试使用 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 应用程序并询问我是否同意 cookies。我尝试将 go 设置为或连接我的帐户并随时允许 cookies 但如果我重新开始我的应用程序,我的设置不见了。

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

我应该如何编写代码才能单击该同意按钮或我应该尝试什么?

点击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();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM