2016-10-28 4 views
1

특정 날짜와 관련된 값 (시간)을 선택해야합니다. 예를 들어 아래의 html에서 번호 6을 기준으로 24:20의 숫자를 읽어야합니다. 내가 사용하는 경우셀렌과 C를 사용하여 텍스트를 선택하는 방법 #

<div class="day-ofmonth"> 
<div class="day-ofmonth"> 
<span class="day-num">6</span> 
<span class="available-time">24:20</span> 
</div> 
<div class="day-ofmonth"> 
<span class="day-num">7</span> 
<span class="available-time">133:50</span> 
</div> 
<div class="day-ofmonth"> 
<div class="day-ofmonth"> 

: 이는 HTML입니다

IWebElement t_value = d.FindElement(By.XPath(".//*  [@id='calinfo']/div[9]/span[2]")); 
var t_again2 = t_value.GetAttribute("textContent"); 

내가 24:20 얻을 것이다; 하지만 난 (이 경우) 숫자 6 (6 월의 날짜를 말합니다)과 Xpath (매일 다른 날짜가 될 것입니다)에 따라 24시 20 분 값을해야합니다. 사람이 올바른 방향으로 날 지점 수 있다면, 감사

답변

1
string availableTime = null; 
// Find all elements with class = 'day-num' 
var dayNums = d.FindElements(By.XPath("//span[@class='day-num']")); 
foreach (IWebElement dayNum in dayNums) 
{ 
    // check if text is equal to 6 
    if (dayNum.Text == "6") 
    { 
     // get the following sibling with class = 'available-time', then get the text 
     availableTime = dayNum.FindElement(By.XPath("following-sibling::span[@class='available-time']")).Text; 
     break; 
    } 
} 
+0

는 요소지만 출력을 찾을 않습니다는 다음과 같습니다 OpenQA.Selenium.Remote.RemoteWebElement – user2669905

+0

' .Text' 속성은'WebElement'에서 텍스트 내용을 가져옵니다. ToString() 대신 miscalling을 사용하고 있습니까? 당신이 얻고있는 WebElement를 디버깅 할 것을 제안합니다. – kurakura88

+0

고맙다. 나는 xpath를 문자열로 나누고 [i] : xpath (stringXpath)를 사용하여 테이블을 파싱하여 돌아 다닐 수 있었다. stringXpath = one + i + two – user2669905

0
xpath=//span[text()='6')]/following-sibling::span[1] 
0

한 라이너 솔루션 :

string availableTime = d.FindElement(By.XPath("//span[@class='day-num' and text()='6']/following-sibling::span[@class='available-time']")).Text; 
+0

그런 요소가 없어졌습니다. 감사합니다. – user2669905

+0

기술적으로는 이전 코드와 같은 코드이지만, 모두 Selenium에서 완료되었습니다. 찾고있는 요소를 숨기는 부모/조상 요소가있을 수 있습니다. – kurakura88

+0

고맙다. 나는 xpath를 문자열로 나누고 [i] : xpath (stringXpath)를 사용하여 테이블을 파싱하여 해결할 수 있었다. stringXpath = one + i + 2 – user2669905

관련 문제