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

2024-10-03 06:18:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用Selenium和Python来填充一个站点上的表单,其中框架在html中是可见的,但是我仍然无法让Selenium检测到任何框架或表单元素。你知道吗

我认为这是因为表单中有Javascript,因为我看到表单字段的html中引用了相同的脚本。你知道吗

我对这个很陌生,所以我不太确定从哪里开始。据我所知,我可能需要使用Python的“execute\u 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时,它抛出“no-such-frame”异常:

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的解决方案,但对任何事情都持开放态度。你知道吗


Tags: notextnamesrchttphtmltypejs
1条回答
网友
1楼 · 发布于 2024-10-03 06:18:55

'@Todormanakov:多亏了Todor,我只能在其他任何事情之前添加以下行,然后才能填写和提交表格:

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

我很想知道为什么这是必要的,但至少现在起作用了。你知道吗

再次感谢您在评论中的回答。你知道吗

相关问题 更多 >