Jump to content
Gary

Click button from WVBrowser

Recommended Posts

Delphi 11.2 WebView4Delphi

Hello all,

I posted on StackOverflow yesterday, but no solution, probably because this seems to be such an easy fix.

 

I am totally new to Web/JavaScript programming and just need to refresh and download a csv file from a geo location service every half hour.

I have everything working except what should be the easiest part, simply clicking the refresh button on the page.

 

I am using an ActionList and menu to test each step for now. What I have so far:

A running app that can navigate to the site, open the report, and download the file to a Nas drive overwriting the previous file. The download logs when finished.

 

Before downloading I need to refresh the report. If I inspect the element in the browser it takes me to this list item:

 

<a id="linkRefresh" href="#" class="tools refresh report-action" data-action="refreshReport" data-event="click"> <span class="report-edit-icon refresh"></span> <span class="tool-label">Refresh</span></a>

 

In my test action I have this code:

procedure TfrmLocation.actRefreshReportExecute(Sender: TObject);
begin
  WVBrowser.ExecuteScript('document.getElementById("linkRefresh").click()');
end;

Nothing happens, is there something I'm missing? Is there an easier way to do this? Is there a way to verify that a valid element is being returned? Can there be more than one document in the page, this button is inside a frame with a #document element?

 

I have spent 2 days on this so far, any help would be appreciated.

 

 

 

Screenshot 2023-01-25 142655.png

Share this post


Link to post

Run the MiniBrowser demo and navigate to that web page. Then click on the top-right button and open the DevTools window.

Switch to the Console tab, type your JavaScript code and press enter to read the error message :

document.getElementById("linkRefresh").click();

The problem could be something as simple as a syntax error or that page could have multiple documents because it could have several frames. In any case, debugging the JavaScript code in the console tab is the best solution before calling WVBrowser.ExecuteScript

Edited by salvadordf

Share this post


Link to post

@salvadordf

I figured it was a matter of drilling down to the element.

3 hours later after much trial and error, Success!!

 

Quote

procedure TfrmLocation.actRefreshReportExecute(Sender: TObject);
begin
  WVBrowser.ExecuteScript('iframes = document.getElementsByTagName("iframe");'+
  'iframes[1].contentWindow.document.getElementById("linkRefresh").click();');
end;

So frustrating spending so much time for what you already know is going to be 1 or 2 lines of code because of your ignorance but satisfying when done.

Thank you so much.

  • Like 1

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×