user2669905
user2669905

Reputation: 11

how to select by text using selenium and c#

I need to select the value (hours) related to an specific date. For example in the html below I need to read the number 24:20 based on the number 6; this is the html:

<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">

if I use:

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

i will get 24:20; but i need to get the value 24:20(in this case) based on number 6 (6 refers to day of the month) and not the Xpath (everyday will be a different date). If anyone can point me in the right direction, Thanks

Upvotes: 1

Views: 1559

Answers (3)

Sheng Jiang 蒋晟
Sheng Jiang 蒋晟

Reputation: 15271

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

Upvotes: 0

kurakura88
kurakura88

Reputation: 2305

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;
    }
}

Upvotes: 1

kurakura88
kurakura88

Reputation: 2305

A one liner solution:

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

Upvotes: 0

Related Questions