V
V
Vladislav2019-12-06 05:18:36
Python
Vladislav, 2019-12-06 05:18:36

How to simulate a click on a button (Selenium, JS, Python, Chrome)?

The task is to emulate a click on the “Verified” button on a tricky UX. button "Checked" has the property of being triggered by the right mouse button, as well as the left button. There is no such thing with the “Marry” button. All elements with dynamic attributes, so a script was made to search for an element by text in it.
In Selenium, click() doesn't fire on any button. Tried to implement via driver.find_element_by_partial_link_text;
Event chains ActionChains(driver);
driver.find_elements_by_css_selector;
with a further click() on the element, it didn't work anywhere on those pages
Then I pasted the JS code through driver.execute_script and it worked, except for the "Checked" button

setTimeout(function() {
      let link = document.querySelectorAll('label'); 
      link = Array.from( link ).filter( e => (/Элемент1/i).test( e.textContent ) );
      link[0].click(); 
    });

or such a variant, equivalent.
const buttonNodes = document.getElementsByTagName('label')
const links = Array.from(buttonNodes).filter(e => (/Элемент1/i).test( e.textContent ))
links[0].click()

The script searches for all elements by tag, adds to the array, filters and clicks.
We change the tag and text, everything works fine on the buttons "Marry" and "ok". But doesn't work with checked even though outerHTML is the same in structure
const buttonNodes = document.getElementsByTagName('button')
const links = Array.from(buttonNodes).filter(e => (/В брак/i).test( e.textContent ))
links[0].click()

OuterHTML button checked
<button class="x-btn-text" type="button" style="position: relative; width: 94px;" tabindex="0" aria-disabled="false">Проверено<img onload="this.__gwtLastUnhandledEvent=&quot;load&quot;;" src="%D0%A1%D0%9F%D0%9E%20%D0%A1%D0%90%D0%A4%D0%90%D0%9F_files/clear_002.gif" style="width: 24px; height: 24px; background: rgba(0, 0, 0, 0) url(&quot;https://192.168.80.2:6780/WebModule/vf2011/01B9092B7306E43EFF062BE4A1F4F390.cache.png&quot;) no-repeat scroll -798px -192px; position: absolute; left: 0px; top: 4px;" role="presentation" class=" x-btn-image" border="0"></button>

OuterHTML Button Marriage
<button class="x-btn-text" type="button" style="position: relative; width: 94px;" tabindex="0" aria-disabled="false">В Брак<img onload="this.__gwtLastUnhandledEvent=&quot;load&quot;;" src="%D0%A1%D0%9F%D0%9E%20%D0%A1%D0%90%D0%A4%D0%90%D0%9F_files/clear_002.gif" style="width: 24px; height: 24px; background: rgba(0, 0, 0, 0) url(&quot;https://192.168.80.2:6780/WebModule/vf2011/01B9092B7306E43EFF062BE4A1F4F390.cache.png&quot;) no-repeat scroll -798px -168px; position: absolute; left: 0px; top: 4px;" role="presentation" class=" x-btn-image" border="0"></button>

console.log(typeof links[0])returns Object returns False The same response is returned on the "Marry" button, although it works. Passed return links[0] to Python, requested the print of the element, its coordinates and size, this is what it says:
console.log(links[0] instanceof(HTMLElement))
<selenium.webdriver.remote.webelement.WebElement (session="052cc0daa6975bc23282c08ce26fc5fa", element="106630e2-439a-4e47-b03f-d4fb6641ec46")> 1592 650 {'height': 32, 'width': 94}

I also tried to emulate a click with a pixel shift in Selenium using move_to_element , move_by_offset , it's useless.
Any ideas? Make click on pixels in JS? At the extreme, emulate the Tab and Enter keys, which will slow down the app many times over (more than 30 events are needed + everything will go when editing the UI) and it’s also not a fact that it will work.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladislav, 2019-12-13
@vlatek

It turned out that the front was assembled on GWT, but it turned out so crooked that the event hangs only on mousedown on the parent of the fifth generation of the button button O_O This parent is a table in which several dozens of "empty" elements are nested.
I solved the problem with this JS crutch:

var buttonNodes = document.getElementsByTagName('button') //ищем все элементы по тегу
var links = Array.from(buttonNodes).filter(e => (/Проверено/i).test( e.textContent )) //ложим в массив и фильтруем по тексту в кнопке
var link= links[0] //выделяем первый найденный элемент
parent = link.parentNode;  //ищем родителей
parent2 = parent.parentNode;
parent3 = parent2.parentNode;
parent4 = parent3.parentNode;
parent5 = parent4.parentNode;
var clientRect = parent5.getClientRects() //выделяем координаты элемента
clientX = clientRect[0].x
clientY = clientRect[0].y
var o = document.createEvent('MouseEvents'); //создаем событие по координатам
o.initMouseEvent( 'mousedown', true, true, window, 1, 0, 0, clientX, clientY, false, false, true, false, 0, null );
parent5.dispatchEvent(o);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question