javascript - Python Selenium:单击带有 JavaScript onFocus 验证的按钮
问题描述
我正在尝试使用 Selenium Python 自动化网页导航。我想单击一个执行 JavaScript 代码的 HTML 按钮,该代码验证按钮是否专注于 onclick 操作。
在这种特定情况下,我使用 Selenium 选择有效对象没有问题,但是简单element.click()
似乎不起作用。
HTML 源代码:
<td width="1%">
<!--Begin /jsp/com/tibco/wfc/ButtonUI.jsp-->
<script language="javascript">
var isFocus = "false";
</script>
<button class="button_normal" name="OK" style="width:100px" onfocus="isFocus='true'" onblur="isFocus='false'" onmouseover="this.className='button_rollover'" onmouseout="this.className='button_normal'" onmousedown="this.className='button_pressed'" onmouseup="this.className='button_rollover'"
onclick="javascript:if(isFocus=='false'){ return false}; showProgressMeter();submitCommand(event,'com_tibco_wfc_Button_498466961', 'com_tibco_wfc_DefaultFrame_1501194824', false, false, 'null', 'o', '', false);;return false;">OK</button>
<!--End /jsp/com/tibco/wfc/ButtonUI.jsp-->
</td>
Python / Selenium 属性:
warning_ok_button = driver.find_element_by_xpath("//button[@name='OK']")
attrib = driver.execute_script('var items = {}; for (index = 0; index < arguments[0].attributes.length; ++index) { items[arguments[0].attributes[index].name] = arguments[0].attributes[index].value }; return items;',warning_ok_button)
pprint(attrib)
{'class': 'button_normal',
'name': 'OK',
'onblur': "isFocus='false'",
'onclick': "javascript:if(isFocus=='false'){ return false}; "
"showProgressMeter();submitCommand(event,'com_tibco_wfc_Button_498466961', "
"'com_tibco_wfc_DefaultFrame_1501194824', false, false, 'null', "
"'o', '', false);;return false;",
'onfocus': "isFocus='true'",
'onmousedown': "this.className='button_pressed'",
'onmouseout': "this.className='button_normal'",
'onmouseover': "this.className='button_rollover'",
'onmouseup': "this.className='button_rollover'",
'style': 'width:100px'}
warning_ok_button.click()
似乎只是将按钮的类别从更改button_normal
为button_rollover
解决方案
感谢@supputuri,我能够应对这一挑战。这里的技巧是触发onfocus
事件,然后是onclick
与我的按钮元素关联的事件。
driver.execute_script("arguments[0].onfocus()",warning_ok_button)
driver.execute_script("arguments[0].click()",warning_ok_button)
推荐阅读
- javascript - Javascript 未在片段调用中运行
- python - 什么是依赖关系,我为什么要关心它们?
- c# - 为什么 Google 的 Auth 无法返回令牌?
- c# - CsvHelper 对象引用未设置为对象实例(GetRecords 问题)
- python - 尝试在列表函数中附加输入数字没有返回
- python - 如何最初打印一个字符串,然后设置一个被选中的位,这样就不会在 python 中再次打印
- python - 使用 keras 预测二项式函数
- django-rest-framework - 如何制作不同的 Django Api Url 路径
- r-markdown - 手动创建表格并自动对其标题进行编号,单词输出
- php - Laravel - 在 Jobs 中使用图像干预。图像ttfbbox错误