wolfsbane
wolfsbane

Reputation: 1827

WebDriverError: Cannot find elements when the xpath expression is null in javascript

I'm testing a login page on the remote server and selenium throws me the above error. But when I test the same code on a local server, it works perfectly. I'm unable to understand as to why the remote server would throw an error and the local server won't when the selenium version on both sites is exactly the same. Initially, I used:

browser.driver.findElement(By.xpath(desiredXpath)).click()

to click on any button. Then I tried to make a method which only performs the click operation so that every click operation could be performed by this method itself. For that I used:

 browser.executeScript('arguments[0].click()', driver.findElement(By.xpath(desiredXpath)))

The above line executes perfectly on a local server but fails to recognise the xpath on the remote server and hence throws an error, WebDriverError: Cannot find elements when the xpath expression is null.

I used a console statement to print the xpath just before this line executes. It prints the xpath through which the element is to be located.

Any suggestions?

Upvotes: 2

Views: 6842

Answers (2)

AdityaReddy
AdityaReddy

Reputation: 3645

My two cents on this ... I am reporting my observation below. I was able to reproduce the above stated error by passing null in xpath of theelementFinder which was passed as an argument in browser.executeScript

The below code when executed with a = null throws the error we are looking out for

it('check', function () {
    browser.get("http://www.protractortest.org/");
    //var a = "//a[contains(@class,'github')]";
    var a = null;
    browser.executeScript('arguments[0].click()', element(by.xpath(a)))
    });

Error Stack

  Stack:
    WebDriverError: Cannot find elements when the XPath expression is null.
        at WebDriverError (C:\Users\aditya\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\error.js:27:10)
        at Object.checkLegacyResponse (C:\Users\aditya\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\error.js:639:15)
        at parseHttpResponse (C:\Users\aditya\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\http\index.js:538:13)
        at C:\Users\aditya\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\http\index.js:472:11
        at ManagedPromise.invokeCallback_ (C:\Users\aditya\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\promise.js:1379:14)
        at TaskQueue.execute_ (C:\Users\aditya\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\promise.js:2913:14)
        at TaskQueue.executeNext_ (C:\Users\aditya\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\promise.js:2896:21)
        at C:\Users\aditya\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\promise.js:2820:25
        at C:\Users\aditya\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\promise.js:639:7
        at process._tickCallback (node.js:369:9)
    From: Task: WebDriver.findElements(By(xpath, null))
        at WebDriver.schedule (C:\Users\aditya\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\webdriver.js:377:17)
        at WebDriver.findElements (C:\Users\aditya\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\webdriver.js:926:22)
        at C:\Users\aditya\AppData\Roaming\npm\node_modules\protractor\built\element.js:161:44
        at ManagedPromise.invokeCallback_ (C:\Users\aditya\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\promise.js:1379:14)
        at TaskQueue.execute_ (C:\Users\aditya\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\promise.js:2913:14)
        at TaskQueue.executeNext_ (C:\Users\aditya\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\promise.js:2896:21)
        at C:\Users\aditya\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\promise.js:2775:27
        at C:\Users\aditya\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\promise.js:639:7
        at process._tickCallback (node.js:369:9)
    From: Task: WebDriver.executeScript()
        at WebDriver.schedule (C:\Users\aditya\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\webdriver.js:377:17)
        at WebDriver.executeScript (C:\Users\aditya\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\webdriver.js:526:16)
        at ProtractorBrowser.to.(anonymous function) [as executeScript] (C:\Users\aditya\AppData\Roaming\npm\node_modules\protractor\built\browser.js:59:29)
        at Object.<anonymous> (C:\Users\aditya\WebstormProjects\demo\demo2.js:10:17)
        at C:\Users\aditya\AppData\Roaming\npm\node_modules\protractor\node_modules\jasminewd2\index.js:94:23
        at new ManagedPromise (C:\Users\aditya\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\promise.js:1082:7)
        at controlFlowExecute (C:\Users\aditya\AppData\Roaming\npm\node_modules\protractor\node_modules\jasminewd2\index.js:80:18)
        at TaskQueue.execute_ (C:\Users\aditya\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\promise.js:2913:14)
        at TaskQueue.executeNext_ (C:\Users\aditya\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\promise.js:2896:21)
        at C:\Users\aditya\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\promise.js:2820:25
    From: Task: Run it(" through Gmail Account") in control flow
        at Object.<anonymous> (C:\Users\aditya\AppData\Roaming\npm\node_modules\protractor\node_modules\jasminewd2\index.js:79:14)
    From asynchronous test:
    Error
        at Suite.<anonymous> (C:\Users\aditya\WebstormProjects\demo\demo2.js:5:5)
        at Object.<anonymous> (C:\Users\aditya\WebstormProjects\demo\demo2.js:4:1)
        at Module._compile (module.js:409:26)
        at Object.Module._extensions..js (module.js:416:10)
        at Module.load (module.js:343:32)
        at Function.Module._load (module.js:300:12)

1 spec, 1 failure
Finished in 1.957 seconds

Possible Root Cause could be the variable desiredXpath is being passed null and we cannot go with console.log() as they are not in sync with protractor Control flow and anything could have happened in between

Upvotes: 1

alecxe
alecxe

Reputation: 474191

You might need to wait for the element to become present:

var elm = element(by.xpath(desiredXpath));
var EC = protractor.ExpectedConditions;

browser.wait(EC.presenceOf(elm), 5000);
elm.click();

// or browser.executeScript("arguments[0].click();", elm.getWebElement());

Upvotes: 1

Related Questions