Python Selenium Webdriver:查找#document元素 [英] Python Selenium Webdriver: finding #document element

查看:1473
本文介绍了Python Selenium Webdriver:查找#document元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用Python的Selenium Webdriver通过此HTML代码获取元素. 但是,我无法访问此#document标记内的任何元素.

I have been using Python's Selenium Webdriver getting elements with this HTML code. However, I could not access any of the elements inside this #document tag.

我都用过 driver.find_element_by_xpath("html/body/div[@id='frame']/iframe/*"),我尝试了 elem = driver.find_element_by_tag("iframe"),后跟 elem.find_element_by_xpath寻找内部元素,但失败了.

I used both driver.find_element_by_xpath("html/body/div[@id='frame']/iframe/*"), and I tried elem = driver.find_element_by_tag("iframe"), following by elem.find_element_by_xpath to find inner elements but failed.

我还尝试使用表达式执行driver.switch_to_frame(driver.find_element_by_tag("iframe")),以查找内部元素,但是它也没有用.

I also tried to do driver.switch_to_frame(driver.find_element_by_tag("iframe")), following with xpath expressions to find inner elements, but it also did not work.

框架:

<div>
    <iframe>
        #document
            <html>
               <body>
                   <div>
                    ....   
                   </div>
               </body>
            </html>
    </iframe>
</div>

推荐答案

切换到iframe,然后使用常规查询方法是正确的使用方法.我在整个大型测试套件中都成功使用了它.

Switching to the iframe and then using the normal query methods is the correct approach to use. I use it successfully throughout a large test suite.

请注意,在iframe中完成内部工作后,请切换回默认内容.

Remember to switch back to the default content when you've finished working inside the iframe though.

现在,解决您的问题.您如何投放iframe的内容?您是否刚刚编写了html并将其保存到文件中?或者您正在查看示例站点.您可能会发现iframe实际上没有包含您期望的内容.试试这个.

Now, to solve your problem. How are you serving the contents of the iframe? Have you literally just written the html and saved it to a file or are you looking at an example site. You might find that the iframe doesn't actually contain the content you expect. Try this.

from selenium.webdriver import Firefox

b = Firefox()
b.get('localhost:8000') # or wherever you are serving this html from
iframe = b.find_element_by_css_selector('iframe')
b.switch_to_frame(iframe)
print b.page_source

这将是iframe中的html.内容是您所期望的吗?还是主要是空的.如果为空,那么我怀疑是因为您需要分别提供iframe的内容.

That will be the html inside the iframe. Is the contents what you expect? Or is it mainly empty. If it's empty then I suspect it's because you need to serve the contents of the iframe separately.

这篇关于Python Selenium Webdriver:查找#document元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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