【发布时间】:2017-08-08 14:56:58
【问题描述】:
我正在尝试遍历页面上的元素,然后从元素中获取文本。在获取文本时,我试图删除一个特定的单词“NEW”,然后将其添加到列表中。
这是我的代码。
def test(self, cardname):
mylist = []
allrows = self.driver.find_elements_by_xpath("//*[contains(@id, 'scopes-pending-')]")
count = len(allrows)
for i in range(count):
rows = self.driver.find_element_by_id("scopes-" + cardname + "" + str(i) + "").text
if "NEW" in rows:
row_split = rows.strip('NEW')
mylist.append(row_split.text)
else:
mylist.append(rows.text)
return mylist
但是我收到了这个错误
Exception: 'str' object has no attribute 'text'
我尝试了很多不同的方法,但都没有奏效。例如,
rows = self.driver.find_element_by_id("scopes-" + cardname + "" + i + "").text
这给了我以下错误:
Exception: must be str, not int
似乎我遗漏了一些非常小的东西,需要帮助来解决(对于 python 来说还是新手)。任何帮助表示赞赏。
【问题讨论】: