首页 > 解决方案 > 页面上的 Javascript 阻止 Selenium 检测某些框架和元素

问题描述

我正在使用 Selenium 和 Python 来填写网站上的表单,其中框架在 html 中可见,但我仍然无法让 Selenium 检测任何框架或表单元素。

我认为这是因为在表单字段的 html 中引用了相同的脚本,因此其中包含 Javascript。

我对此很陌生,所以我不太确定从哪里开始。根据我的阅读,我可能需要使用 Python 的“execute_script”和/或 Selenium 的 javascriptexecutor。

这是从顶部到框架的 HTML 片段,其中包含我之后的字段(框架名称“main”是其中包含表单的那个):

<html><head>
    <meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate">
    <meta http-equiv="Pragma" content="no-cache">
    <meta http-equiv="Expires" content="0">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>eBridge Inc.</title>
    <script type="text/javascript">
        function startup() {
            parent.header.location.href = 'header.aspx?guid=' + window.name;
            parent.nav.location.href = 'nav.aspx?guid=' + window.name;
            parent.main.location.href = 'welcome.aspx?guid=' + window.name;
            parent.footer.location.href = 'footer.aspx?guid=' + window.name;
        }
    </script>
</head>
<frameset rows="75px,25px,*,30px" frameborder="0" border="0" framespacing="0" onload="startup();">
    <frame name="header" scrolling="no" noresize="noresize" frameborder="0" marginheight="0">
    <frame name="nav" scrolling="no" noresize="noresize" frameborder="0" marginheight="0">
    <frame name="main" noresize="noresize" frameborder="0">
        <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"><html><head>
        <html>
            <head>
                <title>Retrieve</title>
                <meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate">
                <meta http-equiv="Pragma" content="no-cache">
                <meta http-equiv="Expires" content="0">
                <link id="mainStylesheet" href="../StyleSheet.css?62" rel="stylesheet" type="text/css">
                <link id="dhtmlxStylesheet" href="../Scripts/combobox/style/dhtmlxcombo_touch.css?62" rel="stylesheet" type="text/css">
                <link id="datepickerStylesheet" href="../Scripts/datepicker/jquery.ui.datepicker.css?62" rel="stylesheet">
                <link id="jqueryuiStylesheet" href="../Scripts/jquery-ui-1.10.3.custom.css?62" rel="stylesheet">

                <script src="../Scripts/jquery-1.7.1.min.js" type="text/javascript"></script>
                <script src="../Scripts/jquery-ui-1.8.15.custom.min.js" type="text/javascript"></script>
                <script src="../Scripts/datepicker/jquery.ui.datepicker.min.js" type="text/javascript"></script>
                <script src="../Scripts/jquery.maskedinput-1.3.min.js" type="text/javascript"></script>
                <script src="../Scripts/json.js" type="text/javascript"></script>

                <script src="../Scripts/combobox/scripts/dhtmlxcommon.js?version=3" type="text/javascript"></script>
                <script src="../Scripts/combobox/scripts/dhtmlxcombo_touch.js?version=3" type="text/javascript"></script>
                <script type="text/javascript">
                    $(document).ready(function () {
                        try
                        {
                            parent.nav.document.location = '../nav.aspx?menu=&guid=' + parent.window.name;
                        }
                        catch(ex){}
                        //focus on first tb/ddl
                        $('#tblIndex').find('input[type=text]').filter(':visible:first').focus();

                        $("#start_date").datepicker({
                            changeMonth: true,
                            changeYear: true
                        });

                        $("#end_date").datepicker({
                            changeMonth: true,
                            changeYear: true
                        });

                        $("#start_date").mask("99/99/9999");
                        $("#end_date").mask("99/99/9999");

                        $("#start_calendar_icon").click(function (event) {
                            $("#start_date").focus();
                        });

                        $("#end_calendar_icon").click(function (event) {
                            $("#end_date").focus();
                        });
                        // for enter keystroke
                        $(document).keyup(function (e) {
                            CheckKeyCode(e);
                            e.preventDefault();
                        });

                        $('.dhx_combo_input').keyup(function (e) {
                            CheckKeyCode(e);
                            e.preventDefault();
                        });

当我在 Selenium 中使用以下 python 时,它会抛出“没有这样的框架”异常:

driver.switch_to.frame("header")

它也对其余帧执行此操作。

所以我尝试使用以下方法列出页面上的所有元素:

for ii in ids:
    print(ii.get_attribute('id'))

它只返回一些页面元素(都不是表单字段),即:

stylesheet
hf
imgLogo
welcome
h_log_out
retrieve
aView
help
aSupport
cabnm

我的目标是将文本输入到“主”框架内的表单字段中。我通常对使用 Python 与页面交互没有任何问题,但我不确定如何处理此页面上似乎阻止我检测和切换框架的脚本。

任何关于脚本是否是罪魁祸首的建议,如果是,如何让它们显示其余的框架和元素,我们将不胜感激。我更喜欢 Python 中的解决方案,但我对任何事情都持开放态度。

标签: javascriptpythonhtmlselenium

解决方案


'@TodorMinakov:感谢 Todor,我只有在添加以下行之后才能填写并提交表格:

driver.switch_to_default_content()
driver.switch_to.frame("main")

我很想知道为什么这是必要的,但至少它现在有效。

再次感谢 Todor 在评论中的回答。


推荐阅读