【发布时间】:2019-07-18 15:25:57
【问题描述】:
我正在尝试编写一个查看列表中所有值并确认列表中的值与预期值匹配的方法。我的 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':
【问题讨论】:
-
您是否尝试在 source_options 中打印结果?无论如何,如果 source_list 是 if 语句中的列表,你应该做相反的事情,如果 source_list 在列表中而不是相反
-
source = (v.get_attribute('innerHTML'))这会将innerHTML作为字符串存储在source中,当您尝试source[0].text时,脚本会在您尝试.text方法时抛出错误。你应该试试if source_list in source:。我觉得你应该用 xpath 做这个检查,而不是做一个循环。 -
我一开始尝试了这个,我的 raise ValueError 被触发,即使该值在示例中并且也在 xpath 中。在这一行之后..... source = (v.get_attribute('innerHTML')) 我会添加 print(source)... 并打印预期值
-
你的source_list是字符串还是列表你能确认一下
标签: python selenium automation