2

我使用Nightwatch.js創建了一個項目。該流程在我們的開發環境中執行檢查(工作正常),並最終將測試電子郵件發送到Gmail帳戶。該過程將轉到Gmail,登錄並單擊正確的電子郵件。無法找到Gmail中的元素

我試圖獲取發送到應用程序的URL(忘記密碼鏈接)並將瀏覽器發送到正確的URL。

的問題是,當我使用下面的代碼:

browser 
    .useXpath() 
    .getText("string(//*[text()[contains(text(),'RetrievePassword')]])",function(result) 
    { 
    console.log(result.log) 
    }) 

我得到這個錯誤:

ERROR: Unable to locate element "" using: xpath

但是,當我告訴Nightwatch點擊鏈接,就會沒有問題,這樣做。任何想法?

的URL看起來是這樣的:

https://test.website.com/Secure/RetrievePassword.aspx?code=123456789

回答

2

我覺得你的XPath的選擇是錯誤的。如果您改用//*[contains(text(),'RetrievePassword')],它應該可以工作。這是一個基本的例子:

HTML的index.html

<!DOCTYPE html> 
<html lang="en"> 
<head> 
    <meta charset="UTF-8"> 
    <title>Nightwatch</title> 
</head> 
<body> 
    <a href="https://test.website.com/Secure/RetrievePassword.aspx?code=123456789">https://test.website.com/Secure/RetrievePassword.aspx?code=123456789</a> 
</body> 
</html> 

的JavaScriptgmail.js

module.exports = { 
    'Gmail': function (browser) { 
    browser 
     .url('http://localhost:8000/index.html') // Change this if needed 
     .waitForElementPresent('body', 1000) 
     .useXpath() 
     .getText("//*[contains(text(),'RetrievePassword')]", function (res) { 
     console.log(res.value); 
     }) 
     .end(); 
    } 
}; 

命令

nightwatch -t tests/gmail.js