简体   繁体   中英

Return DOM element from JavaScript to Java and optimize click

I want return element from DOM (JavaScript) to Java code and optimize click action on it.

Firstly I search element(button) in document by JavaScript

var buttonToReturn = document.getElementById('actionButton');

When found it(be sure that the button exist), I returned the name actionButton by mapped function

ButtonFoundFunction(this, browser, "buttonFound"); - in Java code

buttonFound(`actionButton`); - in JavaScript

Get the returned button name - actionButton in Java code by ButtonFoundFunction which extend BrowserFunction and override public Object function(Object[] arguments)... and execute click on this element as below

browser.execute("document.getElementById('" + arguments[0] + "').click();");

but document.getElementById(...) search the button second time. So what is the problem ?

I want optimize it to search button once(only in JavaScript), return whole element to Java code(the buttonToReturn ) and execute click on it without second search by document.getElementById(...) . I would like to the execute should look as below

browser.execute(arguments[0] + ".click();");

It means that I would execute in browser click on this element, not second time searching it.

I tried return the buttonToReturn but it do not work, BrowserFunction is not called(wrong type?). Maybe I should use other object for it in Java code instead Object ?

          1. searchButton()
+------+   ------------->   +----------+
| Java |                    |JavaScript|
+------+   <-------------   +----------+
          2. buttonFound()
 ^
 |        
 3. browser.execute(arguments[0] + ".click();");

EDIT

buttonToReturn is HTMLDivElement and I can not returned it.

SOLVED

I can't pass a native JS object to Java.

When JavaScript looks like that:

var buttons = [];

var buttons[0] = document.getElementById(`actionButton`);

to execute click in Java it is enough by code:

browser.execute("buttons[0].click()");

Because buttons array is in SWT Browser cache.

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