1

I am using Python with Selenium Webdriver I have got the value of a textfield which is 1,000.000 I am using an if statement to check if the value from the textfield = 1,000.000 If Yes then pass else i use send_keys and enter 1,000.000 into the text field.

I am getting the following error: TypeError: cannot concatenate 'str' and 'WebElement' objects

Error
Traceback (most recent call last):
File "C:\Webdriver\reverted to backup\ClearCore 501\TestCases\DataPreviewsPage_TestCase.py", line 93, in test_add_DataPreviews
dp.is_maxrecords_1000_displayed()
File "C:\Webdriver\reverted to backup\ClearCore 501\Pages\data_previews.py", line 76, in is_maxrecords_1000_displayed
print "max_records_textfield does not have default value 1,000.00 The value  is = " + max_records_textfield
TypeError: cannot concatenate 'str' and 'WebElement' objects

My code snippet is:

max records textfield has the value 1,000.000 as default

    def is_maxrecords_1000_displayed(self):
        max_records_textfield = self.driver.find_element(*MainPageLocators.datapreviews_maxrecords_textfield2)
        print "max_records_textfield = ",  max_records_textfield.get_attribute('value')
        if max_records_textfield.get_attribute('value') == "1,000.000":
            print "max_records_textfield" + max_records_textfield.get_attribute('value')
        else:
            print "max_records_textfield does not have default value 1,000.00 The value is = " + max_records_textfield
            print "Going to enter 1,000.00 into the textfield"
            max_records_textfield.send_keys("1,000.00")

Do i need to cast the webelement (max_records_textfield) to a string variable? How would i do this?

1
  • Use - max_records_textfield.get_attribute('value') , instead of the WebElement - print "max_records_textfield does not have default value 1,000.00 The value is = " + max_records_textfield.get_attribute('value') Commented Jul 24, 2015 at 14:22

1 Answer 1

1

The Error message is very clear, You are trying to concatenate string with a WebElement object.

If you want to print its value use - max_records_textfield.get_attribute('value') (Just as you used in the if condition and the print statement inside the if block) -

print "max_records_textfield does not have default value 1,000.00 The value is = " + max_records_textfield.get_attribute('value')
Sign up to request clarification or add additional context in comments.

1 Comment

Ah i forgot the .get_attribute('value') at the end. It's working now. Thanks.

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.