简体   繁体   English

Selenium C#无法使用ID,Name或XPath找到Element

[英]Selenium C# cannot find Element using Id, Name or XPath

I am trying to find an element on a page that is inside a panel(or table, I'm not sure). 我正在尝试在面板(或表格,我不确定)内​​的页面上找到一个元素。 When using the ID or Name it cannot be located. 使用ID或名称时,无法找到它。 I am now trying to find the element using the XPath but having trouble. 我现在正在尝试使用XPath查找元素,但是遇到了麻烦。 Firebug is no longer supported and I can't locate it using Firefox Development tools (copy XPath) either. Firebug不再受支持,我也无法使用Firefox开发工具(复制XPath)找到它。 I have tried adding wait exceptions and switch frames. 我尝试添加等待异常和切换帧。 I have tried the following code: 我尝试了以下代码:

web.FindElement(By.XPath("//td(@contains='FirstName'))")).SendKeys("Sarah");

This results in 这导致

"Message: OpenQA.Selenium.InvalidSelectorException : Given xpath expression "//td(@contains='FirstName'))" is invalid: SyntaxError: The expression is not a legal expression." “消息:OpenQA.Selenium.InvalidSelectorException:给定的xpath表达式“ // td(@ contains ='FirstName'))”无效:SyntaxError:该表达式不是合法表达式。

When I use Inspect Element on the webpage the output is this: 当我在网页上使用Inspect Element时,输出为:

<tr>
  <td>First Name:</td>
  <td>
   <input id="ContentPlaceHolder1_ASPxRoundPane14_txtFirstName" name="ctl00$ContentPlaceHolder1$ASPxRoundPanel4$txtFirstName" type="text">
   <br>
   <span id="ContentPlaceHolder1_ASPxRoundPanel4_RequiredFieldValidator2" style="color:Red;visibility:hidden;">First Name must be entered</span>
  </td>
</tr>

I've searched solutions for days but am still coming up empty. 我已经搜索了好几天的解决方案,但仍然空着。

As I said in the comment, your expression is wrong. 正如我在评论中所说,您的表达是错误的。 You can't @ a contains. 您不能@包含。 @ is for attributes in the element. @是元素中的属性。 So you want to just grab a specific part of the ID it seems "FirstName". 因此,您只想获取ID中似乎是“名字”的特定部分。 You will need to use "contains()" and inside the parenthesis is where you put the attribute you want to grab. 您将需要使用“ contains()”,并且在括号内是放置要获取的属性的位置。 Now the way you are going about it you are saying "equals" and you aren't specifying an attribute. 现在,您要进行的操作是说“等于”,而您没有指定属性。 So if you used an attribute, "equals" wouldn't of worked either as "equals" wants the "whole" attribute to equal only "FirstName", which is not the case in your "ID" attribute as it has more than just "FirstName". 因此,如果您使用属性,则“等于”将不起作用,因为“等于”希望“整个”属性仅等于“名字”,而“ ID”属性则不是这种情况,因为它不仅具有“名字”。 In order to search an attribute, you need to do the following. 为了搜索属性,您需要执行以下操作。

//input[contains(@id, 'FirstName')]

You can't set the "text" of a <td> element since it's a block level element. 您不能设置<td>元素的“文本”,因为它是块级元素。

You need to set the text of the input. 您需要设置输入文本。

You can find the input using this selector: 您可以使用以下选择器找到输入:

web.FindElement(By.CssSelector("td > input[id$='txtFirstName']")).SendKeys("The First Name")

This CSS Selector means (in English): 此CSS选择器的意思是(英文):

Find me a <td> , and immediately proceeding that element, find me an input, that has an id that ends with "txtFirstName" 为我找到一个<td> ,然后立即继续该元素,为我找到一个输入,该输入的id ends with “ txtFirstName” ends with

You can see that there's not much to go on, but txtFirstName in the id attribute looks unique enough. 您可以看到没有太多事情txtFirstName ,但是id属性中的txtFirstName看起来足够唯一。

If you want it simpler ( remember, KISS ;) ) 如果您想要更简单(请记住,KISS;))

just use 只是使用

web.FindElement(By.CssSelector("input[id$='txtFirstName']")).SendKeys("The First Name")

试试这个xpath:

`web.FindElement(By.XPath("//input[contains(@id,'FirstName')]")).SendKeys("Sarah");`

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

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