硒-在检查HTML之前找不到可见元素? [英] Selenium - can't find visible element until HTML is inspected?

查看:54
本文介绍了硒-在检查HTML之前找不到可见元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当前正在使用Selenium进行网络爬虫应用程序,并且在成功使用了多个模块之后,以下情况使我陷于困境:我试图通过文本"Reports"找到位于内部的菜单"类的元素一个名为"frame_applic"的框架.

I am currently using Selenium for a webcrawling application, and after several successful modules, the following situation left me stuck: I'm trying to locate an element of the class "menu" with the text "Reports" that is located inside a frame named "frame_applic".

很简单,对吧?应该很简单:

Pretty straightforward, right? Should be as simple as:

browser = webdriver.Chrome()
#Do the necessary crawling to get to this specific menu...

browser.switch_to_frame('frame_applic')
for obj in browser.find_elements_by_class_name('menu'):
    if obj.text == 'Reports':
      bt_reports = obj    
      break

#The variable bt_reports should be the one requested.

这是有趣的地方:尽管当我尝试切换到所需的帧时没有出现错误,但是没有找到类名称为菜单"的元素.当然,我肯定拼错了框架或类名,对吗?因此,在决定检查HTML并且不做任何其他更改之后,这些元素就存在了,就好像仅查看网页源代码的行为就改变了结果的结果一样.这里可能会发生什么?

Here is where it gets interesting: although no error is raised when I attempt to switch to the desired frame, no elements are found with a class name "menu". Surely I must have misspelled the frame or the class name, right? So after deciding to inspect the HTML and doing no other change whatsoever, the elements are there, as if the mere act of looking at the source code of the webpage changed the outcome of the result. What could possibly be happening here?

不允许发布完整的HTML,但是以下结构包含了这个问题:

I am not allowed to post the full HTML, but the following structure encompasses the question:

<html>
<head>
    <TITLE>Page</TITLE>
</head>
<frameset ...>
    <frame name="frame_menu" src="https:..." >
    <frame name="frame_applic" src="menu.asp?...">
    #document
        <table width="100%" border="0" cellpadding="5" cellspacing="0">

            <tr>
                <td colspan="2" align="right">
                    <table width="95%" border="0" cellpadding="5" cellspacing="0">

                        <tr><td><a href="https:..." class="menu">Reports</a></td></tr>
                        <tr><td><a href="https:..." class="menu">Change Password</a></td></tr>
                        <tr><td><a href="https:..." class="menu">Change Secret Phrase</a></td></tr>
                    </table>
                </td>
            </tr>
        </table>
        </body>

    <frame name="frame_bottom" src="https:...">
</frameset> 
</html>

推荐答案

仔细检查后,我发现潜在的问题是所需帧中的源信息在Active Server Page(asp)中,如图所示根据:

After some closer inspection, I found out that the underlying problem is that the source information from the desired frame is in an Active Server Page (asp), as seen under:

<frame name="frame_applic" src="menu.asp?..."> 

它不能从我所在的页面上直接访问.在浏览器上检查元素的行为显然迫使这些元素共存于webdriver实例中,从而使我能够在框架内获取元素,而以前无法访问它们.

And it is not directly accessible from the page I'm on. The act of inspecting the elements on the browser apparently forces the elements to coexist in the webdriver instance, allowing me to fetch the elements inside the frame, whereas they wouldn't be previously accessible.

我能够解决此问题,方法是先找到请求的框架,然后将Webdriver指向它的源,方法是:

I was able to solve this issue by first finding the requested frame, and then pointing the webdriver to the source of it, with:

browser.get(browser.find_element_by_xpath("//frame[@name='frame_applic']")\
.get_attribute('src'))

然后像以前一样简单地获取元素,

And then simply getting the elements as I was doing previously, with:

menu_list = browser.find_elements_by_class_name('menu')

这篇关于硒-在检查HTML之前找不到可见元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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