How to get a value from a webpage? Error in the website

Solution 1:

I made two changes to your code:

Change 1: I added a new line here to disable JS, which was causing a problem somewhere in the site:

WebClient wc = new WebClient(BrowserVersion.CHROME);
wc.getOptions().setJavaScriptEnabled(false);  // <-- new line

Change 2: I changed your HtmlAnchor classes to HtmlSpan classes:

HtmlSpan anc = page.getFirstByXPath(...); // <-- changed in 2 places

You are accessing spans, not anchors.

After these changes I got the following output:

Dolar Venta: $ 116,02
Dolar Compra: $ 115,37

Edit

I should add: If you don't want to completely disable JavaScript in the target page, you can instead use the following to allow JS processing to continue, after an error:

wc.getOptions().setThrowExceptionOnScriptError(false);

In the case of this specific web site, the data you want to access is available in the HTML - JS is not needed to access it.