Selenium:无法打印来自webtable的不同行的值

时间:2016-08-06 16:30:05

标签: selenium selenium-webdriver

我正在尝试从图片中显示的下表中为不同的行打印City列的值。

我使用了以下代码。我的代码打印"迪拜" 4次而不是打印不同的城市。有人可以帮我解决这个问题吗?

{{1}}

Table Image

2 个答案:

答案 0 :(得分:1)

实际上您的代码是正确的,只有xpath用于获取城市元素的问题。

当您要使用xpath //td[2]查找城市名称时,您实际上每次都在搜索第二列,但在整个页面上,这就是您每次都获得相同城市名称的原因。

您需要提供.//td[2] xpath,因为当您向xpath提供.时,它将仅搜索元素上下文,否则将在整个页面上搜索文档,并根据整体页面输出绝对正确。

现在,如果您只想打印所有城市,请尝试以下方法: -

List<WebElement> cities = driver.findElements(By.xpath("//*[@id='content']/table/tbody//td[2]")); 

for(WebElement city : cities) 
{
 System.out.println(city.getText());
}

答案 1 :(得分:0)

尝试使用以下代码打印City列的值:

private DatabaseReference mDatabase;
mDatabase = FirebaseDatabase.getInstance().getReference();
mDatabase.child('users').child('user_id1').setChildListener(ListenerName);

希望它有所帮助!

相关问题