无法找到硒元素的webdriver按名称和XPath [英] Unable to find element in Selenium WebDriver By Name and XPath

查看:142
本文介绍了无法找到硒元素的webdriver按名称和XPath的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我与硒的webdriver工作,并写了一个小code发现和元素(即按钮),点击它。
下面是HTML code的按钮:

 <输入类型=提交名称=j_id0:j_id2:j_id3:j_id4:j_id7VALUE =新主健康计划的onclick =AddLink()级= BTN>

下面是C#code测试用例:

  IWebElement newMasterHealthProgramsLink = driver.FindElement(By.Name(j_id0:j_id2:j_id3:j_id4:j_id7));
newMasterHealthProgramsLink.Click();

我试着使用XPath,以及:
IWebElement newMasterHealthProgramsLink = driver.FindElement(By.XPath(//输入[@ ID ='j_id0:j_id2:j_id3:j_id4:j_id5']));
            newMasterHealthProgramsLink.Click();

我发现了一个解决方案,说你一定没实现等待这一点。页面不等待完全加载并试图找到的元素。所以我加了等待命令,但没有发生usefel。仍然得到了同样的错误:

  TestAutomation.Driver.Login:
OpenQA.Selenium.NoSuchElementException:元素无法找到


解决方案

由于您的元素是在iframe中,你需要'转换'到IFrameusing的API的webdriver

在默认情况下,硒会使用'顶'框架,因此,任何发现元素的查询将被引导到最顶部框架,并忽略任何一个孩子的IFrame。

要解决这个问题,切换到当前的IFrame的指导硒推到该帧的任何请求,而不是。

driver.SwitchTo()。帧()

请注意,你需要访问帧的方式,无论是它的ID,指数(顶部框架是0,下一帧下来就是1,等...)或名称。

另外一个需要注意的是,任何进一步的请求都将被定向到IFRAME,忽略任何人,包括顶一个......所以如果你需要访问顶部框架,你需要切换回:

driver.Switch()。DefaultContent()

I am working with Selenium WebDriver and wrote a small code to find and element (i.e a button) and click on it. Here is the HTML code for the button:

<input type="submit" name="j_id0:j_id2:j_id3:j_id4:j_id7" value="New Master Health Program" onclick="AddLink()" class="btn">

Here is the C# code for the Test Case:

IWebElement newMasterHealthProgramsLink = driver.FindElement(By.Name("j_id0:j_id2:j_id3:j_id4:j_id7"));
newMasterHealthProgramsLink.Click();

I tried using XPath as well: IWebElement newMasterHealthProgramsLink = driver.FindElement(By.XPath("//input[@id='j_id0:j_id2:j_id3:j_id4:j_id5']")); newMasterHealthProgramsLink.Click();

I found a solution saying that you must not have implemented Wait for this. Page does not wait to load completely and tries to find the element. So I added wait command but nothing usefel happened. Still getting the same error:

TestAutomation.Driver.Login:
OpenQA.Selenium.NoSuchElementException : The element could not be found

解决方案

Since your element is in an IFrame, you'll need to 'switch' to that IFrameusing the WebDriver API.

By default, Selenium will use the 'top' frame, and therefore any 'find element' queries will be directed to the most top frame, and ignore any child IFrames.

To solve this, 'switching' to the current IFrame directs Selenium to shove any requests to that frame instead.

driver.SwitchTo().Frame()

Note that you'll need a way of accessing the frame, either by it's ID, index (the top frame is 0, next frame down is 1, etc...) or name.

Another note is that any further requests will be directed to that IFrame, ignoring any others, which includes the top one...so if you need to access the top frame, you'll need to switch back:

driver.Switch().DefaultContent().

这篇关于无法找到硒元素的webdriver按名称和XPath的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆