3

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

Moshe Slavin
  • 5,127
  • 5
  • 23
  • 38
Dodger_
  • 31
  • 3
  • What browser? I have used selenium with C# and sometimes you have to run JavaScript to do the click for you (especially in IE). Other times you have to use an action instead - example: https://stackoverflow.com/a/13938778/9538369 – James Wasson Jun 12 '19 at 15:51
  • In this case it's Firefox. I've tried the following and it still fails (button class changes from button_normal to button_rollover): ```python actions = ActionChains(driver) actions.move_to_element(warning_ok_button).click() actions.perform() ``` – Dodger_ Jun 12 '19 at 16:36
  • Have you tried executing a script to click the button. I know its not ideal but sometimes it's necessary. Also I have noticed that if you sleep between some actions the actions work correctly - that could also be your issue. – James Wasson Jun 12 '19 at 16:43
  • Check if `driver.execute_script("arguments[0].click()",warning_ok_button)` triggers the javascript. – supputuri Jun 12 '19 at 17:00
  • @supputuri: That is great suggestion, but I'm afraid it didn't work this time around. – Dodger_ Jun 12 '19 at 17:19
  • Can you check if the event is attached to the `td` or `button`. you can get it by following [my answer](https://stackoverflow.com/questions/55977388/textbox-events/55978587#55978587) – supputuri Jun 12 '19 at 17:24
  • Sorry, If you don't mind can you please clarify what exactly you are exptecting our of the click? – supputuri Jun 12 '19 at 17:32
  • @supputuri: Love the screenshoot examples that you've provided. This allowed me to inspect the all the Event Listeners for various of different HTML elements. However, the bottom line is that the event is attached to the `button` not the `td` element. – Dodger_ Jun 12 '19 at 17:36
  • Thanks. Now that we are sure the event is attached to the button. Can you please let me know what your expecting out of the click (sorry I missing something here). – supputuri Jun 12 '19 at 17:39
  • @supputuri: My expectation from the click to mimic real person behavior (move the mouse over button and click on it). This action just dismisses the warning message and allows me to continue with the rest of my workflow. For any other non JavaScript related elements simple `element.click()` works just fine. In this case it starts to look like I will be forced to extract the JS code from 'onclick' button attribute, modify it slightly and run it (didn't have much success in executing raw JS `submitCommand()` command) – Dodger_ Jun 12 '19 at 17:41
  • so basically you have to trigger, `onfocus` event and `onclick` event. Can you try with `onfocus` and then clicking on the button. Seems to be `click` alone is not triggering the change. – supputuri Jun 12 '19 at 17:45
  • @Dodger, selenium will always generate these events using javascript. When you move the mouse over a button (manually) it ONLY fires mouseover or hover... not focus. It's not until the button is manually clicked that focus appears (though I'm not sure if this behavior is different on different browsers.) So if you want to simulate this, fire all the events... so, first fire hover, then mousedown, then focus, then mouseup, then click. It's worth noting that focus and click also fire if the user tabs to the button and then presses enter, however mouserover, mousedown and mouseup would not. – pcalkins Jun 12 '19 at 20:43
  • Also, the click event is fired when both mousedown and mouseup are fired on the same element. (user could have dragged off the object while mouse is down...) – pcalkins Jun 12 '19 at 20:46

2 Answers2

0

Have you tried clicking the <td> by xpath?

  • object doesn't seem to attribute that will allow me to click on it. ```AttributeError: 'FirefoxWebElement' object has no attribute 'clik'``` – Dodger_ Jun 12 '19 at 17:21
  • Good catch! `click()` on the `` element didn't return any errors as such, however the behawior is identical to `buttonelement.click()` (button class changes from button_normal to button_rollover) – Dodger_ Jun 12 '19 at 17:46
0

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)
Dodger_
  • 31
  • 3