Javascript 如何使用selenium和Mocha获取xPath()选择的锚定标记的文本

Javascript 如何使用selenium和Mocha获取xPath()选择的锚定标记的文本,javascript,selenium,xpath,mocha.js,Javascript,Selenium,Xpath,Mocha.js,我已成功选择了一个 您使用的是复数findElements,它提供了元素列表。对列表使用e.text将不起作用,因为您只能将.text与webobject一起使用 使用单数版本获取页面上的第一个匹配项:driver.findElement() 如果确实需要仅获取列表中一个元素的文本,请使用e[0]。text获取列表中第一个元素的文本。您可以尝试以下方法: driver.findElement(By.xpath("//a[contains(@class, 'user-name m-r-

我已成功选择了一个



  • 您使用的是复数findElements,它提供了元素列表。对列表使用e.text将不起作用,因为您只能将.text与webobject一起使用

    使用单数版本获取页面上的第一个匹配项:driver.findElement()


    如果确实需要仅获取列表中一个元素的文本,请使用e[0]。text获取列表中第一个元素的文本。

    您可以尝试以下方法:

    driver.findElement(By.xpath("//a[contains(@class, 'user-name m-r-sm text-muted welcome-message')]")).getText().then(function(text){
                               console.log("Username : " + text);
                            });
    
    • 您只需通过findElement(非findElement)进行搜索
    • 并直接提取函数部分之前的文本
    更新:

    有时对象不是真正隐藏的,但也不在视口中,那么gettext()也会返回一个空字符串。
    要检查,请尝试以下操作:

    driver.findElement(By.xpath("//a[contains(@class, 'user-name m-r-sm text-muted welcome-message')]")).getAttribute("innerText").then(function(text){
                               console.log("Username : " + text);
                            });
    

    你能给我们提供带有你想要的锚定标记的html代码吗?我想它是
    e.getText()。然后(…
    ,因为“undefined”正在返回,看起来你实际上没有用xpath获取元素。你能根据html重新检查xpath,或者发布html吗?在这种情况下,
    console.log怎么会出现(元素数:“+e.length”;console.log(“找到用户名:”);
    正在显示?如果无法选择元素,则此LOC也不应打印任何内容。对吗?您可以发布您的HTML,至少是您感兴趣的锚定标记周围的部分,可能您搜索了错误的元素,我们只能知道您是否发布了您的HTML。干杯
    用户名:
    ,它完全是空白的呃,你可以试着做的事!那真的很有魅力。:)谢谢兄弟。我只是在想各种可能。但这一个真的很有魅力。
        C:\xampp\htdocs\testPhantomJS\node_modules\selenium-webdriver>env KEY=trade moch
    a -t 120000 testMocha/login-as-administrator-mocha.js
    You chose to enter as 'trade'
    
    
      TrackRevenue Test
    Verification Successful - The correct title is displayed on the web page.
    Login Successful
    No. of elements :1
    Found The USerName :
    Username : undefined
        √ should work (71593ms)
    
    
      1 passing (1m)
    
    driver.findElements(By.xpath("//a[contains(@class, 'user-name m-r-sm text-muted welcome-message')]")).then(function(e){
                            if(e.length > 0)
                            {
                                console.log("No. of elements :"+e.length);
                                console.log("Found The USerName : ");
                                console.log("Username : "+e.text);//this is the line with the issue. It prints undefined
                            }
                        });
    
    C:\xampp\htdocs\testPhantomJS\node_modules\selenium-webdriver>env KEY=trade moch
    a -t 120000 testMocha/login-as-administrator-mocha.js
    You chose to enter as 'trade'
    
    
      TrackRevenue Test
    Verification Successful - The correct title is displayed on the web page.
    Login Successful
    No. of elements :1
    Found The USerName :
    Username : undefined
        √ should work (87006ms)
    
    
      1 passing (1m)
    
    driver.findElements(By.xpath("//a[contains(@class, 'user-name m-r-sm text-muted welcome-message')]")).then(function(e){
                        if(e.length > 0)
                        {
                            console.log("No. of elements :"+e.length);
                            console.log("Found The USerName : ");
                            console.log("Username : "+e[0].getText());
                        }
                    });
    
    C:\xampp\htdocs\testPhantomJS\node_modules\selenium-webdriver>env KEY=trade moch
    a -t 120000 testMocha/login-as-administrator-mocha.js
    You chose to enter as 'trade'
    
    
      TrackRevenue Test
    Verification Successful - The correct title is displayed on the web page.
    Login Successful
    No. of elements :1
    Found The USerName :
    Username : Promise::456 {[[PromiseStatus]]: "pending"}
        √ should work (37212ms)
    
    
      1 passing (37s)
    
    <ul class="nav navbar-top-links navbar-right">
       <li>
          <a class="user-name m-r-sm text-muted welcome-message" href="/profile/">saswat@matrixnmedia.com</a>
       </li>
       <li>
          <a href="http://saswatr3.ouh.co/main/account/help.php">
          <i class="fa fa-life-ring"></i>
       </a>
       </li>
       <li>
          <a class="log-out" href="/logout">
          <i class="fa fa-sign-out"></i>
           Log out
          </a>
       </li>
    </ul>
    
    driver.findElement(By.xpath("//a[contains(@class, 'user-name m-r-sm text-muted welcome-message')]")).getText().then(function(text){
                               console.log("Username : " + text);
                            });
    
    driver.findElement(By.xpath("//a[contains(@class, 'user-name m-r-sm text-muted welcome-message')]")).getAttribute("innerText").then(function(text){
                               console.log("Username : " + text);
                            });