employing a robotic tool to download a webpage under various file names

I am currently utilizing selenium to develop a robot that automates the process of opening and saving a webpage. Here is the code snippet:

    WebDriver driver = new FirefoxDriver();
driver.get("http://ieeexplore.ieee.org/stamp/stamp.jsp?tp=&arnumber=7043856");

Robot robot = new Robot();

robot.delay(20000);


robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_S);
robot.keyRelease(KeyEvent.VK_CONTROL);    
robot.keyRelease(KeyEvent.VK_S);

robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);

However, I have encountered two issues - firstly, pressing enter only opens the `save as` window without executing the save command. Secondly, how can I modify the script to avoid overwriting files with the same name or allow for custom file names?

Answer №1

It is true that when using driver.getPageSource(), the css, scripts, and related resources are not saved for offline viewing.

I managed to save the file successfully by simply adding a Thread.sleep() after each operation.

If you encounter issues with file names being overwritten, consider using a random number generator to append unique numbers. A simple function can help streamline this process.

See the code snippet below for reference:

// Java code example
public static void main(String[] args) throws AWTException, InterruptedException {
    // WebDriver initialization
    WebDriver driver = new FirefoxDriver();
    driver.get("http://ieeexplore.ieee.org/stamp/stamp.jsp?tp=&arnumber=7043856");

    // Letting the page load completely
    String pageSource = driver.getPageSource();

    Robot robot = new Robot();

    // Press Ctrl+S to save page
    robot.keyPress(KeyEvent.VK_CONTROL);
    robot.keyPress(KeyEvent.VK_S);
    robot.keyRelease(KeyEvent.VK_CONTROL);
    robot.keyRelease(KeyEvent.VK_S);

    Thread.sleep(5000);

    // Generating a 2-digit random number
    String random = RandomStringUtils.randomNumeric(2);
    char charOne = random.charAt(0);
   
    ...

}

public static int getKeyEvent(char key) {
   ...
}

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

Evaluating the performance of a Heroku free server under stress conditions

I'm currently working on an Android app that has a backend hosted on a Heroku free server. The current response time for serving requests is 2 seconds. I'm curious to know how many users it can handle simultaneously. I've searched through v ...

Encountering a TimeoutException while utilizing Selenium in Python

Utilizing WebDriverWait in my script involves two different instances with the same syntax. The first time I use it is to check for any typos within a defined class, while the second time is under a function nested within the same class. Surprisingly, an e ...

Select a component inside the <section> element

The element is embedded within a tag, but despite several attempts, I have been unable to successfully click on it. wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@value='Login to Register']"))).click(); Additionally, Web ...

Executing Selenium tests on Firefox version 15

I am currently attempting to use Selenium 2.5 with Firefox 15.0.1 on Ubuntu 20.04 LTS using python2. Below is the Python script I am trying to run: from selenium import webdriver driver = webdriver.Firefox() However, I encounter the following error messa ...

Where can I find the text fields for the First name and Last name?

Can someone help me inspect the textboxes and button? I need to use the sendkeys function for the textbox. Sample HTML Code : <!DOCTYPE html> <html> <body> <h2>HTML Forms</h2> <form action="/action_page.php"> Fir ...

Is there a way to retrieve the current version of selenium, selenium server, or selenium webdriver through programming methods?

Currently, I am conducting automated tests across different environments and it is crucial for me to understand the runtime scenario accurately. I have noticed discrepancies in the performance of identical Ruby scripts and I want to be able to gather as m ...

Tips for saving error messages to a text file after Selenium WebDriver Java scripts fail:variables can be utilized to capture and store exception and error

Can anyone assist me with saving exception and error messages in a text file for only the failed scripts using TestNG, Jenkins, Java, and hybrid automation framework? Any help would be greatly appreciated. ...

Using the power of Selenium's XPath, we can easily navigate through a table to access

Looking for assistance in creating selenium xpath for the various column links within a table. Each row has a unique identifier and contains information about a product, including the name. Based on the product name, I need to locate other links within the ...

Having trouble applying CSS to multiple classes used in an AJAX call

I'm trying to utilize richfaces for some fast AJAX widgets, but I am encountering difficulties in setting CSS parameters for them. After inspecting the generated code, I found that the elements have the classes "rf-ds" and "rpds". Despite applying st ...

The JSONObject contains a value, however it may sometimes return a null value

[ { "valve": "4679", "selling": "5516", "bal": "9075.4", "o id": "37", "invested": "11122", //<<<<<<<- this value returns null "aProfit": "1012", //<<<<<<<- this value returns null ...

When using headless Chrome, the element attributes may differ from those in regular Chrome, causing Selenium to throw a NoSuchElementException

This particular snippet of code functions properly when used in the regular version of Google Chrome: from selenium import webdriver from time import sleep def login(email, password): with webdriver.Chrome('/path/to/chromedriver') as drive ...

Identify and enumerate the actionable components within a web page

I need assistance developing a Java function that can identify and return the count of interactive objects on a webpage that trigger an action when clicked, but excluding hyperlinks. Examples include buttons, image buttons, etc. Does anyone have any sugge ...

The WebDriver Manager for Selenium Automation is experiencing issues with the latest Chrome update, version 116

I implemented the selenium framework and included this dependency. However, I encountered an issue where the browser wasn't being invoked due to Chrome version 116. <dependency> <groupId>io.github.bonigarcia</groupId> <art ...

How to programmatically interact with a dropdown menu without a select element in Python using Selenium

Hey there, I'm currently practicing my skills with Selenium on a practice forum. If you're interested in checking it out, here's the link: click here When you visit the page and inspect the dropdown menu for state and city, you'll noti ...

Send JSON information to a Spring Boot server application

I am brand new to working with Spring Boot. Currently, I am attempting to send registration form data (in JSON format) from a Vue frontend to a Spring Boot backend. However, the backend always indicates that the received data is null. How can I properly re ...

Steps to obtain browser network logs with Python Selenium

Seeking assistance on retrieving browser network logs through Selenium for troubleshooting request/responses. Any guidance would be greatly appreciated. Currently utilizing Selenium 3.14.0 in conjunction with the most recent version of Chrome browser. ...

What is the process for establishing a class within PageObject framework?

As a beginner in Selenium, I am using IntelliJ along with Selenium WebDriver and Junit. My current challenge lies in setting up the TestBase class within the PageObject framework. Below is an excerpt of my TestBase class: import org.junit.After; import or ...

Generate a custom website using React to display multiple copies of a single item dynamically

As a newcomer to React and web development, I've been pondering the possibility of creating dynamic webpages. Let's say I have a .json file containing information about various soccer leagues, structured like this: "api": { "results": 1376, ...

What is the best way to connect a fresh post to a different URL in ReactJS and Redux?

Currently, I am working with Redux and ReactJS to develop a discussion platform. One of the key features I am trying to implement is where users can input the title and content of their post, which will then be submitted to create a new post. After submit ...

Exporting table data to a CSV file using Selenium and Python

My goal is to automate the process of extracting my transcript data and saving it to a CSV file using Python with Selenium. I've managed to automate reaching the table page, however, I am facing an issue where only the first row of the table is displa ...