Python Selenium: нажатие кнопки с проверкой JavaScript onFocus - PullRequest
3 голосов
/ 12 июня 2019

Я пытаюсь автоматизировать навигацию по веб-странице с помощью Selenium Python.Я хочу нажать на кнопку HTML, которая выполняет код JavaScript, который проверяет, находится ли кнопка в фокусе действия 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

Ответы [ 2 ]

0 голосов
/ 12 июня 2019

Благодаря @supputuri я смог справиться с этой задачей.Хитрость здесь заключалась в том, чтобы вызвать событие onfocus, за которым следует событие onclick, связанное с моим элементом кнопки.

driver.execute_script("arguments[0].onfocus()",warning_ok_button)
driver.execute_script("arguments[0].click()",warning_ok_button)
0 голосов
/ 12 июня 2019

Вы пытались нажать <td> по xpath?

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...