简体   繁体   中英

Python Selenium: Clicking on button with JavaScript onFocus validation

I am trying to automate the webpage navigation using Selenium Python. I want to click on a HTML button that executes JavaScript code that validates if a button is in focus upon the onclick action.

In this specific case I have no problem in selecting a valid object using Selenium, however simple element.click() doesn't seem to work.

HTML source:

 <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 attributes:

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() only seems to be changing the class of a button from button_normal to button_rollover

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

Thanks to @supputuri I was able to tackle this challenge. The trick over here was to trigger, onfocus event followed by onclick event associated with my button element.

driver.execute_script("arguments[0].onfocus()",warning_ok_button)
driver.execute_script("arguments[0].click()",warning_ok_button)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM