开发者

retrieving text in selenium using xpath?

开发者 https://www.devze.com 2023-01-05 05:14 出处:网络
Hi i have this HTML code: 开发者_如何学Python<tr class=\"odd events\" style=\"\"> <tr>

Hi i have this HTML code:

开发者_如何学Python
<tr class="odd events" style="">
<tr>
  <a title="Expand to see Actions" class="toggleEvent" name="actions_for_host_1" href="#"></a>
  <td id="7" colspan="6">
    <div>
      <ul>
        <li>Low Alarm at 2010/06/25 07:09 ( <span title="2010/06/25 14:09 (UTC)" class="time_helper">Pacific</span> )</li>
      </ul>
      <div></div>
    </div>
  </td>
</tr>

How can I retrieve the <li> value using xpath and selenium ruby, so I get back "Low Alarm at 2010/06/25 07:09 Pacific"?

I don't know what the time and date will be but I know the <li> tag will always start with Low Alarm

thanks

sorry i couldn't phrase the question better.


You need to use the getText command to retrieve the text using a suitable locator. There are a few possible locators that you could use, some suggestions are listed below:

XPath:

  • locate the li based on the child span with the class time_helper

    //li[span[@class='time_helper']]
    
  • locate the li based on the text starting with 'Low Alarm'

    //li[starts-with(text(), 'Low Alarm')]
    
  • locate the li based on the text containing 'Low Alarm'

    //li[contains(text(), 'Low Alarm')]
    

CSS:

  • locate the li based on the text starting with 'Low Alarm'

    css=li:contains(^Low Alarm)
    
  • locate the li based on the text containing 'Low Alarm'

    css=li:contains(Low Alarm)
    


Is there a reason why you're specifically trying to use an xpath locator for this? A CSS locator may do the job just as well, e.g.

css=li:contains('Low Alarm')
0

精彩评论

暂无评论...
验证码 换一张
取 消