Search code examples
pythonseleniumwebdriver

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.

Here is my code.

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

But I am getting this error

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

which gives me the following error:

Exception: must be str, not int

It seems like I am missing something very small and need help figuring it out (still new to python). Any help is appreciated.


Solution

  • 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 (singular since it's only one)

    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.

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

    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.


    As to the errors,

    Exception: 'str' object has no attribute 'text'

    row_split = rows.strip('NEW')
    mylist.append(row_split.text)
    

    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.

    Exception: must be str, not int

    rows = self.driver.find_element_by_id("scopes-" + cardname + "" + i + "").text
    

    You fixed this one already. It's complaining that i is an int and not a string. str(i) fixes that.