1

I am starting with selenium, I have had a problem with selenium not finding the displayed element after I selected 2 previous items.

This is my code:

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();

            });
        });
    });
});

This is error log:

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.

What's wrong in that case?

Mickael B.
  • 4,755
  • 4
  • 24
  • 48
Laurie
  • 11
  • 2
  • Upload your given error please. – dpapadopoulos May 03 '20 at 15:58
  • Hi @dpapadopoulos , I have just edited and attached an error log. I tried following all of these suggestions: https://stackoverflow.com/questions/18225997/stale-element-reference-element-is-not-attached-to-the-page-document but all failed . – Laurie May 03 '20 at 16:45
  • This is a JS issue and Protractor. This has to be a promise handling issue. You have to interact with JS properly. Please read this: https://javascript.info/promise-error-handling – dpapadopoulos May 03 '20 at 16:58
  • Try to do this to check if it's working: var example = element(by.xpath('YOUR_XPATH')); example.click(); – dpapadopoulos May 03 '20 at 17:00
  • Hi Laurie, just wait for the element to be loaded and then try to click. Or just add browser.sleep before 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(); – dheeraj May 05 '20 at 13:52

1 Answers1

0

using ExpectedConditions might solve the issue as there is promise exception since element not loaded. Please try this snippet.

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

            });
        });
    });
});
dheeraj
  • 324
  • 1
  • 9