例外:'str' 对象没有属性 'text' [英] Exception: 'str' object has no attribute 'text'
问题描述
我试图遍历页面上的元素,然后从元素中获取文本.在获取文本时,我试图删除一个特定的单词新",然后将其添加到列表中.
I am trying to loop through elements on the page and then get the text from the element. While getting the text, I am trying to strip a specific word "NEW" and then add it in the list.
这是我的代码.
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'
我尝试了很多不同的方法,但都没有奏效.例如,
I have tried a bunch of different ways but none of them are working. For example,
rows = self.driver.find_element_by_id("scopes-" + cardname + "" + i + "").text
这给了我以下错误:
Exception: must be str, not int
似乎我遗漏了一些非常小的东西,需要帮助来解决它(对 python 来说仍然是新手).任何帮助表示赞赏.
It seems like I am missing something very small and need help figuring it out (still new to python). Any help is appreciated.
推荐答案
您希望从一开始就做好的一件事是使用描述性变量名称.这将帮助您和其他任何必须阅读您的代码的人了解您正在尝试做什么.
One thing you want to get right from the very beginning is to use descriptive variable names. This will help you and anyone else that has to read your code understand what you are trying to do.
我根据我对它们的最佳猜测更改了一些变量名称.显然,如果我猜错了,可以随意将它们更改为任何有意义的内容.
I changed a few variable names using my best guess at what they were. Obviously feel free to change them to whatever makes sense if I guessed wrong.
mylist
-> labelList
allrows
-> rowLabels
rows
-> rowLabel
(单数,因为它只有一个)
rows
-> rowLabel
(singular since it's only one)
删除了一些额外"的变量.如果我不打算再次使用它,我倾向于不创建一个新变量.例如,count
只包含 len(allrows)
.您可以删除 count
并在 for
循环中的唯一显示位置使用 len(allrows)
.
Removed some "extra" variables. I tend to not create a new variable if I'm not going to use it again. For example, count
just holds len(allrows)
. You can remove count
and just use len(allrows)
in the only place it shows up, in the for
loop.
删除了定位器中的一些额外 ""
,例如...cardname + "" + str(i) + ""
.+ "" +
在这里没有任何作用,因为您只是连接了一个空字符串 ""
所以我删除了它们.
Removed some extra ""
s you had in your locator, e.g. ...cardname + "" + str(i) + ""
. The + "" +
doesn't do anything here since you are just concatenating an empty string ""
so I removed those.
rows.strip()
将删除字符串.如果子字符串不存在,它只返回整个字符串.既然如此,您就不需要 if-else
.
rows.strip()
will remove the string if the substring exists. If the substring doesn't exist, it just returns the entire string. Since this is the case, you don't need an if-else
.
下面的代码是我将如何编写.
The below code is how I would write this.
def test(self, cardname):
labelList = []
allrows = self.driver.find_elements_by_xpath("//*[contains(@id, 'scopes-pending-')]")
for i in range(len(allrows)):
rowLabel = self.driver.find_element_by_id("scopes-" + cardname + str(i)).text
labelList.append(rowLabel.strip('NEW'))
return labelList
警告...我不是 Python 程序员,所以可能有更多的优化和方法可以使这段代码比我建议的更像 Python.
Caveat... I'm not a python programmer so there may be some more optimizations and ways to make this code more python-y than I have suggested.
关于错误,
异常:'str' 对象没有属性 'text'
Exception: 'str' object has no attribute 'text'
row_split = rows.strip('NEW')
mylist.append(row_split.text)
在上面几行中,row_split
是一个字符串,而您正在执行导致错误的
..text
可用于 Web 元素.
In the above lines, row_split
is a string and you are doing <string>.text
which causes the error. .text
can be used on a Web Element.
例外:必须是 str,而不是 int
Exception: must be str, not int
rows = self.driver.find_element_by_id("scopes-" + cardname + "" + i + "").text
你已经解决了这个问题.它抱怨 i
是一个 int
而不是字符串.str(i)
解决了这个问题.
You fixed this one already. It's complaining that i
is an int
and not a string. str(i)
fixes that.
这篇关于例外:'str' 对象没有属性 'text'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!