2013-12-14 2 views
11

href에서 값을 얻으려면 어떻게해야합니까? 이 예를 들어 같은href 값을 얻으십시오 (WebDriver)

는 :

<div id="cont"><div class="bclass1" id="idOne">Test</div> 

    <div id="testId"><a href="**NEED THIS VALUE AS STRING**"> 
    <img src="img1.png" class="clasOne" /> 
    </a> 

</div> 
</div> 
</div> 

나는 문자열로 그 값을해야합니다.

나는이 함께 시도했다 :

String e = driverCE.findElement(By.xpath("//div[@id='testId']")).getAttribute("href"); 
      JOptionPane.showMessageDialog(null, e); 

을하지만, 단지 NULL 값을 반환 ...

답변

33

당신 '은'

이 시도 대신에 'DIV'로 요소를 지적 코드 아래

driverCE.findElement(By.xpath("//div[@id='testId']/a")).getAttribute("href"); 
+0

지금 바로 작업하십시오! 감사. – user3062055

+5

@ user3062055이 대답을 수락 된 것으로 표시하는 것을 잊지 마십시오. – Stephan

0

두 개 이상의 앵커 태그가있는 경우 다음 코드 스 니펫이 도움이됩니다. href

//find all anchor tags in the page 
List<WebElement> refList = driver.findElements(By.tagName("a")); 

    //iterate over web elements and use the getAttribute method to 
    //find the hypertext reference's value. 

    for(WebElement we : refList) { 
     System.out.println(we.getAttribute("href")); 
    }