Python selenium脚本在Linux上对Crontab很奇怪

问题描述 投票:0回答:1

嗨所以我有这个selenium脚本(在Raspian上使用firefox和geckodriver)基本上使用外部网站为Instagram上的任何给定用户下载活动故事:

def download_user_stories(self, user_name):
        driver = self.driver


        driver.get("https://storiesig.com/stories/"+user_name)
        time.sleep(2)



        for i in range(1,50):
            try:
                xpath = "//div[@class='jsx-1407646540 container']//article[" + str(i) + "]//div[3]//a[1]"
                print(xpath)
                link_location = driver.find_element_by_xpath(xpath)
                link = link_location.get_attribute('href')


                current_time = datetime.datetime.now()
                corrected_time = current_time.strftime("%Y-%b-%d")

                if '.mp4' not in link: 
                    extension = '.jpg'
                else:
                    extension = '.mp4' 

                location = '/Users/"my name"/desktop/'+ user_name + '/' + corrected_time + '-' + str(i) + extension

                print (location) 

                urllib.urlretrieve(link,location)

            except Exception as e:
                print("went to except")
                print (e)
                break

我最初通过终端运行它,它工作正常。然后我通过覆盆子pi上的crontab安排它,它运行,输出错误到文本文件,我得到这个:

working on "friends name" now
//div[@class='jsx-1407646540 container']//article[1]//div[3]//a[1]
/home/pi/Desktop/InstaScraper/Script/"friends name"/2019-Feb-21-1.jpg
//div[@class='jsx-1407646540 container']//article[2]//div[3]//a[1]
went to except
Message: Unable to locate element: //div[@class='jsx-1407646540 container']//article[2]//div[3]//a[1]

所以它找到了下载的第一个链接,但是虽然我确认了这个特定的用户还有更多的下载,但它找不到其余的链接。我还确认,它最终无法找到的元素是下一个下载元素的XPath。所以我很困惑为什么它没有找到一个确切的故事。更奇怪的是,即使最初这样做,它也不再通过终端运行良好。我没有得到改变或为什么它没有像在终端中那样在crontab中工作。

另一个有趣的说明是它为User_Name_List.txt中的第一个用户正确下载了所有链接,但没有为其余用户下载。 (该函数在具有用户名列表的类中的另一个函数的循环中调用)

我用Google搜索并考虑了它,我只是想弄清楚这里有什么不对。

任何帮助和解释将不胜感激。

注意 - 如果您认为问题可能出在哪里,您可以访问storiesig.com并查看各种活动故事(不是亮点)下载链接的相对XPath系统。

python selenium xpath cron geckodriver
1个回答
0
投票

从相对XPath切换到绝对XPath解决了这个问题。

© www.soinside.com 2019 - 2024. All rights reserved.