简体   繁体   English

C#中找不到Selenium XPath错误

[英]Selenium XPath Not Found Error in C#

I have an HTML page (opening using Firefox and/or Chrome), and I am trying to find the correct XPath of the (image) Element I am trying to click on the webpage. 我有一个HTML页面(使用Firefox和/或Chrome打开),并且我试图找到要在网页上单击的(图像)元素的正确XPath。 I'm coding in C# using Selenium Automation. 我正在使用Selenium Automation在C#中进行编码。 Here is how the element looks like when I right-click and select inspect: 右键单击并选择检查时,该元素的外观如下:

(The actual website is too long so I'm subbing it with {website} and {different-website}). (实际网站太长,因此我将其与{website}和{different-website}进行了比较)。

EDIT: I'm sorry I forgot, but the {website} is dynamic. 编辑:对不起,我忘记了,但是{website}是动态的。 And {different-website} is static. 并且{different-website}是静态的。

<a href="https://{website}">
<img border="0" alt="open now" src="https://{different-website}.gif">
</a>

I'm trying to click the img using my automation, but everything I try the automation fails and says "Unable to locate element". 我正在尝试使用自动化来单击img,但是我尝试进行自动化的所有操作均失败并显示“无法找到元素”。 I notice that the img im trying to click is kind of nested into the above {a} element. 我注意到尝试点击的img嵌套在上述{a}元素中。 Maybe that's why it is so difficult to select and click. 也许这就是为什么它很难选择和单击。 I've tried things such as: 我已经尝试过诸如:

driver.FindElement(By.XPath("//a[starts-with(@href='https://www.{website}')]")).Click();

driver.FindElement(By.XPath("//a[contains(@href='https://www.{website}')]")).Click();

driver.FindElement(By.XPath("//img[(@src,'img1935.gif')]")).Click(); 

driver.FindElement(By.XPath("//img[(@src,'img1935.gif')]/../a")).Click(); 

driver.FindElement(By.XPath("//a[not(@href)/img/@src | //a[img]/@href")).Click(); 

driver.FindElement(By.XPath("//a[@alt='open now']/@src")).Click(); 

driver.FindElement(By.XPath("//img[@alt='open now']/@src")).Click(); 

All of these returned "Unable to locate Element" and I'm stuck on what else I should do or try. 所有这些都返回“无法找到元素”,而我被困在其他应做或尝试的事情上。 Any suggestions would be greatly appreciated. 任何建议将不胜感激。 Thank you very much. 非常感谢你。

Try //a[@href="https://{website}"]/img[1] 试试//a[@href="https://{website}"]/img[1]

  • All a elements (where href = "http://{website}") 所有a元素(其中,HREF = “HTTP:// {网站}”)
  • Then select the first img child element of those elements 然后选择这些元素的第一个img子元素

You can test it here - that website will also give suggestions. 您可以在此处进行测试 -该网站也会提供建议。

I find this page really useful when trying to work out XPath 在尝试制定XPath时,我发现此页面非常有用

If you have dynamic urls the you should avoid using fixed path in the selector.You can use something like: 如果您使用动态网址,则应避免在选择器中使用固定路径。您可以使用类似以下内容的网址:

//a[contains(@href, 'part_of_href_value')]

If the link is translated for different websites you can use something like: 如果链接是针对其他网站翻译的,则可以使用以下方式:

//a[contains(@href, 'part_of_href_value1') or contains(@href, 'part_of_href_value2') or contains(@href, 'part_of_href_value3')]

You can use any part of the href. 您可以使用href的任何部分。 You can adapt the above selectors for images also, replace a with img and @href with @src 您可以调整为图像也上述选择,更换aimg@href@src

Example: I have 2 links with href's: 示例:我有2个带有href的链接:
href= https://www.example1.com/blabla/page
href= http://www.second-example.org/bla/page-s

To select both links, i use example word since is common: 要选择两个链接,我使用示例词,因为这是常见的:
//a[contains(@href, 'example')]

To select both links by different parts/words: 要通过不同的部分/单词选择两个链接:
//a[contains(@href, 'blabla/page') or contains(@href, 'bla/page')]

Many of your XPaths won't find what you are looking for. 您的许多XPath都找不到您想要的东西。 Have you tried something simple like 你尝试过简单的事情吗

driver.FindElement(By.XPath("//a/img[@alt='open now']")).Click();

If this doesn't work, you need to provide more info or a link to the page. 如果这不起作用,则需要提供更多信息或指向页面的链接。

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

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