Python Selenium TypeError: cannot concatenate 'str' and 'WebElement' objects

Python Selenium TypeError: cannot concatenate 'str' and 'WebElement' objects

我正在使用 Python 和 Selenium Webdriver 我得到了一个文本字段的值,它是 1,000.000 我正在使用 if 语句来检查文本字段中的值是否 = 1,000.000 如果是,则通过 else 我使用 send_keys 并在文本字段中输入 1,000.000。

我收到以下错误: 类型错误:无法连接 'str' 和 'WebElement' 对象

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

我的代码片段是:

最大记录文本字段的默认值为 1,000.000

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

我需要将网络元素 (max_records_textfield) 转换为字符串变量吗? 我该怎么做?

错误消息非常清楚,您正在尝试将字符串与 WebElement 对象连接。

如果你想打印它的值使用 - max_records_textfield.get_attribute('value') (就像你在 if 条件和 if 块内的打印语句中使用的一样) -

print "max_records_textfield does not have default value 1,000.00 The value is = " + max_records_textfield.get_attribute('value')