AttributeError:“ str”对象没有属性“ text”:

时间:2019-07-18 15:25:57

标签: python selenium automation

我正在尝试编写一种方法,该方法查看列表中的所有值并确认列表中的值与期望值匹配。我的if语句中出现AttributeError。

@step('Confirm sources in the dropdown when uploading a file "{source_list}"')
def step_impl(context, source_list):

 open_list = context.browser.find_element_by_xpath("//select[@id='id_source']")
    if not open_list:
        raise ValueError('Source dropdown menu not found')
    open_list.click()
    time.sleep(3)
    source_options = context.browser.find_elements_by_xpath(
        "// *[ @ id = 'id_source']/option")
    for v in source_options:
        source = (v.get_attribute('innerHTML'))
        if source_list in source[0].text:
            print("Verified sources are in source list: ", source_list, "\n")
        else:
            raise ValueError("Source type of '%s' was not found in list" % source_list

我的功能文件中有一个示例,其中包含列表中的所有值。当我现在运行它时,我得到

  

“ AttributeError:'str'对象没有属性'text':

1 个答案:

答案 0 :(得分:0)

这是带有xpath的简化版本。

@step('Confirm sources in the dropdown when uploading a file "{source_list}"')
def step_impl(context, source_list):

    open_list = context.browser.find_element_by_xpath("//select[@id='id_source']")
    if not open_list:
        raise ValueError('Source dropdown menu not found')
        if open_list.find_element_by_xpath("./option[contains(text(),'" + source_list + "')]"):
            print("Verified sources are in source list: ", source_list, "\n")
        else:
            raise ValueError("Source type of '%s' was not found in list" % source_list