2013-03-28 2 views
0

웹 사이트에서 웹 자동화를 수행하기 위해 Selenium을 처음 사용하고 두 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 태그에는 실제로 다른 텍스트도 포함됩니다.

내가 여기에서 추출한 내용은 다음과 같습니다. 운전 기사가 내 아내를 위해이 산을 구입했는데, 그녀의 피드백은 운전 중에도 사용하기가 정말 좋고 사용하기 쉽다는 것입니다.

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

이 실제로 내가 원하는 것이 아니다 모든 가장 안쪽 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… 

내 코드입니다. ./td/div/div[3]으로 특정 div 태그를 타겟팅 할 수 있지만 div 태그 사이에 텍스트를 가져올 수 없습니다.

의견이 있으십니까?

감사

+0

올바른 HTML 스 니펫/추출 할 항목이 있습니까? 스 니펫에는 "절대적으로"라는 단어는 포함되지 않습니다. – Taylor

+0

예. 죄송합니다. 붙여 넣은 항목을 모르겠습니다 ... 질문을 업데이트했습니다. – Kitizhi

답변

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

+0

약간의 조작으로 정규 표현식으로 원하는 결과를 얻을 수있었습니다. 고맙습니다! – Kitizhi

+0

당신이 @Kitizhi를 어떻게 만들 었는지 신경 써야하나요? –

0

/td/div/div [3]을 사용하여 요소를 찾은 후이 web 요소에서 getText()를 수행하면이 div/요소의 텍스트가 반환됩니다.

관련 문제