Python script utilizing Selenium is returning an empty string when trying to extract data from

When I try to retrieve the value from a dynamically loading table by clicking on the TD element, it works fine. However, when I attempt to fetch the text from the same TD element, it returns an empty string despite trying XPATH and CSS Selector methods.

Here is what the UI looks like; I am trying to extract the amount: https://i.stack.imgur.com/dw2o7.png

Below is the HTML Snapshot https://i.stack.imgur.com/tfYrL.png

amount = driver.find_element_by_xpath('//*[@id="Table_Cheque_Ref_Details"]/tbody/tr[2]/td[11]').click

This part is working properly.

amount=driver.find_element_by_xpath('//*[@id="Table_Cheque_Ref_Details"]/tbody/tr[2]/td[11]').text
print amount

This is returning an empty string.

amount = WebDriverWait(driver, 10).until(EC.text_to_be_present_in_element((By.XPATH, '//*[@id="xfe38"]'), '0')) print amount

This is resulting in a Timeout Exception.

Answer №1

Solution

It has been pointed out by @Andersson that you can utilize get_attribute('value') to retrieve the text from an input field. Let's illustrate this with the following example:

driver.get('http://demo.automationtesting.in/Register.html') # access sample page
time.sleep(3) # wait for page load

input_el = driver.find_element_by_xpath("//*[@id='basicBootstrapForm']/div[4]/div/input") # locate phone input field
input_el.click()
input_el.send_keys("123")

print(input_el.text) # no output
print(input_el.get_attribute("value")) # prints '123'

Analysis

If we inspect the dev tools:

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

We will observe the HTML snippet representing our input:

<input type="tel" class="form-control ng-touched ng-dirty ng-valid-parse ng-invalid ng-valid-required ng-invalid-pattern" ng-model="Phone" required="" pattern="^\d{10}$">

The tag does not contain any visible text, hence calling

print(input_el.text)

yields no result. However, if the HTML had resembled:

<label class="col-md-3 col-xs-3 col-sm-3 control-label">Email address*</label>

then

print(label_el.text)

would display the content within the label tag - Email address*.

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

Error: Function call in PySpark dataframe with IndexError due to exceeding list index range

While attempting to execute the function below in PySpark, I encountered an index out of range error. key = Issue_type=["35 USC 101","35 USC § 101","35 U.S.C. 101","35 U.S.C. § 101","§ 101","35 USC 102","35 USC §102","35 U.S.C. 102","35 U.S.C. § 102", ...

Can the pandas apply function be enhanced for better performance within groupby operations?

Presenting my dataframe - df, with detailed information about students: Stud_id card Nation Gender Age Code Amount yearmonth 111 1 India M Adult 543 100 201601 111 1 India M Adult 543 100 201601 11 ...

Struggling to map JSON data (received from WCFRest) onto an HTML table

After creating a WCFRestful service that populates data in JSON format as shown below: {"GetEmployeesJSONResult":"[{\"Name\":\"Sumanth\",\"Id\":101,\"Salary\":5000},{\"Name\":\"Sumanth\",\"I ...

What could be causing the incorrect updating of React State when passing my function to useState?

Currently, I am in the process of implementing a feature to toggle checkboxes and have encountered two inquiries. I have a checkbox component as well as a parent component responsible for managing the checkboxes' behavior. The issue arises when utiliz ...

What is the best way to assign attributes to multiple HTML elements using an array?

Seeking assistance to hide various sections in my HTML file upon button click, with the exception of one specific section. Encountered an error message: Uncaught TypeError: arr[i].setAttribute is not a function Here's a snippet of my code: const hide ...

Phonegap's JavaScript canvas feature is experiencing issues

Recently, I came across a JavaScript bouncing ball animation that works perfectly on the Chrome browser when used on a PC. However, when I tried running it using Phonegap Eclipse Android emulator, I encountered an issue where the canvas appeared blank and ...

Share your hefty files through an online portal

What is the most effective method for allowing users to upload large files from their web browsers to a server? We are talking about file sizes of 200MB or even up to several gigabytes. I have brainstormed some potential solutions to this issue, although I ...

CSS Grid having trouble with wrapping elements

Greetings to all, As a newcomer to the world of web development, I am currently exploring the wonders of the CSS grid tool. However, I have encountered a roadblock: My intention is for the cards to automatically flow one by one into the next row while ma ...

PySpark - Utilizing Conditional Logic

Just starting out with PySpark and seeking advice on translating the following SAS code into PySpark: SAS Code: If ColA > Then Do; If ColB Not In ('B') and ColC <= 0 Then Do; New_Col = Sum(ColA, ColR, ColP); End; Else ...

Unique option preservation on customized HTML select menus - Maintain original selection

Currently, I am attempting to replicate a custom HTML select based on the example provided by W3 Schools. You can view the demo through this link: https://www.w3schools.com/howto/tryit.asp?filename=tryhow_custom_select The issue I am encountering is that ...

Is there a way to update the button's value upon clicking it?

I've hit a roadblock in my tic tac toe game project during class, and I've been struggling for the past two days to get the X's and O's to show up. The deadline for this assignment is tomorrow! Here are the task requirements: COMPSCI20 ...

Unable to expand the dropdown button collection due to the btn-group being open

Having trouble with the .open not working in Bootstrap 4.3 after adding the btn-group class to open the dropdown... I am looking for a way to load the dropdown without using JavaScript from Bootstrap. This is the directive I am trying to use: @Host ...

What is the best way to grab and retrieve a toast notification using Python's Selenium?

When the toast pop out message briefly shows for only a couple of seconds, its element is structured as: <div id class="layui-layer-content">abcde!</div> To retrieve and extract the message abcde!, what steps should be taken? ...

IE6 disrupts stored layouts

I've encountered a strange issue with my design in IE6. It loads perfectly at first, but after reloading it a couple of times, everything suddenly collapses. The strange part is that when I upload an updated version, it fixes the issue, only to break ...

The conversion of string to number is not getting displayed correctly when using console.log or document.write. Additionally, the concatenated display is also not functioning as intended

Being new to JS and HTML, this program was created to enhance my understanding of the concepts. I attempted a basic program to convert a string to a number in three different ways, but I am having trouble determining if the conversion actually took place. ...

Adding a semi-transparent layer to cover half of the canvas while still allowing the canvas to be visible

I'm struggling with overlaying a div on top of a canvas while keeping the canvas visible. Currently, I can only get the div to cover the canvas and hide it. If anyone has an example to share, that would be greatly appreciated. var canvas = document ...

A guide on incorporating a customized Google map into your website

Recently, I utilized the Google Map editing service from this site: https://developers.google.com/maps/documentation/javascript/styling This link provided me with two things: 1. A JSON code 2. The Google API link However, I am unsure about how to incorpo ...

How to calculate the logarithm of a positive number in Python and deal with the result of negative

Displayed below is an image: HI00008918.png The goal is to implement a logarithmic function (f(x) = (1/a)*log(x + 1), with the value of a = 0.01), on the image... Here is the code segment: import numpy as np import matplotlib.pyplot as plt import skimage ...

Python Creating a callback function in Matplotlib with parameters

Is it possible to pass additional parameters other than 'event' in a callback function for button presses? For example, I need to access the text of the button ('Next' in this scenario) within the callback function. How can this be achi ...

Intriguing flashing dark streak, reminiscent of a boundary within NextJS

I have recently encountered an unexpected issue with my full-width video header that worked perfectly on multiple pages before. Strangely, a thin black line has appeared around the bottom and right side of the div (.header) containing the video, even thoug ...