Alex
Alex

Reputation: 44265

How to get the 'value' of a html element in python-selenium?

In a html page I have the following snippet:

<div class="jupyter-widgets widget-hbox widget-text">
    <div class="widget-label" style="display: block;">Walltime:</div>
    <input type="text" class="form-control" placeholder="">
</div>

in which the input element renders, like expected, as an input field. However, this input field has some value in it.

Using the inspector of the browser it is possible to select that element and use the following expression in the console to get that value:

$0.value

However, how to do this in python-selenium?

When I have the input element in a variable named 'input' I have tried the following:

driver.execute_script("arguments[0].value;", element)

which does not return the value in that input box, but None instead. How to do it correctly to retrieve the value from that 'input' box?

Upvotes: 3

Views: 987

Answers (2)

Moshe Slavin
Moshe Slavin

Reputation: 5214

With get_attribute you can get any attribute of an element! in your case:

element.get_attribute("value")

As @Guy commented text is not part of the html so don't use:

element.text

Upvotes: 3

Guy
Guy

Reputation: 50809

You need to return the value

driver.execute_script('return arguments[0].value;', element)

You can also use get_attribute()

element.get_attribute('value')

Upvotes: 4

Related Questions