• <tfoot id='uDDyG'></tfoot>
      • <bdo id='uDDyG'></bdo><ul id='uDDyG'></ul>

        <legend id='uDDyG'><style id='uDDyG'><dir id='uDDyG'><q id='uDDyG'></q></dir></style></legend>

      1. <i id='uDDyG'><tr id='uDDyG'><dt id='uDDyG'><q id='uDDyG'><span id='uDDyG'><b id='uDDyG'><form id='uDDyG'><ins id='uDDyG'></ins><ul id='uDDyG'></ul><sub id='uDDyG'></sub></form><legend id='uDDyG'></legend><bdo id='uDDyG'><pre id='uDDyG'><center id='uDDyG'></center></pre></bdo></b><th id='uDDyG'></th></span></q></dt></tr></i><div id='uDDyG'><tfoot id='uDDyG'></tfoot><dl id='uDDyG'><fieldset id='uDDyG'></fieldset></dl></div>
      2. <small id='uDDyG'></small><noframes id='uDDyG'>

      3. selenium chrome 驱动的getText() 方法有时会返回一个空

        时间:2023-09-28

        <legend id='xabeE'><style id='xabeE'><dir id='xabeE'><q id='xabeE'></q></dir></style></legend>

          <bdo id='xabeE'></bdo><ul id='xabeE'></ul>
            <i id='xabeE'><tr id='xabeE'><dt id='xabeE'><q id='xabeE'><span id='xabeE'><b id='xabeE'><form id='xabeE'><ins id='xabeE'></ins><ul id='xabeE'></ul><sub id='xabeE'></sub></form><legend id='xabeE'></legend><bdo id='xabeE'><pre id='xabeE'><center id='xabeE'></center></pre></bdo></b><th id='xabeE'></th></span></q></dt></tr></i><div id='xabeE'><tfoot id='xabeE'></tfoot><dl id='xabeE'><fieldset id='xabeE'></fieldset></dl></div>
              <tbody id='xabeE'></tbody>

              <tfoot id='xabeE'></tfoot>
              1. <small id='xabeE'></small><noframes id='xabeE'>

                  本文介绍了selenium chrome 驱动的getText() 方法有时会返回一个空字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我有一个奇怪的案例,硒铬驱动程序 getText() 方法 (java) 为某些元素返回一个空字符串,即使它为其他具有相同元素的元素返回一个非空字符串xpath.这是页面的一小部分.

                  I have a curious case where the selenium chrome driver getText() method (java) returns an empty string for some elements, even though it returns a non-empty string for other elements with the same xpath. Here is a bit of the page.

                  <div __gwt_cell="cell-gwt-uid-223" style="outline-style:none;">
                  <div>Text_1</div>
                  <div>Text_2</div>
                  <div>Text_3</div>
                  <div>Text_4</div>
                  <div>Text_5</div>
                  <div>Text_6</div>
                  </div>
                  

                  对于每个内部标签,我可以获得 getTagName()getLocation()isEnabled() 的有效返回值,和 isDisplayed().但是,getText() 会为某些 div 返回一个空字符串.

                  for each of the inner tags, I can get valid return values for getTagName(), getLocation(), isEnabled(), and isDisplayed(). However, getText() returns an empty string for some of the divs.

                  此外,我注意到如果我使用 mac chrome 驱动程序,它始终是 getText() 返回空字符串的Text_5".如果我使用 windows chrome 驱动程序,它始终是 getText() 返回空字符串的Text_2".如果我使用 firefox 驱动程序,getText() 从所有 div 中返回预期的文本.

                  Further, I notice that if I use the mac chrome driver, it is consistently the ‘Text_5’ for which getText() returns an empty string. If I use the windows chrome driver, it is , it is consistently the ‘Text_2’ for which getText() returns an empty string. If I use the firefox driver, getText() returns the expected text from all the divs.

                  有其他人遇到过这种困难吗?

                  Has anyone else had this difficulty?

                  在我的代码中,我使用了类似的东西……

                  In my code, I use something like this…

                  ArrayList<WebElement> list = (ArrayList<WebElement>) driver.findElements(By.xpath("my xPath here"));
                  for (WebElement e: list) System.out.println(e.getText());
                  

                  如下所示,这是我正在使用的实际 xPath.上面的页面片段处理最后两个 div.

                  As suggested below, here is the actual xPath I am using. The page snippet above deals with the last two divs.

                  //*[@class='gwt-DialogBox']//tr[contains(@class,'data-grid-table-row')]//td[contains(@class,'lms-assignment-selection-wizard-cell')]/div/div
                  

                  推荐答案

                  更新: textContent 属性是更好的选择,并且在大多数浏览器中都受支持.这篇博文详细解释了这些差异:innerText vs.文本内容

                  Update: The textContent attribute is a better option and supported across the majority of browsers. The differences are explained in detail at this blog post: innerText vs. textContent

                  作为替代方案,innerText 属性将返回存在于 DOM 中的元素的文本内容.

                  As an alternative, the innerText attribute will return the text content of an element which exists in the DOM.

                  element.getAttribute("innerText")
                  

                  isDisplayed() 方法有时会在元素没有真正隐藏而是在视口之外时绊倒;getText() 为此类元素返回一个空字符串.

                  The isDisplayed() method can sometimes trip over when the element is not really hidden but outside the viewport; getText() returns an empty string for such an element.

                  您也可以通过使用 javascript 滚动到它来将元素带入视口,如下所示:

                  You can also bring the element into the viewport by scrolling to it using javascript, as follows:

                  ((JavaScriptExecutor)driver).executeScript("arguments[0].scrollIntoView(true);", element);
                  

                  然后 getText() 应该返回正确的值.

                  and then getText() should return the correct value.

                  isDisplayed() 方法的详细信息可以在这个 SO 问题中找到:

                  Details on the isDisplayed() method can be found in this SO question:

                  Selenium WebDriver 的 isDisplayed() 方法是如何工作的

                  这篇关于selenium chrome 驱动的getText() 方法有时会返回一个空字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:端口无效.退出... org.openqa.selenium.os.OsProcess check 下一篇:通过 Selenium 在 Chrome 中启用弹出窗口

                  相关文章

                  最新文章

                  1. <tfoot id='C7dxZ'></tfoot>
                    • <bdo id='C7dxZ'></bdo><ul id='C7dxZ'></ul>
                  2. <small id='C7dxZ'></small><noframes id='C7dxZ'>

                  3. <i id='C7dxZ'><tr id='C7dxZ'><dt id='C7dxZ'><q id='C7dxZ'><span id='C7dxZ'><b id='C7dxZ'><form id='C7dxZ'><ins id='C7dxZ'></ins><ul id='C7dxZ'></ul><sub id='C7dxZ'></sub></form><legend id='C7dxZ'></legend><bdo id='C7dxZ'><pre id='C7dxZ'><center id='C7dxZ'></center></pre></bdo></b><th id='C7dxZ'></th></span></q></dt></tr></i><div id='C7dxZ'><tfoot id='C7dxZ'></tfoot><dl id='C7dxZ'><fieldset id='C7dxZ'></fieldset></dl></div>
                    <legend id='C7dxZ'><style id='C7dxZ'><dir id='C7dxZ'><q id='C7dxZ'></q></dir></style></legend>