简体   繁体   English

Selenium中的xPath-我在做什么错?

[英]xPath in Selenium - what am I doing wrong?

I have a page with the following snippet in the HTML: 我在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. 现在,我尝试使用Selenium中的XPath表达式指定<tr>标记。 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: 但是,如果我将XPath表达式更改为:

 //*[@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? 也许您的HTML中还有其他引起问题的内容? Do you have more than one <tr> (or any other element) with the same ID? 您是否有多个具有相同ID的<tr> (或任何其他元素)?

Because IDs are (meant to be) unique, you should be able to use your second XPath expression with confidence. 因为ID是(唯一的)唯一的,所以您应该能够放心地使用第二个XPath表达式。 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=以便Selenium知道您正在使用的定位器类型:

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: 另外,如果只想选择<tr>元素,则还可以使用以下之一:

  • c_scopePane4_tr_header c_scopePane4_tr_header
  • identifier=c_scopePane4_tr_header 标识符= c_scopePane4_tr_header
  • id=c_scopePane4_tr_header id = c_scopePane4_tr_header

Alternate CSS Style locator: 备用CSS样式定位器:

css=tr#c_scopePane4_tr_header

or DOM Style: 或DOM样式:

dom=document.getElementById("c_scopePane4_tr_header")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 selenium 的函数我做错了什么? - What am I doing wrong using functions with selenium? 我究竟做错了什么? 我正在尝试将 selenium 依赖项添加到 Intellij,但我不断收到错误 - What am I doing wrong? I am trying to add selenium dependencies to Intellij and I keep getting errors 无法使用 Python Selenium 选择项目我做错了什么? - Unable to select item using Python Selenium What am I doing wrong? 以下代码在做什么? 我将硒用于Python,由于某种原因,我收到以下错误 - What am I doing wrong with the following code? I am using selenium for Python and for some reason, I get the error below 硒日历:月和日期选择:我在做什么错 - Selenium Calendar: Months and Dates Selections: What i'm doing wrong Webdriver.io +摩卡-我在做什么错? - Webdriver.io + Mocha - What am I doing wrong?? 多次抓取:代码中的问题。 我究竟做错了什么? - Multiple scraping: problem in the code. What am I doing wrong? 在SkipException,testNG上遇到问题,我在做什么错? - Having trouble with SkipException, testNG, what am I doing wrong? isTextPresent在Selenium测试中一直失败。 不知道我做错了什么 - isTextPresent fails all the time in Selenium test. Not sure if I am doing anything wrong 我做错了什么? - What i'm doing wrong?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM