I have a page that starts off without an iframe, and then via JavaScript gets an iframe added after clicking an anchor.
The problem I'm having is that when switching to the frame with driver.switch_to_frame(x)
I still can find any of my content.
I've tried looping through the frames found with driver.find_elements_by_tag_name('iframe')
and checking each of them for the class I expect to find, but no such luck.
driver.switch_to_active_element()
does not give me the correct iframe either.
I'm not sure if the iframe content is just not accessible due to the JS DOM changes not reflected in what Selenium is seeing from the driver. I've gone through the same process for other iframes with no problems, but this one just won't cooperate. Below is a simplified version of what I'm dealing with.
Before JS:
<html>
<body>
<a onclick="jsmagic">load iframe</a>
</body>
</html>
After JS:
<html>
<body>
<iframe><div class='modal'>Message</div></iframe>
<a onclick="jsmagic">load iframe</a>
</body>
</html>
Python WebDriver attempt:
driver.switch_to_frame(0)
driver.find_elements_by_class_name('modal')
driver.switch_to_default_content()
I've also tried variations on that like:
frame = driver.find_elements_by_tag_name('iframe')[0]
driver.switch_to_frame(frame)
driver.find_elements_by_class_name('modal')
I've event tried using driver.execute_script
to get to the object, but getting at the content is beyond me. The Firefox console mands that work doesn't run via Selenium.
I have a page that starts off without an iframe, and then via JavaScript gets an iframe added after clicking an anchor.
The problem I'm having is that when switching to the frame with driver.switch_to_frame(x)
I still can find any of my content.
I've tried looping through the frames found with driver.find_elements_by_tag_name('iframe')
and checking each of them for the class I expect to find, but no such luck.
driver.switch_to_active_element()
does not give me the correct iframe either.
I'm not sure if the iframe content is just not accessible due to the JS DOM changes not reflected in what Selenium is seeing from the driver. I've gone through the same process for other iframes with no problems, but this one just won't cooperate. Below is a simplified version of what I'm dealing with.
Before JS:
<html>
<body>
<a onclick="jsmagic">load iframe</a>
</body>
</html>
After JS:
<html>
<body>
<iframe><div class='modal'>Message</div></iframe>
<a onclick="jsmagic">load iframe</a>
</body>
</html>
Python WebDriver attempt:
driver.switch_to_frame(0)
driver.find_elements_by_class_name('modal')
driver.switch_to_default_content()
I've also tried variations on that like:
frame = driver.find_elements_by_tag_name('iframe')[0]
driver.switch_to_frame(frame)
driver.find_elements_by_class_name('modal')
I've event tried using driver.execute_script
to get to the object, but getting at the content is beyond me. The Firefox console mands that work doesn't run via Selenium.
-
How about using
driver.page_source
, writing it to a file, and seeing if it contains the content you expect. That won't solve the problem, but it might help you in the debugging process. If it does contain thediv
you want, and Selenium still won't cooperate, you could parse the page source using other tools. – ChrisP Commented Jul 26, 2013 at 1:13 - Does your source HTML looks almost exactly the same as you posted above or did you cut it for brevity? Also is the frame visible? Do you see the text "Message"? – nilesh Commented Jul 26, 2013 at 2:58
- @ChrisP I'm currently using the frame.parent.page_source to find the link text, and it works. Not that it should, since I'd expect the frame to contain the text, not the parent. – Belrog Commented Jul 26, 2013 at 21:12
- @nilesh I cut it for brevity. The message is shown. My actual problem is more involved, but I'm fairly sure I've reduced the problem to what I described. – Belrog Commented Jul 26, 2013 at 21:14
- @Belrog, can you validate the HTML? Is there a chance that an unclosed tag or something is tripping up Selenium? – ChrisP Commented Jul 26, 2013 at 23:58
1 Answer
Reset to default 3It seems that it is a timing issue, I ended up with something along these lines:
def modal_must_show(self, string):
for _ in range(12):
for frame in self.driver.find_elements_by_tag_name('iframe'):
try:
if not frame.is_displayed():
continue
if frame.parent.page_source.find(string):
return
except:
pass
assert False, 'Modal missing string'
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745401023a4626101.html
评论列表(0条)