Is there a way to extract and exhibit the text from an image using Python?

Could someone assist me in extracting only the text within the highlighted red area? I have been experimenting with Python but haven't been successful in achieving this. My goal is to create a script that prompts for an address, then opens Firefox (or Chrome), navigates to Google's website, and searches for the travel time and distance from the provided address saved in the Python script. The plain text from the red square needs to be displayed on the command screen.

I would greatly appreciate any guidance on how to access this element. Below is what I have attempted so far:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
import time




print("Please enter an address")
address = input()

driver = webdriver.Firefox()
url = r"https://www.google.com"
driver.get(url)

search_box = driver.find_element_by_name("q")

search_box.send_keys("Navigate to "+ address + " "+"from terror st keilor park" + Keys.RETURN)

result = driver.find_element_by_xpath("//div[@id='exp0']/div[1]/div/div[@class='BbbuR uc9Qxb uE1RRc']")

print(result.text)

View reference image here

Answer №1

Utilize the WebDriverWait function to wait for the element until it is clickable using the specified xpath.

element=WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.XPATH,'//div[@data-rre-id="exp0"]')))
print(element.text)

In order to run the above code successfully, make sure to import the following libraries.

from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By

Answer №2

Consider leveraging the Google Maps API to directly access data from Google in Python, eliminating the need to manually open a browser, capture an image, and then process the text within it.

Alternatively, albeit not recommended, you could send a request through Python code to retrieve and parse the webpage's response.

To learn how to make web requests in Python, search "python requests examples."

Next, research "python parse HTML" to understand how to extract specific information from web pages using code.

There are numerous methods to acquire the desired information, with optical character recognition on images likely being the least efficient. However, if you're determined to pursue this route, look up "python optical character recognition."

I trust this guidance will prove beneficial :)

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

A guide on encrypting the data of a file that is uploaded in Meteor.js

As a newcomer to Meteor and coding in general, I have completed tutorial projects and examples and now feel ready to start my own project. I want users to be able to select a file from their computer using an input field, read the contents of the file, and ...

additional one hour of generated report added >

My Select option code is causing some issues. When I view the uploaded data, there seems to be a duplication and an extra tag at the end: <option value="Running Request |Running > 1 hour">Running Request |Running > 1 hour</option> The ...

What causes jquery height and javascript height to both be returned as 0?

I'm facing an issue with a visible div on my screen - despite being visible, its height always returns as 0. I've attempted various jQuery and JavaScript methods to retrieve the height, but it consistently shows as 0. Here's the structure of ...

When using Selenium WebDriver's get_attribute method, it may return a truncated value of the href attribute if the value

I've been attempting to extract the href attribute value from an anchor tag on a specific page within my application using Selenium Webdriver (Python), but I'm encountering an issue where part of the result is getting stripped off. Below is the ...

Instructions for adding and deleting input text boxes on an ASP.NET master page

*I am currently facing an issue with adding and removing input textboxes for a CV form using ASP.NET in a master page. Whenever I click on the icon, it doesn't seem to work as intended. The idea is to have a textbox and an icon "+" to add more textbox ...

Select all nodes in XPath except for the `figure` node

I am currently utilizing the scrapy framework with python 3.7.9 to scrape articles from various Britannica websites. Below are the provided links for reference: link1 link2 For extracting text, I am implementing the XPath expression //section[@id='r ...

How can I populate a <select> tag with options dynamically using C# when the page loads?

I am using jQuery ajax to populate cascaded dropdown elements from a database. The issue I'm facing is that I can't seem to add the default "Select Program" option at the top of my first dropdown dynamically. Even though I tried using the prepend ...

Tips for limiting the .click function to only the initial image in order to prevent loading all images within the container

I am facing a situation where multiple images are being returned into a specific div, which is working as intended. <div class="go" id="container"></div> Upon clicking on an image, it is loaded into a modal popup. However, instead of capturin ...

Tips for keeping a Youtube video playing even after the page is refreshed

Is it possible to save the current position of a Youtube video and have it resume from that point when the page is refreshed, instead of starting from the beginning? I am considering using cookies to store the last position or utilizing GET. Although my w ...

Scrape dynamic web data with JSOUP

Having trouble grabbing the price? I cannot seem to get any output for the price and its weight. I've attempted different methods, but nothing is working Document doc = Jsoup.connect("https://www.jakmall.com/tokocamzone/mi-travel-charger-20a-output- ...

Identifying activity on a handheld device

I am currently working on a website and I have noticed that it doesn't work as well on mobile devices as it does on desktop. There are performance issues that need to be addressed. I've seen other websites redirecting users to a different page wh ...

Pause before running a command (Selenium WebDriver)

Working with Java in Eclipse using Selenium WebDriver, I am looking for a way to wait for a command to be successfully executed without specifying a specific amount of time like Thread.sleep(). Ideally, I want the command to be executed immediately after ...

Retrieve numerical data from the element and enclose it within a span tag

My goal was to indent a number, but the HTML generated from my CMS is making it difficult to target the number and apply a specific style. <div class="content"> <b>01. Header</b> Lorem ipsum dolor sit amet, consectetur adipisicing elit, ...

What is the best way to incorporate a custom modal created with HTML/CSS into my Bootstrap website?

I am facing an issue with the modal implementation on my bootstrap website. The modal is supposed to open when a user clicks a button. Strangely, it was working perfectly fine on another HTML/CSS file, but once I added it to this Bootstrap file, it stopped ...

Error encountered in ngtsc(2345) where an argument of type 'Event' is being used instead of an expected parameter type of 'SortEvent'

I recently started using angular and encountered an issue while trying to sort columns. The error message I received was: Argument of type 'Event' is not assignable to parameter of type 'SortEvent'. Type 'Event' is missing t ...

Transforming beautifulsoup content back into HTML format

Currently, I'm utilizing beautifulsoup to extract, parse, and modify an HTML document. The process seems to be functioning flawlessly; however, upon converting the soup object back into HTML using prettify(formatter="html"), I've notice ...

Setting up an event listener for a newly added list through the use of node appendChild

I am currently working on dynamically adding a select list to my HTML document. While I have successfully added the node to the DOM, I am struggling with creating an event listener in a separate JavaScript file that recognizes the newly created select list ...

Hide the scrollbar in CSS when it is not needed

I need help figuring out how to hide the scroll bar using overflow-y:scroll;. I am working on a website where posts are displayed in a main area, and I want to only show the scroll bar when the content exceeds the current width. My second question is abou ...

PHP Bootstrap Confirmation Dialog Tutorial

Is there a way to implement a delete confirmation dialog? When 'yes' is clicked, the message should be deleted, and when 'no' is clicked, the delete operation should be canceled. At present, this is how it looks in my view: <a href ...

How can we best understand the concept of custom directives using the link method?

As a beginner in AngularJS, I am looking to implement an autocomplete feature for text input. My JSON data is stored in JavaScript and I need help simplifying the process. Can you provide me with a straightforward solution? The specific requirement is to ...