gpt4 book ai didi

javascript - 点击 Angular 色为='button'的div元素无效

转载 作者:行者123 更新时间:2023-12-01 00:26:20 25 4
gpt4 key购买 nike

点击role='button'的div元素不起作用。我需要点击图标,但我不能这样做。

html:

<div class="list">
<div class="item">
<div role="button" tabindex="-1">
<strong>ItemName2</strong>
</div>
<div class="d">
<div class="item-icon" role="button" tabindex="-1" style="display: none">
<i aria-label="icon: edit" class="edit"></i>
</div>
</div>
</div>
<div class="item"> ... </div>
<div class="item"> ... </div>
<div class="item"> ... </div>
</div>

js:

  try {
await driver.get("http://127.0.0.1");

let findButtons = await driver.findElements(By.tagName('strong'));

let buttons = findButtons.map(elem => elem.getText());
const allButtons = await Promise.all(buttons);
console.log(allButtons); // It is displayed all button values, such as ItemName1
let tButton;
for (let i = 0; i < findButtons.length; i++) {
if (allButtons[i] == 'ItemName2') {
tButton = await findButtons[i];
tButton.click(); // I try to click on this button, where value = ItemName2
console.log(allButtons[i]); // It is displayed button value 'ItemName2'
}}}

控制台错误:

(node:12254) UnhandledPromiseRejectionWarning: StaleElementReferenceError: stale element reference: element is not attached to the page document

最佳答案

您收到过时元素异常,因为您试图获取具有旧引用的元素。每次您单击循环中的元素时,元素引用都会更新,并且 allButtons[i] 不起作用。为了解决这个问题,您必须获取最新的按钮引用。尝试下面的方法。

js:

const { By, Key, until } = require("selenium-webdriver");
const webdriver = require("selenium-webdriver");
require("chromedriver");
async () => {
let driver = await new webdriver.Builder().forBrowser("chrome").build();
try {
await driver.get("http://10.203.201.77:8000/login");

let findButtons = await driver.findElements(By.tagName('strong'));

let buttons = findButtons.map(elem => elem.getText());
const allButtons = await Promise.all(buttons);
console.log(allButtons); // It is displayed all button values, such as ItemName1
let tButton;
for (let i = 0; i < findButtons.length; i++) {
buttons = findButtons.map(elem => elem.getText()); # getting the button so that the elements refererence will refresh
if (allButtons[i] == 'ItemName2') {
tButton = await findButtons[i];
tButton.click(); // I try to click on this button, where value = ItemName2
console.log(allButtons[i]); // It is displayed button value 'ItemName2'

}
}
console.log("DONE");
} catch (e) {
console.log(e);
} finally {
await driver.quit();
}
}
}

关于javascript - 点击 Angular 色为='button'的div元素无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58954627/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com