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

标签 javascript python html selenium

我使用 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 在评论中的回答。

关于页面上的 Javascript 阻止 Selenium 检测某些框架和元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54338939/

相关文章:

javascript - 根据文件输入状态运行函数

javascript - 在序列中添加 3 个空格

Javascript:如何将每n个字符转换为数组中的项目?

python - PyTorch 中 RNN(nn.LSTM、nn.GRU 等)的输出 h_n 是如何构造的?

python - 将数据框中的列与单个列表中的许多项目进行比较,并挑选出公共(public)元素

html - 表格单元格的最大高度

css - 以前有效的链接现在无效

javascript - 单选按钮的样式标签,它是一个 div 标签

python - 在Windows Anaconda2上安装Tensorflow

html - 像素完美设计 : How To Create A Website That Looks Exactly Like The PSD Template