2013-03-28 200 views
0

我是新來的使用硒在網站上執行web自動化,我很難提取兩個div標籤之間的文本。硒,如何提取兩個div標籤之間的文本

這是我嘗試從中提取文本的HTML代碼的剪切位。

... 
<tr> 
    <td width="150"> 
    <a href="http://rads.stackoverflow.com/amzn/click/B0099RGRT8"> 
    <img height="90" border="0" width="90" alt="iOttie Easy Flex2 Windshield Dashboard Car Mount H&hellip by iOttie" src="http://ecx.images-amazon.com/images/I/51mf6Ry9J2L._SL500_SS90_.jpg"> 
    </a> 
    <div class="xxsmall" style="margin-top: 5px"> 
     <a href="http://rads.stackoverflow.com/amzn/click/B0099RGRT8">iOttie Easy Flex2 Windshield Dashboard Car Mount Holder Desk Stand for iPhone 5 4S 4 3GS Samsung Gal&amp;hellip</a> 
     by iOttie 
    </div> 
    </td> 
    <td style="padding-left: 10px;"> 
     <div> 
      <div> 
       <span style="margin-left:-5px; vertical-align: -1"> 

       </span> 
       <b> 
       <a href="http://www.amazon.com/gp/cdp/member-reviews/A2UQ07EFPSX78X/ref=cm_pdp_rev_title_1?ie=UTF8&sort_by=MostRecentReview#R12ATB4KTIWFV8">Bought for my wife, now I want one. Excellent Product.</a> 
       </b> 
       , 
       <span class="nowrap">November 30, 2012</span> 
      </div> 
      <div style="margin-top: 5px;"> 
       I bought this mount for my wife, the feedback from her was is that it was really nice and easy to use even while driving. 
       <br> 
       <br> 
       So I "borrowed" it for a couple days, and now I am going to get one for myself. I am using it with an iPhone, but it would work fine with phones of all sizes, which is nice. If my phone size ever changes the mount will accommodate different sizes phones. 
       <br> 
       <br> 
       The phone is very easy to insert and remove , even while driving. 
       <br> 
       The mount is easy to position but not loose enough that it doesn't hold the position you want. 
       <br> 
       <br> 
       I was very impressed with the windshield mount, it is not just a typical suction cup mount. (Which always at some point… 
       <a href="http://www.amazon.com/gp/cdp/member-reviews/A2UQ07EFPSX78X/ref=cm_pdp_rev_more?ie=UTF8&sort_by=MostRecentReview#R12ATB4KTIWFV8">Read more</a> 
      </div> 
     </div> 
    </td> 
</tr> 
... 

其他div標籤實際上包含其他文字也是如此。

我想從中提取的是: 我爲我的妻子購買了這座山,她的反饋是,即使在開車的時候它也非常好用,而且很容易使用。

  I bought this mount for my wife, the feedback from her was is that it was really nice and easy to use even while driving. 

      So I "borrowed" it for a couple days, and now I am going to get one for myself. I am using it with an iPhone, but it would work fine with phones of all sizes, which is nice. If my phone size ever changes the mount will accommodate different sizes phones. 

      The phone is very easy to insert and remove , even while driving. 

      The mount is easy to position but not loose enough that it doesn't hold the position you want. 

      I was very impressed with the windshield mount, it is not just a typical suction cup mount. (Which always at some point… 

這是我的代碼:

String review; 
try { 
    review = WebElement.bucketElement.findElement(By.xpath("./td/div")).getText(); 
} catch (NoSuchElementException nsee) { 
    review = "NA"; 
} 

這實際上提取所有所有最內側的div標籤的文字是不是我想要的。我可以使用./td/div/div[3]來定位特定的div標籤,但我無法獲取div標籤之間的文本。

有什麼想法?

感謝

+0

你有正確的html片段/你想要提取什麼?例如,片段中不包含「絕對」一詞。 – Taylor 2013-03-29 16:50:38

+0

對不起,我不知道我粘貼了什麼...我已經更新了這個問題。 – Kitizhi 2013-03-30 20:09:19

回答

1

你可以使用普通expresions作爲一種解決方法:

String review; 
try { 
    review = WebElement.bucketElement.findElement(By.xpath("./td/div")).getText(); 
    review.replaceAll("(<.+>)", ""); 
} catch (NoSuchElementException nsee) { 
    review = "NA"; 
} 

正則表達式中刪除所有的標籤和內部元素的文本。只剩下第一級文字。這意味着,如果您有:

some strange<div>other text</div> text 結果字符串將是:some strange text

如果您需要更復雜的正規表示here is useful link to test it

+0

感謝您的回覆Zygimtantas,但它似乎像你的解決方案不工作。它仍然抓取其他div標籤的內部文本。也許我需要稍微更新數據集,以便其他div標籤中的文本更加明顯。 – Kitizhi 2013-03-29 16:43:33

+0

通過一些調整,我設法使用正則表達式獲得期望的結果。謝謝! – Kitizhi 2013-04-02 05:15:12

+0

小心提到你做了哪些調整@Kitizhi? – 2016-05-23 14:28:31

0

發現使用元素後/ TD/DIV/DIV [3],如果你在這個webelement做的getText(),它會回報你在這個div /元素的文本。

相關問題