1

I'm trying to use send_keys to fill a field and in the same time, store this value in a variable. When I run this code, the text of the variable is not printed.

locators.py

from selenium.webdriver.common.by import By

class CreateNewContractor(object):

  FIRST_NAME = (By.ID, "id_0-first_name")

pages.py

from locators import *

class CreateNewContractor1(Page):


    def fill_contractor(self):

        email = self.find_element(*CreateNewContractor.FIRST_NAME).send_keys("Hello")
        email.text
        print email

How can I store and print the text filled in the email variable?

2
  • email.get_attribute('value') Commented Nov 10, 2016 at 16:51
  • @Andersson I tried the option above but the test passed and even so not print the text value. Commented Nov 10, 2016 at 17:09

1 Answer 1

4

The email variable would get the value None - this is what send_keys() method returns.

Instead, you can simply keep the text in a variable:

text = "hello"
self.find_element(*CreateNewContractor.FIRST_NAME).send_keys(text)
print(text)

Or, if you want to actually get the value of the input, use get_attribute() method:

elm = self.find_element(*CreateNewContractor.FIRST_NAME)
elm.send_keys("hello")
print(elm.get_attribute("value"))
Sign up to request clarification or add additional context in comments.

5 Comments

I tried all options above but the test passed and even so not print the text value.
@Rafael are you actually calling the method during the test?
@Rafael thanks, could it just be the way your runner output the standard out messages? I mean, try putting, say, print("hello") after sending keys - do you see it printed?
I realized now that nothing is printing in my console. See: take.ms/kvvx1 and my output take.ms/meM62
For print statements using pytest.. you should execute them with the flag '-s'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.