Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
338 views
in Technique[技术] by (71.8m points)

Why does my function returns None type even when it is a list before returning, in python?

Here is the code that I wrote using selenium to build instagram bot:

I wrote a function to return the list of people not accepting my request

@insta_method
def pending_req_list(self):

    self.goto_url(self.pending_req_url)
    WebDriverWait(self.driver, 20).until(expected_conditions.presence_of_element_located((By.XPATH, '//*[@id="react-root"]/section/main/div/article/main/section')))


    while True:
        try:
            buttn = self.driver.find_elements_by_css_selector('#react-root > section > main > div > article > main > button')
            buttn[0].click()
            time.sleep(3)
        except Exception:
            break

Everything till here was fine but then as i continued:

    name_str = self.driver.find_element_by_xpath('//*[@id="react-root"]/section/main/div/article/main/section').text
    print(name_str, type(name_str))

    name_list = name_str.split()
    print('

', name_list)
    return name_list

Sorry for indentation errors here as i couldn't rectify here. Now when printing and checking the type, everything was perfect but then when i accepted the return value by running, it returned None Type

Here is how i accepted it from another file:

all_names = bot.pending_req_list()
print(all_names)

Now it showes None type. Please assist.

NOTE: I am accessing values in another file...

The full function:

@insta_method
def pending_req_list(self):
    """
    Goes to the pending requests url and copies the users' name and forms a list
    """

    self.goto_url(self.pending_req_url)
    WebDriverWait(self.driver, 20).until(expected_conditions.presence_of_element_located((By.XPATH, '//*[@id="react-root"]/section/main/div/article/main/section')))


    while True:
        try:
            buttn = self.driver.find_elements_by_css_selector('#react-root > section > main > div > article > main > button')
            buttn[0].click()
            time.sleep(3)
        except Exception:
            break


    name_str = self.driver.find_element_by_xpath('//*[@id="react-root"]/section/main/div/article/main/section').text
    self.name_list = name_str.split()
    return self.name_list

Another file accessing:

    from instagram_bot import InstaBot
    bot = InstaBot()
    bot.login()
    all_names = bot.pending_req_list()
    print(all_names)

If you notice that i added self.name_list, i did it as i thought it would help but it didn't.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...