What is the process for retrieving data from a website?

Currently, I am in the process of setting up an automated test using the basic page object pattern. The objective is to open Google, input a search query, click on a link from the results, and retrieve the information. So far, I have successfully navigated to the desired page. However, my challenge lies in extracting the contact information located at the bottom of www.sahipro.com and displaying it in the console of my test class. Despite my efforts, I find myself unable to filter out only the emails and numbers as I keep getting additional HTML content. Furthermore, I'm struggling to understand how to reference these page results within my test class.

The task of isolating the contact text and transferring it to a separate test class has proven daunting for me, considering my limited experience in this field.

try {

//Get Document object after parsing the html from given url.
Document document = Jsoup.connect("http://https://sahipro.com/").get();

Element support = document.text("<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5e2d2b2e2e31...">
print("  Support Email: " + support);

}catch (IOException e) {
e.printStackTrace();
}

print("done");
}

public static void print(String string) {
System.out.println(string);
}

Although I have managed to fetch the information, it appears in HTML format when displayed in the console:

running...
Support Email: <html>
<head>
<meta http-equiv="refresh" 
content="0;url=http://www.dnsrsearch.com/index.php
origURL=http://https%2Fsahipro.com%2F&amp;bc=">
</head>
<body>
<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="8ffcfaffffe0fd...">[email protected]</a>
</body>
</html>

Answer №1

Utilize the Element class method in jsoup to retrieve text from a specified node, rather than extracting text from the document root directly. You can achieve this by using the select method to target a specific node.

 document.select("a[^='mailto:']")[0].text();

Implementing this approach into your code:

try {

 //Retrieve Document object after parsing the HTML content from the provided URL.
      Document document = 
      Jsoup.connect("http://https://sahipro.com/").get();

       String support = document.select("a[^='mailto:']")[0].text();
        String phone = document.select("a[^='tel:']")[0].text();

      print("  Support Email: " + support + " Support Phone: "+ phone ); 

     } catch (IOException e) {
      e.printStackTrace();
     }

     print("done");
 }

public static void print(String string) {
    System.out.println(string);
 }

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Tips for accessing files following the transmission of a post request within the req.body

Encountering a problem where image uploads to an s3 bucket are not successful. The error message received is: API resolved without sending a response for /api/upload/uploadPhoto, this may result in stalled requests. The front end includes an input that ca ...

Is there a way to use Selenium to click on "javascript:void(0)"?

Can someone please help me figure out how to click on the Login button within the HTML code provided below? I am not very knowledgeable in coding, so any assistance would be greatly appreciated. I am using Selenium with Python 3.9. Thank you all so much f ...

JavaScript tag filtering system: Display only the div elements that contain all the specified classes in an array; otherwise, hide them

Can you provide some guidance on accomplishing this task? I am looking to toggle the visibility of a div based on its classes and an array. If the array contains the term something, then only divs with the class something will be displayed. If the array ...

Encountering difficulties while attempting to convert JSON to XML resulting in an error being

I can't seem to figure out how to successfully convert the JSON result into XML format. I've tried the code below but it's not working as expected. Any help would be greatly appreciated. Here is the code snippet: <script src="../Jquery ...

Encountering timeout issues with Next.JS fetch and Axios requests on Vercel production environment

I've been encountering an issue where I am unable to fetch a specific JSON data as it times out and fails to receive a response on Vercel deploy. The JSON data I'm trying to fetch is only 18KB in size and the fetch request works perfectly fine in ...

Utilize vue.js to cache and stream videos

I'm new to the world of vue.js and I'm facing a certain dilemma. I implemented a caching system using resource-loader that preloads my images and videos and stores the data in an array. Everything is functioning correctly, but now I'm unsur ...

Issue: EMFILE - exceeding maximum number of opened files

I'm currently working with nw.js, attempting to save images in an array of img tags with random names. However, I've encountered a few errors and I'm unsure what's causing them. for (i = 0; i < imgs.length; i++) { request(imgs[i].g ...

Downloading a file utilizing Selenium through programming (versions 2.46 and above)

The FileDownloader class was functioning well until the upgrade to selenium 2.46: Guide on downloading files with Selenium in Java Upon running the test with selenium 2.46, I am now redirected to the login page. Has anyone else encountered this problem? ...

Switches in a React-Native ListView are not refreshing properly

Situation: I encountered an issue with a ListView of Switches Problem: The Switches are not changing state when pressed. When debugging, each switch appears to be checked momentarily after the setValue function is called, but then reverts back to unchecked ...

Troubaling with AngularJS Routing issues

I am encountering an issue with my routing system. The "otherwise" case is functioning correctly, however, when I click on a menu item, the routing does not load the corresponding page automatically. Can someone assist me in identifying what is wrong with ...

Employing the validateAll() function in conjunction with a personalized v-select component within the scope

One of my recent scenarios involves scoping a form in order to validate it using the Vee-Validate method shown below. validateTRForm: function (scope) { this.$validator.validateAll(scope).then((result) => { if (result) { } ...

Issue with HTTP headers not being effective in $.AJAX request

Regardless of the method I attempt to use for setting headers in an AJAX call, and no matter which header I set, every time there is a header present, the AJAX call is aborted and does not go through. However, if I try to make the call without setting any ...

Having trouble with loading the Select locator in Selenium from a properties file using Java

I have stored my locators in a Java properties file. All the locators are working correctly except for the Select locator. I have verified that the locator is being printed when assigned to a string variable. However, it does not work when used in the Sele ...

What is the reason for the PHP condition not functioning properly unless I explicitly echo the variable?

When the variable is echoed in the code below, the condition works, but if it's not echoed, the condition fails to work. What could be causing this issue? The original code snippet: $msg=$_SESSION['$msg']; echo $msg; if($msg != null){ ?> ...

Using a computed property setter in Vue.js/Javascript while focusing on a datepicker can lead to crashing the browser

Can anyone explain why my javascript / vuejs code is crashing on my JSFiddle when I focus on the start date datepicker (causing the browser to hang)? If you uncomment the endDate computed property and comment out the current one, it works fine but the fun ...

Refining search outcomes using multiple Multiselect boxes in VueJS

In my Vue component, I have successfully displayed results from a data object and created multiple multiselect boxes. However, I am facing challenges with filtering. I understand how to set and compare a single value from the multiselect for displaying sp ...

set up the router by defining the behavior for each route

My goal is to redirect the user back to the homepage when they click the browser's back arrow to return to the previous page. However, I'm facing an issue where I cannot go back to the previous page when I'm on the login page using the brow ...

Practicing diligently, JavaScript code snippets

In my app using Playframework 2.2, I heavily rely on ajax to load content dynamically. There are times when I need to bind or change an event handler after loading content via ajax. For example, if I want to modify the click handler for a newly retrieved ...

Implementing dynamic URLs using static routing in Express

I'm looking for a way to render a page statically in Express that involves using a dynamic URL. For example, In my forum, users create posts and each post has its unique URL that shows the post when clicked: localhost:8080/posts/postNumber Current ...

Using jQuery .animate() leading to erratic input movements

I am currently utilizing jQuery's .animate() feature to create a smooth animation effect on the width of a <div> element when a child <input> is in focus. Nevertheless, I'm encountering an issue where the input field jumps up and down ...