console.log("text") 当在 javascript 中没有使用 selenium 的可用按钮元素时
console.log("text") when there is no button element avalible using selenium in javascript
我想 console.log("No element")
当没有可用的按钮元素但它不起作用时。
const {Builder, By} = require("selenium-webdriver");
let driver = new Builder().forBrowser("chrome").build();
async function myfun(){
await driver.get("website")
await driver.findElement(By.linkText("yes")).click();
await driver.findElement(By.linkText("yes")).click();
await driver.sleep(500);
await driver.findElement(By.id("inputfield")).sendKeys("name");
setInterval(function(err){
if (err){
console.log("No element");
}else{
driver.findElement(By.tagName("button")).click();
}
}, 1000);
}
myfun()
我收到这些错误:
(node:16248) UnhandledPromiseRejectionWarning: NoSuchElementError: no such element: Unable to locate element: {"method":"css selector","selector":"button"}
(Session info: chrome=81.0.4044.92)
(node:16248) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:16248) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
我也尝试过 try/cath 方法,但没有成功。
try {
driver.findElement(By.tagName("button")).click();
} catch(err) {
console.log("No element")
}
归功于用户 Arthur Weborg. I found this post which solved my problem perfectly. It is copied from another thread. For a clearer understanding see the full thread here。
选择的答案没有按预期工作(err.state
是 undefined
并且总是抛出 NoSuchElementError
)——尽管使用可选回调的概念仍然有效。
由于我遇到了与 OP 引用相同的错误,我认为在确定目标元素是否存在时应该引用 NoSuchElementError
。顾名思义,当元素不存在时抛出的错误。所以errorCallback中的条件应该是:
err instanceof webdriver.error.NoSuchElementError
所以完整的代码块如下(对于那些利用该语法的人,我也使用 async
/await
):
var existed = await driver.findElement(webdriver.By.id('test')).then(function() {
return true;//it existed
}, function(err) {
if (err instanceof webdriver.error.NoSuchElementError) {
return false;//it was not found
} else {
webdriver.promise.rejected(err);
}
});
//handle value of existed appropriately here
我想 console.log("No element")
当没有可用的按钮元素但它不起作用时。
const {Builder, By} = require("selenium-webdriver");
let driver = new Builder().forBrowser("chrome").build();
async function myfun(){
await driver.get("website")
await driver.findElement(By.linkText("yes")).click();
await driver.findElement(By.linkText("yes")).click();
await driver.sleep(500);
await driver.findElement(By.id("inputfield")).sendKeys("name");
setInterval(function(err){
if (err){
console.log("No element");
}else{
driver.findElement(By.tagName("button")).click();
}
}, 1000);
}
myfun()
我收到这些错误:
(node:16248) UnhandledPromiseRejectionWarning: NoSuchElementError: no such element: Unable to locate element: {"method":"css selector","selector":"button"}
(Session info: chrome=81.0.4044.92)
(node:16248) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:16248) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
我也尝试过 try/cath 方法,但没有成功。
try {
driver.findElement(By.tagName("button")).click();
} catch(err) {
console.log("No element")
}
归功于用户 Arthur Weborg. I found this post which solved my problem perfectly. It is copied from another thread. For a clearer understanding see the full thread here。
选择的答案没有按预期工作(err.state
是 undefined
并且总是抛出 NoSuchElementError
)——尽管使用可选回调的概念仍然有效。
由于我遇到了与 OP 引用相同的错误,我认为在确定目标元素是否存在时应该引用 NoSuchElementError
。顾名思义,当元素不存在时抛出的错误。所以errorCallback中的条件应该是:
err instanceof webdriver.error.NoSuchElementError
所以完整的代码块如下(对于那些利用该语法的人,我也使用 async
/await
):
var existed = await driver.findElement(webdriver.By.id('test')).then(function() {
return true;//it existed
}, function(err) {
if (err instanceof webdriver.error.NoSuchElementError) {
return false;//it was not found
} else {
webdriver.promise.rejected(err);
}
});
//handle value of existed appropriately here