Search code examples
javaandroidseleniumjsoup

How to get a number from a website for an an android app use


I have a question regarding scraping data form a website.

There is this exchange rate website that I want to use to get the exchange amount after an exchange from USD to EURO :

https://www.paysera.com/v2/en-DZ/fees/currency-conversion-calculator#/?locale=lt&clientType=natural&from_currency=USD&from_amount=100&to_currency=EUR

I found out that if you add any number after from_amount= and before the &to_currency=EUR (XXX), it will calculate the exchange rate of that exact amount; The exchange rate isn’t constant, so it depends on the amount.

What I want to do is make the user input the amount (inpDollar) then add it between from_amount= and &to_currency=EUR; Then extract the amount as a number.

The exact location of the amount i want is shown in this screenshot

https://i.imgur.com/79xGeG9.png

I’ve read some threads about Jsoup and Selenium, i have already added them to my dependencies, but I don’t have the necessary knowledge to write that kind of code.

I want to use this inside an android app (android studio) to automate some calculations.

Thank for your help.


Solution

  • I hope this is what you are looking for. Please take a look at the below code and let me know if you have any questions about it.

    In the code below, the user is asked for dollarvalue [Please enter dollar values in your editor and press enter]. The value is used in the URL and when the page opens with data, the required value is retrieved.

    import org.openqa.selenium.support.ui.ExpectedConditions;
    import org.openqa.selenium.support.ui.WebDriverWait;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.openqa.selenium.chrome.ChromeOptions;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.By;
    
    WebDriver driver = new ChromeDriver();
    WebDriverWait wait = new WebDriverWait(driver, 30);
    
    Scanner input = new Scanner(System.in);
    System.out.print("Enter the Dollar Value :- ");
    String dollarValue = input.nextLine();
    
    
    driver.get(String.format("https://www.paysera.com/v2/en-DZ/fees/currency-conversion-calculator#/?locale=lt&client" +
        "Type=natural&from_currency=USD&from_amount=%s&to_currency=EUR", dollarValue));
    
    String calculatedValue = wait.until(ExpectedConditions.visibilityOfElementLocated(
        By.xpath("//span[@class=\"commercial-rate\"]/span"))).getText();
    System.out.println("CalculatedValue is " + calculatedValue);
    

    for launching a browser in the background you can use the below code -

    ChromeOptions options = new ChromeOptions();
    options.addArguments("--headless", "--silent");
    

    Note - All the classes I mentioned above must be imported. If I forgot any, import them as well.