当在Selenium中加载项目时,NoSuchElement - Javascript语言。

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

我开始使用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.

这种情况下有什么问题?

javascript selenium protractor
1个回答
0
投票

使用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();
    });

            });
        });
    });
});
© www.soinside.com 2019 - 2024. All rights reserved.