小能豆

Python - 函数仅在单独文件中有效

python

def actions():
    while True:
        try:
            print("Entering loop 1")

            # Press the Page Down key 3 times
            PgDown()
            time.sleep(1)

            # Clicks on video
            Click(612, 702)

            # Get the URL of the video
            time.sleep(5)
            video_url = browser.current_url
            print(f"Video URL: {video_url}")

            # Saves username for credit
            caption, account_name, hashtags = CapHashCredit(browser)

            # Downloads video
            DownloadVideo(video_url)
            time.sleep(3)
            PostVideo(caption, account_name, hashtags)
            time.sleep(3)

            while True:

                print("Entering loop 2")

                # Clicks off video
                time.sleep(3)
                browser.back()
                print("Back a page")
                time.sleep(3)

                # Press the Page Down key 3 times
                PgDown()

                # Click on video again
                time.sleep(15)
                Click(612, 702)
                time.sleep(5)

                # If URL has changed, download the new video
                new_video_url = browser.current_url
                if new_video_url != video_url:
                    print("New video URL: ", new_video_url)
                    video_url = new_video_url

                    caption, account_name, hashtags = CapHashCredit()

                    DownloadVideo(video_url)
                    time.sleep(3)
                    PostVideo(caption, account_name, hashtags)
                    time.sleep(3)

                    browser.back()
                    print("Back a page")
                    time.sleep(3)
                    break  # Exit the inner while loop and restart the outer loop

        except Exception as err:
            print("An error occurred:", err)


def CapHashCredit():
    try:
        video_url = browser.current_url
        print("Creating caption for:", video_url)
        time.sleep(5)
        # Copy account name
        account_name = browser.find_element(By.XPATH, '/html/body/div[2]/div/div/div[1]/div/div/div/div[1]/section/main/div[1]/div[1]/article/div/div[2]/div/div[1]/div/header/div[2]/div[1]/div[1]/div/div/span/a')

        # Copy hashtags
        caption_element = browser.find_element_by_xpath('/html/body/div[2]/div/div/div[1]/div/div/div/div[1]/section/main/div[1]/div/article/div/div[2]/div/div[2]/div[1]/ul/div/li/div/div/div[2]/div[1]/h1')
        caption = caption_element.text
        hashtag_elements = caption_element.find_elements_by_xpath('.//a[contains(@href, "/explore/tags/")]')
        hashtags = ' '.join([hashtag.text for hashtag in hashtag_elements])

        # Remove hashtags from caption
        if hashtags:
            caption = caption.replace(hashtags, '')

        print("Caption:", caption.strip())
        if hashtags:
            print("Hashtags:", hashtags)
        print("Account name:", account_name.text)

    except Exception as e:
        print(f"Can't create caption: {e}")
        browser.quit()

我试图基本上从 Instagram 帖子中获取帐户名称、标题和主题标签,我试图在另一个函数中使用调用此函数,当caption, account_name, hashtags = CapHashCredit(browser)我这样做时它说我找不到 xpath(使用 selenium 库), 但是,如果我将这段代码放在一个单独的 .py 文件中并运行它,它将工作得很好并打印出 3 个变量,而且我 100% 确定 xpath 存在,因为它打印出正确的链接第 4 行。有谁知道为什么这不起作用?```

我试过在一个单独的文件中运行 CapHashCredit,它工作正常。


阅读 82

收藏
2023-05-21

共1个答案

小能豆

根据你提供的代码,有一些问题可能导致在 actions() 函数中调用 CapHashCredit() 函数时出现问题。

  1. actions() 函数中调用 CapHashCredit() 函数时,你没有将 browser 作为参数传递给 CapHashCredit()。因此,在 CapHashCredit() 函数中无法访问 browser 对象。你可以将 browser 作为参数传递给 CapHashCredit(),如下所示:
caption, account_name, hashtags = CapHashCredit(browser)
  1. CapHashCredit() 函数中,你使用了 XPath 来查找元素。但是,你没有使用正确的方法来查找元素,例如使用 find_element()find_elements()。你可以尝试使用以下方法来查找元素:
account_name = browser.find_element(By.XPATH, '/html/body/div[2]/div/div/div[1]/div/div/div/div[1]/section/main/div[1]/div[1]/article/div/div[2]/div/div[1]/div/header/div[2]/div[1]/div[1]/div/div/span/a')
caption_element = browser.find_element(By.XPATH, '/html/body/div[2]/div/div/div[1]/div/div/div/div[1]/section/main/div[1]/div/article/div/div[2]/div/div[2]/div[1]/ul/div/li/div/div/div[2]/div[1]/h1')
hashtag_elements = caption_element.find_elements(By.XPATH, './/a[contains(@href, "/explore/tags/")]')

确保你已经导入了正确的 Selenium 模块,例如:

from selenium.webdriver.common.by import By

通过这些更改,你应该能够在 actions() 函数中成功调用 CapHashCredit() 函数,并获取到正确的结果。

2023-05-21