Home > Mobile >  Converting a list (WebElement a) to text and appending to an array python
Converting a list (WebElement a) to text and appending to an array python

Time:02-25

Question is straightforward, there are java tutorials but I cant apply them to python since I am a noob. I have a piece of code which should scrape a list of a links, but only their text as like numbers of pages - on gsmarena, like 1,2,3,4 and then put them into array which gets sorted and the biggest number will bo chosen as the amount of iterations a certain function will do, but i get an error: AttributeError: 'list' object has no attribute 'text', and i cannot find a way to convert this list into text, because doing it one by one doesnt work since it has to be in a for loop.

def scrape_brands(self, scraped_brands=None):
        print("starting to scrape")
        for model in self.find_element_by_xpath("/html/body/div[2]/div[1]/div[2]/div/div[2]").find_elements_by_tag_name("tr"):
            for values in model.find_elements_by_tag_name("td"):
                print(values.text)
                values.click()
                self.implicitly_wait(2)
                print("klikam")
                self.scrape_models()
                self.implicitly_wait(2)
                self.back_to_brands()

    def scrape_models(self, scraped_models=None):
         for device in self.find_element_by_class_name("makers").find_elements_by_tag_name("a"):
            for devices in device.find_elements_by_tag_name("strong"):
                print(devices.text)
                self.implicitly_wait(1)
                print("doprintene")
                for i in self.find_element_by_class_name("nav-pages").find_elements_by_tag_name("a"):
                    x = self.find_element_by_class_name("nav-pages").find_elements_by_tag_name("a").text
                    x.append
                    x.sort()
                    print(x[-1])
                self.next_page()

CodePudding user response:

x = self.find_element_by_class_name("nav-pages").find_elements_by_tag_name("a").text
#                                                                              ^^^^

Seems like your find_elements_by_tag_name method is returning a list rather than a single element you expect. Either take first element:

x = self.find_element_by_class_name("nav-pages").find_elements_by_tag_name("a")[0].text
#                                                                              ^^^

Or iterate through this list.

  • Related