NoSuchElementError:没有这样的元素:无法定位元素:{“method”:“css选择器”,“selector”:“* [name =“username”]“}在selenium JavaScript中

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

我正在尝试使用 selenium 和 JavaScript 在页面上自动登录。我想在获得预期的当前 url 后控制台“成功”,但出现错误。这是我写的代码。

const {Builder, By, Key} = require("selenium-webdriver");

require("chromedriver");

async function test() {
  let driver = await new Builder().forBrowser("chrome").build();
  await driver.get("https://opensource-demo.orangehrmlive.com/web/index.php/auth/login");
  await driver.findElement(By.name("username")).sendKeys("Admin"); 
  await driver.findElement(By.name("password")).sendKeys("admin123"); 
  await driver.findElement(By.className("oxd-button oxd-button--medium oxd-button--main orangehrm-login-button")).click();
  const currentUrl = await driver.getCurrentUrl();
  if (currentUrl === "https://opensource-demo.orangehrmlive.com/web/index.php/dashboard/index") {
  console.log("SUCCESSFUL");
} else {
  console.log("ERROR");
}
}
test();

现在当我运行

node myfilename.js
时,我收到以下错误:

/home/akhil/Desktop/testJs/node_modules/selenium-webdriver/lib/error.js:521
    let err = new ctor(data.message)
              ^

NoSuchElementError: no such element: Unable to locate element: {"method":"css selector","selector":"*[name="username"]"}
  (Session info: chrome=122.0.6261.94)
    at Object.throwDecodedError (/home/akhil/Desktop/testJs/node_modules/selenium-webdriver/lib/error.js:521:15)
    at parseHttpResponse (/home/akhil/Desktop/testJs/node_modules/selenium-webdriver/lib/http.js:510:13)
    at Executor.execute (/home/akhil/Desktop/testJs/node_modules/selenium-webdriver/lib/http.js:443:28)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async Driver.execute (/home/akhil/Desktop/testJs/node_modules/selenium-webdriver/lib/webdriver.js:740:17)
    at async toWireValue (/home/akhil/Desktop/testJs/node_modules/selenium-webdriver/lib/webdriver.js:146:15)
    at async /home/akhil/Desktop/testJs/node_modules/selenium-webdriver/lib/webdriver.js:192:16
    at async forEachKey (/home/akhil/Desktop/testJs/node_modules/selenium-webdriver/lib/webdriver.js:186:9)
    at async convertKeys (/home/akhil/Desktop/testJs/node_modules/selenium-webdriver/lib/webdriver.js:191:3)
    at async Driver.execute (/home/akhil/Desktop/testJs/node_modules/selenium-webdriver/lib/webdriver.js:738:22) {
  remoteStacktrace: '#0 0x560c89955e93 <unknown>\n' +
    '#1 0x560c8964dce6 <unknown>\n' +
    '#2 0x560c89698e48 <unknown>\n' +
    '#3 0x560c89698f01 <unknown>\n' +
    '#4 0x560c896dc3f4 <unknown>\n' +
    '#5 0x560c896baedd <unknown>\n' +
    '#6 0x560c896d9899 <unknown>\n' +
    '#7 0x560c896bac53 <unknown>\n' +
    '#8 0x560c8968bdb3 <unknown>\n' +
    '#9 0x560c8968c77e <unknown>\n' +
    '#10 0x560c8991b7cb <unknown>\n' +
    '#11 0x560c8991f7e5 <unknown>\n' +
    '#12 0x560c899090e1 <unknown>\n' +
    '#13 0x560c89920372 <unknown>\n' +
    '#14 0x560c898ed1bf <unknown>\n' +
    '#15 0x560c89944488 <unknown>\n' +
    '#16 0x560c89944683 <unknown>\n' +
    '#17 0x560c89955044 <unknown>\n' +
    '#18 0x7f4922294ac3 <unknown>\n'
}
javascript selenium-webdriver
1个回答
0
投票

问题是该元素似乎不在 DOM 中,因此 WebDriver 无法找到它。为了解决这个问题,只需写:

await driver.wait(until.elementLocated(By.name("username")));

这告诉 WebDriver 等待,直到找到该元素。


此外,当单个元素上存在多个类名时,有些人可能无法通过类名查找元素。以下是您可以如何做到的。

await driver.findElement(By.css(".oxd-button.oxd-button--medium.oxd-button--main.orangehrm-login-button"))

© www.soinside.com 2019 - 2024. All rights reserved.