1
votes

I am trying to get value from a dynamic loading table, when I click on the TD element it works all ok. But when I am tryin to get the text from same TD , it is returning empity string. I have tried XPATH, CSS_Selector

Below is the UI look, I want to get the amount enter image description here

Below is the HTML Snap enter image description here

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

Working Fine

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

returns empity string

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

Timeout Exception

1
Try driver.find_element_by_id('amount').get_attribute('value') - Andersson

1 Answers

1
votes

Solution

As @Andersson already mentioned, you can use get_attribute('value') to extract the text from input field. Let's create the following example:

driver.get('http://demo.automationtesting.in/Register.html') # get sample page
time.sleep(3) # pause to wait until page loads

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

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

Analysis

If we go to the dev tools:

img

we will see the following HTML for 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}$">

it does not have any text in the tag, that's why calling

print(input_el.text)

will print nothing. If the HTML would look like:

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

then

print(label_el.text)

would print the text inside label tag - Email address*.