Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

Help to get dynamic value created with javascript in Python

148 views
Skip to first unread message

Luca Simonetti

unread,
Nov 29, 2015, 1:05:49 AM11/29/15
to Selenium Users
Hi everybody,

I'm getting crazy trying to find out how to get a element value from a javascript dynamic generated page:
Following is the html code for the element that I'm trying to get:

<td>
<script type="text/javascript">
<input id="t08451" type="text" readonly="" onkeypress="if(checkEnterInText(event)) { writeParameter('t08451',this.value); restartUpdate(this); };" onblur="writeParameter('t08451',this.value); restartUpdate(this);" onfocus="stopPeriodicUpdate(this);" title="" size="10" maxlength="10" value="" name="t08451">
</td>

This is my pytnon code:

from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
from selenium.webdriver.support.ui import WebDriverWait # available since 2.4.0
from selenium.webdriver.support import expected_conditions as EC # available since 2.26.0

# Create new istance of Firefox driver
binary = FirefoxBinary('/Applications/Programmi/Internet/Firefox.app/Contents/MacOS/firefox')
driver = webdriver.Firefox(firefox_binary=binary)
print ('creata istanza webdriver')

# Goto URL
driver.get('http://xxx.xxx.xxx.x/page1.htm')

# Print title
print 'Titolo pagina: '
print driver.title

# find the element that's name attribute is t08451 ovvero EVC AO04 Enjector Control
driver.implicitly_wait(10) # seconds
element = driver.find_element_by_name("t08451")           # it works same way with by ID locator
print 'element: '
print element

driver.implicitly_wait(10) # seconds

element_value = element.get_attribute('value')

print 'Value: '
print element_value


driver.quit()

and this is the output of the script:

$ python2.7 ./sel_fire.py
creata istanza webdriver
Titolo pagina:
I/O
element:
<selenium.webdriver.remote.webelement.WebElement (session="c5644dcd-5566-c942-9d00-0ce99be37f11", element="{c8a361ee-02ef-5a4c-ba9f-cf9ee34c94b5}")>
Value:

$

As you can see I receive back a blank value. I've also tried to use the driver.execute_script method with no solution, but any help will be appreciate.

Thanks in advance.
Luca

PeterJeffreyGale

unread,
Nov 29, 2015, 1:34:55 PM11/29/15
to Selenium Users
The default element value looks to be set to 'blank':

<input id="t08451" type="text" readonly="" onkeypress="if(checkEnterInText(event)) { writeParameter('t08451',this.value); restartUpdate(this); };" onblur="writeParameter('t08451',this.value); restartUpdate(this);" onfocus="stopPeriodicUpdate(this);" title="" size="10" maxlength="10" value="" name="t08451">


If you are expecting the 'value'attribute to get updated by the javascript embedded in this elelemt:

<input id="t08451" type="text" readonly="" onkeypress="if(checkEnterInText(event)) { writeParameter('t08451',this.value); restartUpdate(this); };" onblur="writeParameter('t08451',this.value); restartUpdate(this);" onfocus="stopPeriodicUpdate(this);" title="" size="10" maxlength="10" value="" name="t08451">

... it only seems to update the 'value' attribute in response to keystrokes (I think), and I can't see that your script has done anything to change the default value.

So what value are you expecting to get returned? And why?

Luca Simonetti

unread,
Nov 30, 2015, 3:37:40 AM11/30/15
to Selenium Users

First of all, thanks PeterJeffreyGale,

the element value that I'm trying to read on the browser appears as soon as I load the page. That page contains values of variables from a industrial machine and I need to record some of those values.

On Firefox as soon as I load the page the elements are displayed, so I'm expecting to read my element after raws

# Print title
print 'Titolo pagina: '
print driver.title

# find the element that's name attribute is t08451 ovvero EVC AO04 Enjector Control
element = driver.find_element_by_name("t08451")
time.sleep(0.2) # Let the page load
print 'element: '
print element

time.sleep(0.2) # Let the page load


element_value = element.get_attribute('value')

print 'Value: '
print element_value


Moreover I tried to use the Selenium IDE Firefox plugin and inside it I am able to read that value with command "verifyValue" and in fact it returns the correct value; therefore I do not understand how to to do the same with Python.
Maybe as you noticed I should try to issue a UI command like a "click" or other keystroke,  I am going to try and I will let you know. However if you or some else have some more suggestions I will appreciate.

Luca

Luca Simonetti

unread,
Dec 1, 2015, 3:03:17 AM12/1/15
to Selenium Users
Hi there,
let me public how I solved my little problem:
thanks to the suggestion of PeterJeffreyGale I added some lines of code to trigger an event on the page from where I'm trying to read data:

from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
from selenium.webdriver.firefox.firefox_profile import FirefoxProfile

from selenium.webdriver.support.ui import WebDriverWait                          # available since 2.4.0
from selenium.webdriver.support import expected_conditions as EC          # available since 2.26.0
from selenium.webdriver.common.action_chains import ActionChains        #to use action like click
from selenium.webdriver.support.ui import WebDriverWait

# Create new istance of Firefox driver
binary = FirefoxBinary('/Applications/Programmi/Internet/Firefox.app/Contents/MacOS/firefox')
driver = webdriver.Firefox(firefox_binary=binary)

# Goto URL
driver.get('http://xxx.xxx.xxx.xxx/page1.htm')                                                # IP addr is intentionally masqueraded

#find the element that's name attribute is t08451

element = driver.find_element_by_name("t08451")
time.sleep(0.2)                                                                                               # Wait 2 milliseconds

Hover = ActionChains(driver).move_to_element(element)                            #
Hover.click()                                                                                                   # these three lines make the magic to trigger a click on my element
Hover.perform()                                                                                              #
element.click()                                                                                                #


time.sleep(0.5)                                                                                                # Wait 0.5 seconds


element_value = element.get_attribute('value')

print 'Value V: '
print element_value

driver.quit()

and below you can see the output of the script containing my valeu from the page:
$ python2.7 ./sel_fire.py
Value V:
50.0
$

Once again thanks to PeterJeffreyGale
Reply all
Reply to author
Forward
0 new messages