1

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

3 Answers 3

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;
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

it does find the element but output is :OpenQA.Selenium.Remote.RemoteWebElement
.Text property will get the text content from the WebElement though. Are you miscalling ToString() instead? I would suggest you to debug what the WebElement you are getting.
Thank you, i was able to get around by dividing the xpath as a string and parsing table using [i]:xpath(stringXpath). stringXpath= one + i +two
0

A one liner solution:

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

2 Comments

It is technically the same code as my previous one, but all done in Selenium. You might have parent/ ancestor Element that hide the Element you are looking.
Thank you, i was able to get around by dividing the xpath as a string and parsing table using [i]:xpath(stringXpath). stringXpath= one + i +two
0
xpath=//span[text()='6')]/following-sibling::span[1]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.