无法用iframe作为root元素创建xpath
我正在尝试为iframe中的元素创建XPATH定位器: 我希望这个iframe是XPath的根源 有什么想法为什么它不起作用?
编辑 - 我不是要以任何方式与此元素进行交互(我知道我需要在与其中的元素进行交互之前切换帧根部元素由于上述情况 这是代码:
<div id="framesWrapper">
<div id="frame1Wrapper">
<iframe src="/sample" id="frame1" width="500px" height="350px"></iframe>
#document
<DOCTYPE hmtl>
<html>
<head>...</head>
<body style="background-color:#a9a9a9" data-new-gr-c-s-check-loaded="14.1065.0" data-gr-ext-installed=""><h1 id="sampleHeading">This is a sample page</h1>
<h1 id="sampleHeading">This is a sample page</h1>
</body>
</html>
</div>
<div id="frame2Wrapper" class="mt-4">
<iframe src="/sample" id="frame2" width="100px" height="100px"></iframe>
</div>
</div>
I'm trying to create an XPath locator for an element that is inside an iFrame:
I want this iframe to be the root of XPath because I have two similar elements in 2 different iframes - but seem unable to do so
any ideas why it's not working?
edit- I'm not trying to interact with this element in any way(I know that I need to switch frames before interacting with elements inside it I'm just having an XPath locator problem) I want to create an XPath with iframe as the root element because of the situation mentioned above
here's the code:
<div id="framesWrapper">
<div id="frame1Wrapper">
<iframe src="/sample" id="frame1" width="500px" height="350px"></iframe>
#document
<DOCTYPE hmtl>
<html>
<head>...</head>
<body style="background-color:#a9a9a9" data-new-gr-c-s-check-loaded="14.1065.0" data-gr-ext-installed=""><h1 id="sampleHeading">This is a sample page</h1>
<h1 id="sampleHeading">This is a sample page</h1>
</body>
</html>
</div>
<div id="frame2Wrapper" class="mt-4">
<iframe src="/sample" id="frame2" width="100px" height="100px"></iframe>
</div>
</div>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
通常,您需要等待页面加载,然后才能尝试与页面上的元素进行交互(似乎是常识)。但是,您可能不知道的是,
WebDriver
对象)仅等待直到预定的超时列表。这往往会有点慢,因为如果页面加载快速,它不会断开等待。WebDriverWait
类)允许您设置一个条件,该条件告诉Web驱动程序如果满足条件,则可以突破等待(即页面上可见一个Web元素) 。如果您知道这些事实,请原谅我进入这种解释。我认为这是必需的。
与位于iframe中的Web元素进行交互的一般配方如下:
1)找到iframe首先,
我明确创建了所需的所有对象,以便您可以看到或更好地理解它们的功能。例如,您无需明确创建
持续时间
对象。我只是认为以这种方式展示它更有帮助。另外,由于您不需要iframe web元素,因此在wait.until(...)
之前没有变量声明。您所需要的只是创建一个等待条件,以便在尝试下面尝试第2步之前,可以使用iframe。2)找到Web元素
3)完成后返回主(父)框架,
这几乎是您需要的。显然,还有更多。
timeOutException
是达到超时限制的例外。您可能想或可能不想将这些代码行包裹在尝试/捕获中。通常,我要做的是在尝试/捕获中包围,以便我第一次尝试时可以执行替代计划。例如,在某些情况下,我在初次尝试后重试。其他时候,我试图找到另一个元素。例如,如果页面加载上没有错误,则可能会看到元素。因此,我的第一个尝试可能是找到该元素,而我的第二次尝试可能是找到一个预期的错误消息。关键是使用尝试/捕获来处理超时可能是有益的。我忘了提到的一件事是,timeoutexception
是runtimeException
的一种类型。因此,您不会被迫抓住(处理)。如果您只是让它失败或以更优雅的方式处理,则取决于您。显然,就您而言,您需要处理两个iFrame。您需要做的就是遵循这两者的食谱,明确地通过每个食谱传递正确的定位器,并在完成后切换到父框架。这很重要。否则,当您尝试与父框架中的其他组件进行交互时,您的Web驱动程序将无法做到它已经存在了)。
Typically, you need to wait for the page to load before attempting to interact with elements on the page (seems like common sense). However, what you may not know is that
WebDriver
object) only waits until a predetermined timeout elapses. This tend to be a bit slow because it won't break out of the wait if the page loads fast.WebDriverWait
class) allows you to set a condition that will tell the web driver to break out of the wait if the condition is met (i.e. a web element is visible on the page).If you knew these facts, forgive me for going into this explanation. I thought it was necessary for what it is to follow.
The general recipe for interacting with a web element located in an iframe is as follows:
1) Locate the iframe first
I explicitly created all the objects you need so that you can see or understand better their function. For example, you don't need to explicitly create the
Duration
objects. I just thought it was more helpful to show it this way. Also, since you don't need the iframe web element, there is no variable declaration preceding thewait.until(...)
. All you need is create a wait condition for the iframe to be available before you attempt step #2 below.2) Locate the Web Element
3) Return to the main (parent) frame when you are done
That should be pretty much what you need. Obviously, there is more to this.
TimeoutException
is the exception that is thrown if the timeout limit is reached. You may or may not want to wrap those lines of code in a try/catch. Typically what I do is I surround in a try/catch so that I can execute an alternate plan if my first attempt times out. For example, in some cases, I retry after an initial attempt. Other times, I attempt to locate another element. For instance, an element might be visible if no errors on the page loading occurs. So, my first attempt might be to locate that element and my second attempt might be to locate an expected error message. The point is that using a try/catch to handle timeouts might be beneficial. One thing I forgot to mention is thatTimeoutException
is a type ofRuntimeException
. As such, you are not forced to catch (handle) them. It is up to you if you will simply let it fail, or handle in a more graceful way.Obviously in your case, you have two iframes you need to deal with. All you need to do is follow this recipe for both, explicitly passing the correct locator for each AND switching to the parent frame when you are done. This is very important. Otherwise, when you attempt to interact with other components in the parent frame, your web driver won't be able to because it will still thinks it is attempting to locate those components in the last frame you told it to look in (and that won't exist anymore).