Retrieve an Excel file using Selenium through a URL, but only obtain JavaScript code instead

I am attempting to download an Excel file using its URL, but all I receive is JavaScript code. I'm unsure of how to retrieve the actual file instead of just the JS code.

Here is my current code:

# -*- coding: utf-8 -*-

from selenium import webdriver
import io
import re

path = 'C:/Program Files (x86)/Google/Chrome/Application/chromedriver.exe'
download_url = "http://samr.cfda.gov.cn/directory/web/WS01/images/localgov/gov_1540501658076.xls"  #URL provided by me

chrome_options = webdriver.ChromeOptions()
#chrome_options.add_argument('--headless')  # headless mode to disable GUI for Chrome
chrome_options.add_argument('--disable-gpu')
chrome_options.add_argument('--no-sandbox')

prefs = {'profile.default_content_settings.popups': 0, 'download.default_directory': 'd:\\new'}
chrome_options.add_experimental_option('prefs', prefs)

client = webdriver.Chrome(path, chrome_options=chrome_options)

try:
    client.get(download_url)
except TimeoutError:
    print("Time took too long")

print(client.page_source)
client.quit()

Any assistance would be greatly appreciated.

Answer №1

Even though the printed output remains constant, introducing a brief delay to ensure the file downloads successfully.

# -*- coding: utf-8 -*-

from selenium import webdriver
import time

path = 'C:/Program Files (x86)/Google/Chrome/Application/chromedriver.exe'
download_url ="http://samr.cfda.gov.cn/directory/web/WS01/images/localgov/gov_1540501658076.xls"  #url i have 

chrome_options = webdriver.ChromeOptions()
#chrome_options.add_argument('--headless')  #headless mode 
chrome_options.add_argument('--disable-gpu')
chrome_options.add_argument('--no-sandbox')

prefs = {'profile.default_content_settings.popups': 0, 'download.default_directory': 'd:\\new'}
chrome_options.add_experimental_option('prefs', prefs)

client = webdriver.Chrome(path,chrome_options=chrome_options)

try:
    client.get(download_url)
    time.sleep(5)
except TimeoutError:
    print("time too long")

print(client.page_source)
client.quit()

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

Techniques for accessing the most recent input values within a loop

Here is the HTML code snippet: <div v-for="item in my_items"> <div> <input type="text" :value=item.name /> </div> <div> <button @click="edit(item.id, item.name)">Edit ...

Can Regex expressions be utilized within the nodeJS aws sdk?

Running this AWS CLI command allows me to retrieve the correct images created within the past 45 days. aws ec2 describe-images --region us-east-1 --owners self -- query'Images[CreationDate<`2021-12-18`] | sort_by(@, &CreationDate)[].Name&apos ...

Reverse changes made to a massive object and then redo them

My current project requires the implementation of undo-redo functionality for a product. At the moment, I am managing a substantial Object retrieved from a MongoDB collection The structure is as follows: { cart:{ products:[ { name: " ...

What is the reason behind encountering these errors while trying to run my Gatsby development server?

I'm currently working on the part three tutorial provided by Gatsby on their official website and I've encountered a problem. Upon executing the command gatsby new tutorial-part-three https://github.com/gatsbyjs/gatsby-starter-hello-world I rec ...

The connection between ng-model and ng-repeat, and the comprehension of $scope

<!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script> </head> <body> <div ng-app="myApp" ng-controller="customersCtrl"> <ul& ...

What is the best way to enhance an object using a class in ES6?

In an effort to improve the clarity of my ES6 class definition, my current code looks like this: class SomeClass { constructor({a, b, c, d, e}) { this.a = a; this.b = b; this.c = c; this.d = d; this.e = e; // additional code here ...

The present IP address of the client through AJAX and PHP

This code snippet is on my PHP page: // Setting the timezone to Asia/Manila date_default_timezone_set('Asia/Manila'); $date = date('m/d/Y h:i:s a', time()); if (!empty($_SERVER['HTTP_CLIENT_IP'])){ $ip=$_SERVER['HTTP_C ...

Link clicking does not trigger URL routing properly

I've been tasked with updating our web application, and I'm facing a challenge that I need help with. My boss wants users to be able to click on a link in an email and have the internal company web app directly navigate to a specific page indicat ...

Efficiency levels of reach = within angular instructions

Creating a directive can be done in various ways. Here is an example of how I structured mine: 'use strict'; myApp.directive('mySwitchOnOff', [ '$rootScope', function($rootScope) { return { restrict: 'C' ...

Concealing items by placing them strategically between the camera and certain objects in Three.js

At the moment, my project involves utilizing three.js with several objects in the scene. One of the key features I am working on is the ability to select an object and have all other objects between the camera and the selected one hidden or removed. I am ...

Repetitive attempts have led to the cancellation of the AJAX and PHP petition statuses

Whenever I click the button, I am trying to update a MySQL table using AJAX jQuery. Unfortunately, I am encountering a problem where the AJAX jQuery does not work properly sometimes. It starts off fine, but after a certain number of attempts, it stops work ...

Obtain the image URL using Selenium and Chrome WebDriver

Currently, I am utilizing selenium webdriver in C# to attempt to retrieve the src of an image by using the GetProperty method in order to confirm that the correct image is being displayed. var productImg = product.FindElement(By.TagName("img")); var produ ...

Why does Res.send return an empty object when console.log indicates it is not empty?

I am currently facing a challenge while using the Google Sheets API with Express, as I have limited experience with JavaScript. My goal is to pass a JSON object from Express to React, but for some reason, when I send the object, it appears empty on the fro ...

angularjs: hide div based on text entered in textfield

Is there a way to hide a div (with the class "card") until the user inputs text into the textfield in an AngularJS application? I believe the solution involves using ng-show, but I'm unsure of how to implement this functionality. Any assistance would ...

Using an AngularJS array with ng-repeat

As soon as a websocket message is received, the code below executes: connection.onmessage = function (eventInfo) { var onConnectionMessage = JSON.parse(eventInfo.data); if (onConnectionMessage.messageType === "newRequest") { getQuizRequests(); } } T ...

Having issues with using the class selector in SVG.select() method of the svg.js library when working with TypeScript

Exploring the capabilities of the svg.js library with typescript has presented some challenges when it comes to utilizing CSS selectors. My goal is to select an SVG element using the select() method with a class selector. In this interactive example, this ...

JavaScript's Automated Retail Machine Feature

I'm still in the early stages of learning Javascript, having dabbled with it for a couple of weeks. This marks my first time posting, so I apologize if my explanation is not comprehensive enough, but I'll do my best! My goal is to create a funct ...

Struggling to properly render JSON data

Having trouble accessing specific elements in a script that merges local JSON files using AJAX? The script works fine in Chrome Console, but you can't reach certain elements like 'object.country'. Any suggestions on how to tackle this issue? ...

Sharing state between components in NextJS involves using techniques like Context API, passing

I am trying to pass state between pages in Next.js. In my App.js, I have wrapped it in a context provider like this: import { useRouter } from 'next/router' import { ClickProvider } from '../context/clickContext' function MyApp({ Compo ...

Add motion to the div element when hovering and moving the mouse away

Looking to add animation to a list moving from left to right and vice versa. Now, I want the list to animate from left to right when the mouse hovers over the div, and then animate from right to left when the mouse leaves the div. Can someone assist me wit ...