javascript - 在 Selenium 中加载项目时的 NoSuchElement - Javascript 语言
问题描述
我从 selenium 开始,在我选择了之前的 2 个项目后,我遇到了 selenium 找不到显示元素的问题。
这是我的代码:
driver.get('http://live.demoguru99.com/index.php/mobile.html');
driver.manage().window().maximize();
it("Click ‘Add to Compare’ of SONY XPERIA: ", function () {
driver.executeScript('window.scrollTo(0, 500);');
driver.findElement(webdriver.By.xpath('//*[@id="top"]/body/div/div/div[2]/div/div[2]/div[1]/div[3]/ul/li[1]/div/div[3]/ul/li[2]/a')).click();
});
it("Click ‘Add to Compare’ of IPHONE: ", function () {
driver.getAllWindowHandles().then(function(handles){
driver.switchTo().window(handles[0]).then(function(){
let result = driver.getTitle();
result.then(function(){
driver.executeScript('window.scrollTo(0, 500);');
driver.findElement(webdriver.By.xpath('//*[@id="top"]/body/div/div/div[2]/div/div[2]/div[1]/div[3]/ul/li[2]/div/div[3]/ul/li[2]/a')).click();
// Fail this step
driver.findElement(webdriver.By.xpath('//*[@id="top"]/body/div/div/div[2]/div/div[2]/div[3]/div[1]/div[2]/div/button/span/span')).click();
});
});
});
});
这是错误日志:
DevTools listening on ws://127.0.0.1:59612/devtools/browser/e60cb75e-606d-4d0f-afc6-dd3d0c5b5965 [14840:7772:0503/233743.775:ERROR:browser_switcher_service.cc(238)] XXX Init() (node:4460) UnhandledPromiseRejectionWarning: NoSuchElementError: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="top"]/body/div/div/div[2]/div/div[2]/div[3]/div[1]/div[2]/div/button/span/span"} (Session info: chrome=81.0.4044.129)
at Object.throwDecodedError (E:\AutomationTest\node_modules\selenium-webdriver\lib\error.js:550:15)
at parseHttpResponse (E:\AutomationTest\node_modules\selenium-webdriver\lib\http.js:565:13)
at Executor.execute (E:\AutomationTest\node_modules\selenium-webdriver\lib\http.js:491:26)
at processTicksAndRejections (internal/process/task_queues.js:97:5)
at async thenableWebDriverProxy.execute (E:\AutomationTest\node_modules\selenium-webdriver\lib\webdriver.js:700:17)
at async toWireValue (E:\AutomationTest\node_modules\selenium-webdriver\lib\webdriver.js:139:15)
at async E:\AutomationTest\node_modules\selenium-webdriver\lib\webdriver.js:190:16
at async forEachKey (E:\AutomationTest\node_modules\selenium-webdriver\lib\webdriver.js:184:9)
at async convertKeys (E:\AutomationTest\node_modules\selenium-webdriver\lib\webdriver.js:189:3)
at async thenableWebDriverProxy.execute (E:\AutomationTest\node_modules\selenium-webdriver\lib\webdriver.js:698:22)
(node:4460) 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:4460) [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.
在这种情况下有什么问题?
解决方案
使用 ExpectedConditions 可能会解决问题,因为由于未加载元素而存在承诺异常。请试试这个片段。
var EC = protractor.ExpectedConditions;
driver.get('http://live.demoguru99.com/index.php/mobile.html');
driver.manage().window().maximize();
it("Click ‘Add to Compare’ of SONY XPERIA: ", function () {
driver.executeScript('window.scrollTo(0, 500);');
driver.findElement(webdriver.By.xpath('//*[@id="top"]/body/div/div/div[2]/div/div[2]/div[1]/div[3]/ul/li[1]/div/div[3]/ul/li[2]/a')).click();
});
it("Click ‘Add to Compare’ of IPHONE: ", function () {
driver.getAllWindowHandles().then(function(handles){
driver.switchTo().window(handles[0]).then(function(){
let result = driver.getTitle();
result.then(function(){
driver.executeScript('window.scrollTo(0, 500);');
driver.findElement(webdriver.By.xpath('//*[@id="top"]/body/div/div/div[2]/div/div[2]/div[1]/div[3]/ul/li[2]/div/div[3]/ul/li[2]/a')).click();
// Fail this step
browser.wait(EC.presenceOf('element(by.xpath("//*[@id="top"]/body/div/div/div[2]/div/div[2]/div[3]/div[1]/div[2]/div/button/span/span"))')).then(()=>{
driver.findElement(webdriver.By.xpath('//*[@id="top"]/body/div/div/div[2]/div/div[2]/div[3]/div[1]/div[2]/div/button/span/span')).click();
});
});
});
});
});
推荐阅读
- python - 值相等时的numpy argmax
- java - Java 通配符 - 创建 Arrays.asList 或 ImmutableList.of 的泛型
- amazon-web-services - 在 AWS 多容器 docker 环境中部署到特定容器
- python - 如何使用 numpy ndarray 使用给定数据创建 n 维数组
- javascript - javascript等待多个异步ajax响应不起作用
- mysql - 如何将语言环境数据库中的更新数据自动同步到云数据库中
- linux - 关于 OpenDaylight 的 VTN 功能
- angular - 关于 Angular 材料中的 Datepicker
- python - 在 Python 中生成通用乘法问题
- spring - Spring Boot 依赖于 Spring 3