如何使用selenium和c#选择文本

时间:2016-10-28 02:21:56

标签: c# testing selenium-webdriver

我需要选择与特定日期相关的值(小时)。例如,在下面的html中,我需要根据数字6读取24:20的数字; 这是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">

如果我使用:

IWebElement t_value = d.FindElement(By.XPath(".//*     [@id='calinfo']/div[9]/span[2]"));
var t_again2 = t_value.GetAttribute("textContent");
我将在24:20得到;但是我需要在24:20(在这种情况下)得到基于数字6的值(6指的是月中的某天)而不是Xpath(每天将是不同的日期)。如果有人能指出我正确的方向,谢谢

3 个答案:

答案 0 :(得分: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;
    }
}

答案 1 :(得分:0)

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

答案 2 :(得分:0)

单线解决方案:

loop = 0
while loop <= 60:
    loop += 1
    count = len(data_list)
    j = count - 1
    pairs = [(i, j, data_list[i], data_list[j]) for i in range(j)]
    chunk_size, extra = divmod(len(pairs), 4 * 8)
    if extra:
        chunk_size += 1
    pool = Pool(8)
    res = pool.imap(calc_func, pairs, chunksize=chunk_size)
    pool.close()
    pool.join()
    res.sort(key=lambda e: e[2], reverse=True) 
    data_list.append(res.pop(0))