硒Chrome驱动程序的getText()方法有时会返回一个空字符串 [英] getText() method of selenium chrome driver sometimes returns an empty string

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

问题描述

我有一个奇怪的情况,即硒chrome驱动程序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. textContent

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()方法如何工作

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆