javascript - python Selenium : Clicking on button with JavaScript onFocus validation

标签 javascript python selenium

我正在尝试使用 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

最佳答案

您是否尝试过点击 <td>通过 xpath?

关于javascript - python Selenium : Clicking on button with JavaScript onFocus validation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56565453/

相关文章:

javascript - 将 JSON 响应传递给 tinymce 模板配置初始值

javascript - 在 ng-repeat 中调用一个函数

python - 循环遍历 JSON 对象并将结果存储在 pandas dataframe 中

java - Selenium,Java-按类查找表(无 ID)

java - Selenium java 浏览器 session 重用

javascript - 如何在angularjs中减去日期

javascript - JS/JQuery 关于按键的正确使用

python - 如何从github将django应用程序部署到VPS(CentOS)?

python - 如何在没有多个循环的情况下将多个函数应用于 pandas 数据框?

html - XPath到标签附近的输入元素?