WebDriverError:在javascript中xpath表达式为null时无法找到元素

时间:2016-12-29 15:34:51

标签: javascript selenium selenium-webdriver jasmine protractor

我正在测试远程服务器上的登录页面,而selenium会抛出上述错误。但是,当我在本地服务器上测试相同的代码时,它完美地工作。我无法理解为什么远程服务器会抛出错误,当两个站点上的selenium版本完全相同时,本地服务器不会。 最初,我用过:

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

点击任何按钮。然后我尝试创建一个只执行单击操作的方法,以便每个单击操作都可以由此方法本身执行。为此我用过:

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

上面的行在本地服务器上执行完美,但无法识别远程服务器上的xpath,因此抛出错误,WebDriverError:当xpath表达式为null时无法找到元素。

我使用控制台语句在此行执行之前打印xpath。它打印元素所在的xpath。

有什么建议吗?

2 个答案:

答案 0 :(得分:1)

您可能需要wait才能使元素出现:

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

答案 1 :(得分:1)

我的两分钱......我在下面报告我的观察。通过在null xpath的{​​{1}}中传递elementFinder并在browser.executeScript

中作为参数传递,我能够重现上述错误

使用a = null执行下面的代码会抛出我们正在寻找的错误

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

错误堆栈

  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

可能的根本原因可能是变量desiredXpath被传递为null而我们不能使用console.log(),因为它们与量角器控制流不同步,并且任何事情都可能发生在

之间