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

13
  • 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: stackoverflow.com/a/13938778/9538369 Commented Jun 12, 2019 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() Commented Jun 12, 2019 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. Commented Jun 12, 2019 at 16:43
  • Check if driver.execute_script("arguments[0].click()",warning_ok_button) triggers the javascript. Commented Jun 12, 2019 at 17:00
  • @supputuri: That is great suggestion, but I'm afraid it didn't work this time around. Commented Jun 12, 2019 at 17:19

2 Answers 2

0

Have you tried clicking the <td> by xpath?

Sign up to request clarification or add additional context in comments.

2 Comments

<td> object doesn't seem to attribute that will allow me to click on it. AttributeError: 'FirefoxWebElement' object has no attribute 'clik'
Good catch! click() on the <td> 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)
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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.