如何在硒中找到元素

时间:2016-02-02 13:59:49

标签: html css selenium

我想找到元素" Holiday4"在span class =" fc-title"。怎么做?我需要使用css选择器还是需要使用xpath?

<tr>

    <td class="fc-day-number fc-sun fc-future hp-cal-selected" data-date="2016-02-14">14</td>
    <td class="fc-day-number fc-mon fc-future" data-date="2016-02-15">15</td>
    <td class="fc-day-number fc-tue fc-future" data-date="2016-02-16">16</td>
    <td class="fc-day-number fc-wed fc-future" data-date="2016-02-17">17</td>
    <td class="fc-day-number fc-thu fc-future" data-date="2016-02-18">18</td>
    <td class="fc-day-number fc-fri fc-future" data-date="2016-02-19">19</td>
    <td class="fc-day-number fc-sat fc-future" data-date="2016-02-20">20</td>

</tr>

<tr>

    <td class="fc-event-container">

        <a class="fc-day-grid-event fc-h-event fc-event fc-start fc-end holiday" style="color:65280" title="Holiday">

            <div class="fc-content">
                <span class="fc-title">Holiday4</span>
            </div>

        </a>

4 个答案:

答案 0 :(得分:0)

使用cssSelector

.fc-content [div:contains('Holiday 4')]

同时检查一下 Need to find element in selenium by css

https://saucelabs.com/selenium/css-selectors

答案 1 :(得分:0)

如果要使用Python

from selenium import webdriver
driver = webdriver.Firefox()
driver.get('http://your_url.com')
element = driver.find_element_by_xpath('//span[@class="fc-title"][contain(text(),"Holiday 4")]')

答案 2 :(得分:0)

使用Xpath下方,

'$2y$10$KWTn41ESI0AOW1io5efht..x4KWfcpbb9ztotxUGrrM...'

答案 3 :(得分:0)

要获取“Holiday4”的文本,您可以通过以下方式找到它。

使用Css选择器:

string str = driver.FindElement(By.CssSelector("[class='fc-title']")).Text;

使用XPath:

string str = driver.FindElement(By.Xpath("tr/td/a/div/span")).Text;