【发布时间】:2020-08-17 14:52:27
【问题描述】:
我从 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.
在这种情况下有什么问题?
【问题讨论】:
-
请上传您给定的错误。
-
嗨@dpapadopoulos,我刚刚编辑并附上了错误日志。我尝试遵循所有这些建议:stackoverflow.com/questions/18225997/… 但都失败了。
-
这是一个 JS 问题和量角器。这必须是一个承诺处理问题。您必须正确地与 JS 交互。请阅读:javascript.info/promise-error-handling
-
尝试这样做以检查它是否正常工作: var example = element(by.xpath('YOUR_XPATH')); example.click();
-
嗨 Laurie,请等待元素加载完毕,然后尝试单击。或者只是在 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();
标签: javascript selenium protractor