简体   繁体   中英

xPath in Selenium - what am I doing wrong?

I have a page with the following snippet in the HTML:

...
<tbody id="c_scopePane4_tbody">
<tr id="c_scopePane4_tr_header">
...
</tr>
</tbody>
...

Now I'm trying to specify the <tr> tag with an XPath expression from within Selenium. Here's what I try:

 //tr[@id='c_scopePane4_tr_header']

This tells me

[error] locator not found: //tr[@id='c_scopePane4_tr_header'], error = Error:
Element //tr[@id='c_scopePane4_tr_header'] not found 

But if I change the XPath expression to:

 //*[@id='c_scopePane4_tr_header']

...then it works. What gives?

It's working for me with the same snippet. Perhaps there's something else in your HTML that's causing problems? Do you have more than one <tr> (or any other element) with the same ID?

Because IDs are (meant to be) unique, you should be able to use your second XPath expression with confidence. Alternatively you can use the following, but be sure to precede your locator with xpath= so that Selenium knows the type of locator you are using:

xpath=id('c_scopePane4_tr_header')

Also, if you just want to select the <tr> element then you can also use one of the following:

  • c_scopePane4_tr_header
  • identifier=c_scopePane4_tr_header
  • id=c_scopePane4_tr_header

Alternate CSS Style locator:

css=tr#c_scopePane4_tr_header

or DOM Style:

dom=document.getElementById("c_scopePane4_tr_header")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM