页面上的Javascript阻止Selenium检测某些框架和元素 - javascript

我将Selenium与Python结合使用,在网站上的表单中可以看到html中可见的框架,但是我仍然无法让Selenium来检测任何框架或表单元素。

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

我对此很陌生,所以我不太确定从哪里开始。从我的阅读中,我可能需要使用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

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

任何有关脚本是否是罪魁祸首的建议,如果是,应如何使它们显示其余框架和元素,对此表示赞赏。我更喜欢Python中的解决方案,但可以接受任何方法。

参考方案

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

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

我很想知道为什么这是必要的,但至少现在可以了。

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

使用C#执行JavaScript页面 - javascript

我正在尝试使用C#抓取一个网站。在此过程中的某个时候,网站返回了我需要执行的JavaScript页面,以便它将生成一些参数,然后使用所生成的参数作为查询变量来发布请求。这是JavaScript文件https://jsfiddle.net/7aw5vr59/浏览器生成的结果文件如下所示:<imimxxxyyy id="ActiveX"…

如何在JavaScript中将多个h1元素替换为h1,h2,h3,h4,h5,h6? - javascript

@foreach (var item in Model) { <div class="panel-heading"> <h1> <a href="#">@item.Title</a> </h1> </div> } 结果; (每页发布数:6)<…

在提交时在表单操作中获取变量丢失 - javascript

            当表单由onchange事件提交时,它不会保留get变量view。任何想法为什么会发生这种情况?提交后,这是它进入的网址,index?month=February&year=2014<form action="index?view=list" class="pure-form pure-fo…

选择后显示输入元素 - javascript

我有一个表格,其中取决于用户的选择,输入元素是否可见。实际上,用户正在以另一种形式设置已定义的合作伙伴类型,并且如果选中该元素,则允许在该类型的合作伙伴上可见的元素类型1将显示以下元素:<input type="text" id="partner" name="partner" class=&…

JavaScript将PHP中的字符串和整数传递给函数 - javascript

我正在尝试将字符串和整数都传递到同一函数中,但是引号引起了问题。我发现错误出在echo $q->info部分,我必须在此代码上使用双引号。有人可以帮我写这个$q->info,但不能获得真正的价值吗?到目前为止,我的代码是<td><a href="javascript:add(<?php echo $q->i…