TransatlanticFoe
TransatlanticFoe

Reputation: 126

Issues with Python Selenium WebElements and .text

I'm writing a text in Selenium that gets the left-most column of a table and verifies that the strings in their cells match a list of dates I have. The code I have looks something like this:

dates = ["20130501", "20130502", "20130506", "20130507", "20130508", "20130509", "20130510", "20130513", "20130514", "20130515"]
mytable = self.driver.find_element(By.ID, "mytable")
datecells = mytable.find_elements(By.CSS_SELECTOR, "tbody td:first-child")
for date, cell in zip(dates, datecells):
    print "'{0}', '{1}'".format(date, cell.text) # For debugging
    #self.assertEqual(date, cell.text)

When the assert is left commented out, I get this printed out as a result:

'20130501', ''
"20130502', ''
'20130506', ''
'20130507', ''
'20130508', ''
'20130509', ''
'20130510', ''
'20130513', ''
'20130514', ''
'20130515', ''

The weird thing is, if I put a breakpoint on the print (Using MyEclipse with PyDev), and look at cell in the variables tab of PyDev Debug before it's output, I can see the proper text, and the code outputs as expected:

'20130501', '20130501'
'20130502', '20130502'
'20130506', '20130506'
'20130507', '20130507'
'20130508', '20130508'
'20130509', '20130509'
'20130510', '20130510'
'20130513', '20130513'
'20130514', '20130514'
'20130515', '20130515'

Is there some weird observer effect quirk of WebElement .text properties where it can only be properly evaluated by this debugger, or is there some condition I should be waiting for in order to get the proper values from the cells without having to step through?

Upvotes: 3

Views: 1180

Answers (1)

TransatlanticFoe
TransatlanticFoe

Reputation: 126

After testing a little bit with PhantomJS's WebDriver, I was able to figure out this was an issue with Firefox's WebDriver specifically, and found this previously asked question: WebElement getText() is an empty string in Firefox if element is not physically visible on the screen

So after changing the for loop to

for date, cell in zip(dates, datecells):
    self.driver.execute_script("arguments[0].scrollIntoView(true);", cell)
    print "'{0}', '{1}'".format(date, cell.text)
    self.assertEqual(date, cell.text)

in order to scroll each cell into view before getting its text, I got the expected output.

Upvotes: 4

Related Questions