当前位置: 动力学知识库 > 问答 > 编程问答 >

Selenium, Unable to find element by containing text following child node

问题描述:

A little bit of background:

The HTML looks like this:

 <table>

<thead>

<tr>

<th>Head1</th>

<th>Head2</th>

</tr>

</thead>

<tbody>

<tr>

<td>

<span>

<span class="icon">S</span>

"Auto"

</span>

</td>

<td>

<span>

Cost, Time

</span>

</td>

</tr>

</tbody>

</table>

A simple version of the code looks like this: (run in pry)

[69] pry> e = driver.find_element(:xpath, "//table/tbody/tr/td/span")

=> #<Selenium::WebDriver::Element:0x7ba9a4d694458ec id=":wdc:1361791490676">

[70] pry> e.text

=> "SAutomotive"

[71] pry> e = driver.find_element(:xpath, "//table/tbody/tr/td/span[contains(text(),'Auto')]")

Selenium::WebDriver::Error::NoSuchElementError: The element could not be found

from /Users/ben/.rvm/gems/ruby-1.9.3-p194/gems/selenium-webdriver-2.29.0/lib/selenium/webdriver/remote/response.rb:52:in `assert_ok'

I have no access to changing the HTML code

Although there is only one row in this table there is the possibility of more being added and I cannot predict the location of the row, this is why i am trying to find it by name

my normal code is:

e = driver.find_element(:xpath, "//table/tbody/tr[td/span[contains(text(),'Auto')]]")

The problem I am having is that I cannot find any way of getting the row in the table by the name given in the text of the first table cell.

网友答案:

Use below xpath

"//table/tbody/tr/td/span[contains(.,'Auto')]"
分享给朋友:
您可能感兴趣的文章:
随机阅读: