2017-09-10 36 views
3

我想选择具有类名是从下方如何从网络访问表中的特定行

代码给网站上的表“topicRow后”的特定行

<table class="content" width="100%"> 
<tbody> 
    <tr class="topicTitle"> 
    <tr class="topicSubTitle"> 
    <tr class="topicRow post"> 
    <tr class="topicRow_Alt post_alt"> 
    <tr class="topicRow post"> 
    <tr class="topicRow_Alt post_alt"> 
    <tr id="forum_ctl03_ctl00" class="header2"> 
    <tr class="post"> 
    <tr> 
</tbody> 
</table> 

硒码

WebElement Webtable=driver.findElement(By.xpath("//*[@class='content']")); 
List<WebElement> TotalRowCount=Webtable.findElements(By.xpath("//*[@class='content']/tbody/tr[contains(@class,'topicRow post')]")); 
System.out.println("No. of Rows in the WebTable: "+TotalRowCount.size()); 

问题

每次打印表中所有行的大小。请指导...

+0

在开发人员工具中测试'xpath',您会得到多少结果? – Guy

+0

@Guy在开发人员工具中,2行突出显示出现在表格中,我也希望选择相同的行。 –

回答

1

使用thread.sleep() 这里是代码

Thread.sleep(5000); 
List<WebElement> TotalRowCount = driver.findElements(By.xpath("//*[@class='content']/tbody/tr[contains(@class,'topicRow post')]")); 

System.out.println("No. of Rows in the WebTable: "+TotalRowCount.size()); 
1

你可以给第一场比赛的第二

Webtable.findElements(By.xpath("//*[@class='content']/tbody/tr[contains(@class,'topicRow post')][1]")); 
2

的指数,1和2在你的代码,你已经使用网表找到行,但你仍然重复用于Webtable.findElements()中的Webtable的xpath部分"//*[@class='content']"

如下更改代码:

List<WebElement> TotalRowCount=Webtable. 
    findElements(By.xpath(".//tr[contains(@class,'topicRow post')]")); 

或更改使用driver.findElements()与重复的XPath部分:

List<WebElement> TotalRowCount=driver. 
    findElements(By.xpath("//* 
     [@class='content']/tbody/tr[contains(@class,'topicRow post')]")); 

如果以上两种方式都不会达到预期效果,请尝试以下代码:

WebElement table = driver.findElement(By.css("table.content")); 
List<WebElement> foundRows = table.findElements(By.css("tr.topicRow.post")); 
System.out.println("Found rows count: "+ foundRows.size()); 
+0

我已经尝试使用驱动程序的第二个选项。当我在调试模式下运行时,它工作正常,但是当我正常运行它时,存在同样的问题。让我试试最后一个选项。 Thanx –

+0

得到了解决方案,使用thread.sleep(5000)。 –