Is there a way to extract and store an image from a webpage using selenium, beautifulsoup, and Python 3?

Currently, my main goal is to extract and save a single image from a website post logging in. After examining the image, I discovered that it has a full xpath of

/html/body/form/main/div/section/div[1]/div/div[2]/div/img
. My plan is to utilize beautiful soup or an image crawler to save the image into a variable and then use tesseract to extract text from the image. So far, I've encountered difficulties with urllib, urllib.requests, and selenium's method of reading images by xpath. I initially attempted to use selenium to save the image but yielded no successful outcomes. At this point, I am seeking assistance with the coding aspect to determine if it's feasible to store the image in a variable and whether tesseract can access the image through that variable. Both the image samples and their inspection images are provided below (the highlighted image showcases the inspected text). Please note that the form displayed is only a representation and does not actually exist in reality - at least to my knowledge. Any guidance on this matter would be greatly appreciated. Thank you.

Image 1:

https://i.stack.imgur.com/kpJ55.png

Image 2:

https://i.stack.imgur.com/DEygr.png

Answer №1

To save the image, you can utilize urllib.

import urllib
from selenium import webdriver

driver = webdriver.Chrome()
driver.get(WEBSITE_URL)

# locate and retrieve the image  
img = driver.find_element_by_xpath('/html/body/form/main/div/section/div[1]/div/div[2]/div/img')
src = img.get_attribute('src')

# download the image
urllib.request.urlretrieve(src, "img.png")

This method will store the image in a file named img.png within your current working directory. Subsequently, you may employ image processing and tesseract to extract text from it. It is advisable not to solely rely on static XPATH for image detection, as changes made by the website's owner could disrupt this process. Instead, consider using:

img = driver.find_element_by_id("ContentPlaceHolder1_Imgquestions")
,

This way, even if there are modifications to the website layout, you'll still be able to locate the image based on its unique id.

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

Switch the ng-bind-html option

Dealing with a string in my scope, I must determine whether I want the HTML escaped or not. A boolean value helps to decide if the HTML should be escaped or left as is. Snippet Check out some of my code examples below: $scope.result = "<b>foo</ ...

Having difficulty in setting a Cookie with php

Working on integrating social login, I attempted logging in with Facebook. Upon successful authentication of the user, I aim to retrieve their value and store their ID in a cookie to establish a session throughout the site. Below is the code snippet, < ...

What can I do to prevent Masonry from floating all of my grid items to the left?

Is there a way to center justify the masonry grid items? I have noticed that my Masonry layout is aligning all the grid items to the left or justifying them to the left side: <div class="wrap"> <div class="parent grid"> <div class=" ...

Allow users to zoom in and out on a specific section of the website similar to how it works on Google Maps

I am looking to implement a feature on my website similar to Google Maps. I want the top bar and side bars to remain fixed regardless of scrolling, whether using the normal scroll wheel or CTRL + scroll wheel. However, I would like the central part of the ...

Aligning the icon within the div and adding a gap between each div

I'm fairly new to web design, specifically dealing with html and css, and I'm attempting to create something similar to the design in the following image: My struggle lies in centering the fa-envelope-o icon both horizontally and vertically. I&a ...

Customize your CSS line height for the bottom of text only

Hey there, I'm pretty new to frontend development so please excuse me if this is a silly question :) I know that it's not usually done, but when you apply line-height to an element like an h1, it adds extra space both on the top and bottom of th ...

Looking to position the Secondary Navigation Bar on squarespace at the bottom of the page, distinct from the primary Navigation Bar

When SALES PAGE becomes the secondary navigation option Instructions for positioning style to Bottom Center I am attempting to place it at the bottom of the navigation bar. Can you provide me with the necessary code or styles in squarespace? When I choose ...

Discrepancies in Span Element Behavior Between Firefox and Chrome

Seeking assistance with a tooltip feature that reveals extra information when a user hovers over a span. The issue arises when the span is split across two lines, causing the extra information to appear in different positions on Firefox and Chrome browsers ...

Is there a way to implement a scrollbar that only scrolls through one specific column in an HTML table?

I need help adding a scrollbar to a specific column in an HTML table. Take a look at this scenario https://jsfiddle.net/6wpdc4tL/: https://i.stack.imgur.com/svzIg.png This table should have two scrollbars, one for the light blue SCROLL column and another ...

Struggled to Find a Solution for Code Alignment

Is there a tool or software that can align different types of codes with just one click? I've tried using the Code alignment plugin in Notepad++, but it hasn't been effective. For example, when aligning HTML code using tags, I couldn't find ...

Creating a JSON file using an object to send requests in Angular

In my Angular 7 project, I am trying to send a multipart request to the server that includes a file (document_example.pdf) and a json file containing a data object (data_object_example.json). The content of data_object_example.json is as follows: { " ...

Include a scrollbar within a div element nested inside a table cell

index.html <div id="wrapper"> words go here, many of them </div> style.css #wrapper { height: 100%; width: 200px; overflow: auto; } I have been struggling to display the scroll bar on my page, and this is what I am currently exp ...

How can I obtain the current state of HTML checkboxes from a local JSON file?

I have an HTML table with checkboxes, and I want to save the state of each checkbox in a local .JSON file instead of using localStorage. When the page reloads, I want it to automatically load the saved JSON file to retrieve the previously configured checkb ...

Bootstrap-tour is incompatible with a row within a table structure

Is there a way to highlight a table row effectively? I've been struggling with it and tried using the fix mentioned in this bootstrap-tour issue here Check out this demonstration on jsFiddle: jsFiddle JAVASCRIPT $("#dialog").dialog(); var t = new ...

What is the best way to redirect to the index page after successfully submitting a new record on the front-end? [Using Rubymine 2020.2.3, Ruby 2.7.2p137, and gem 3.1.2]

For weeks now, I have been struggling with a college project course, unable to resolve an error despite the advice given by colleagues and tutors. In my create method for a specific table, I am facing an issue where the page should redirect back to the in ...

Tips for locating Xpath when the identifiers for id, class, and type are identical

How can I select the X path for the code snippet provided below, which has the same ID, Class, and Type? <tbody> <tr id="mytr"> <td id="PanelTable" class="menulevel1norm" onclick="PopupWin('Left',divMenuYANTRA_MENU_KE ...

Stylish CSS for your website's navigation

I am facing a challenge with creating a menu for my website. I want it to be positioned on the left side, similar to the image provided below. However, despite writing the code and applying styles as expected, the menu does not display correctly and appear ...

jQuery live DataAttribute manipulation

Here is the code snippet I am working with: <head runat="server"> <title>Sidebar</title> <link href="css/style.css" rel="stylesheet" /> <script src="scripts/jquery-1.11.1.min.js"></script> <script src ...

Choosing a specific item from a drop down menu in Selenium WebDriver

My challenge is to choose a specific option from a drop down list using .NET Selenium web driver (the selected options will then be submitted as part of the form). I am attempting the following method (which seems to work when I debug step by step - the o ...

Firefox compatibility issue: Bootstrap modal button not functioning properly when wrapping other HTML elements

Hey everyone, I've come up with some awesome image hover effects that you can use. I've implemented bootstrap modals so that when you click on 'show code', a pop-up will display with the HTML and CSS codes for easy copy-pasting. However ...