<legend id='5fcR2'><style id='5fcR2'><dir id='5fcR2'><q id='5fcR2'></q></dir></style></legend>
  • <tfoot id='5fcR2'></tfoot>

      • <bdo id='5fcR2'></bdo><ul id='5fcR2'></ul>

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

        <small id='5fcR2'></small><noframes id='5fcR2'>

        selenium chrome 驱动的getText() 方法有时会返回一个空字符串

        时间:2023-06-27
        <legend id='1xNPr'><style id='1xNPr'><dir id='1xNPr'><q id='1xNPr'></q></dir></style></legend>

          • <bdo id='1xNPr'></bdo><ul id='1xNPr'></ul>

            <small id='1xNPr'></small><noframes id='1xNPr'>

              <tbody id='1xNPr'></tbody>

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

                  <tfoot id='1xNPr'></tfoot>
                2. 本文介绍了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() 方法有时会返回一个空字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文章

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

                      <tfoot id='oUe3g'></tfoot>

                        <bdo id='oUe3g'></bdo><ul id='oUe3g'></ul>