简体   繁体   中英

Selenium + Python: Store text in a variable and print

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?

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"))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM