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

2022-01-15 00:00:00 selenium selenium-chromedriver java

我有一个奇怪的案例,硒铬驱动程序 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() 方法是如何工作的

相关文章