2016-03-07 54 views
0

我試圖從HTML表格中提取文本。Selenium Java:Chrome Webdriver通過xpath findElement不返回期望值

我使用硒和鉻驅動程序。

sResult = m_chromeWebdriver.findElement(By.xpath("/html/body/table[3]/tbody/tr[2]/td[2]/center/table[3]/tbody/tr[" + i + "]/td[5]/a")).getText(); ///abbr 

這是表的extrat:

enter image description here

我已經嘗試了兩個表的代碼,它們的格式改爲:

<a class="bluelink" href="fstats.php?champ=904&amp;team=470&amp;team2=406&amp;tour=110"><abbr title="that this then">1:0 (0:0)</abbr> </a> 

enter image description here

and

<a class="bluelink" href="fstats.php?champ=804&amp;team=435&amp;team2=441&amp;tour=110">1:1</a> 

enter image description here

對於第一個我得到1:0(0:0),但後者 - 我得到一個空字符串。

我也可以告訴大家,我想對後者的格式follwoing代碼:

 List<WebElement> elemRes = elemTable.findElements(By.xpath("tr[" + i + "]/td")); 

     for(int x = 0; x < elemRes.size(); x++){ 

      System.out.println(elemRes.get(x).getText()); 
     } 

其中i是行索引。我可以將的所有單元格文本打印到除最後一個(X:Y單元格類型)之外的控制檯。循環確實到達了這個單元格,但是沒有打印任何東西!

enter image description here

原因中的元素結構的變化是由設計,我有上沒有影響。

你能提醒一下嗎?

謝謝。

+0

能否請您向我們展示硒你用來獲取第二個字符串的代碼?這可能是xpath選擇器中的一個問題。正如Shubham Jain在下面提到的,如果你正在尋找一個特定的項目,你最好創建一個基於href的xpath或css選擇器。 –

+0

@BreaksSoftware是的,我會在1分鐘內更新上面的主要問題。感謝您的時間。 – dushkin

+0

@BreaksSoftware isn; t「&nbsp p;」在這裏扮演角色? – dushkin

回答

0

你有沒有嘗試下面的XPath: -

//a[@class='bluelink'] 

嘗試在你的代碼如下: -

sResult = m_chromeWebdriver.findElement(By.xpath("//a[@class='bluelink']")).getText(); 

希望它會幫助你:)

+0

謝謝。不起作用,因爲該頁面包含了這個類的很多元素,而且我只需要特定的元素。 – dushkin

+0

這個URL是靜態的在href?fstats.php中?champ = 904 & team = 470 & team2 = 406 & tour = 110 –

+0

如果是,並且此URL也是唯一的,那麼我們也可以使用它來定位元素 –