Pyhton Selenium意外单击问题

2024-06-01 04:36:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我正试图通过python selenium获得追随者。但有时python会自己点击。 我想做一个没有错误的程序。我尝试了我尝试了“尝试捕捉”的构造,但没有成功。这是我的密码:

def getFollowers(self):
        try:
            self.browser.get(f"https://www.instagram.com/{self.username}")
            time.sleep(2)
            followers=self.browser.find_element_by_xpath("//*[@id='react-root']/section/main/div/header/section/ul/li[2]/a").click()
            time.sleep(2)
            dialog=self.browser.find_element_by_xpath("/html/body/div[5]/div/div/div[2]")
            followerCount=len(dialog.find_elements_by_tag_name("li"))
            print(f"first count:{followerCount}")
            action=webdriver.ActionChains(self.browser)
//*******************************************Probly my problem is here****************************************
            while True:
                dialog.click()
                action.key_down(Keys.SPACE).key_up(Keys.SPACE).perform()
                time.sleep(3)
                newCount=len(dialog.find_elements_by_tag_name("li"))
                if followerCount!=newCount or newCount==24:
                    print(f"New count:{newCount}")
                    time.sleep(3)
                    followerCount=newCount
                else:
                    break
//**********************************************************************************************************
            followers=dialog.find_elements_by_tag_name("li")
            followersList=[]
            for user in followers:
                link=user.find_element_by_css_selector("a").get_attribute("href")
                # print(link)
                followersList.append(link)
            with open("followers.txt","w",encoding="UTF-8") as file:
                for item in followersList:
                    file.write(item+"\n")
            time.sleep(5)
        except:
            pass

我也有def getfollowing,它可以完美地工作。如果你愿意,我也可以给你看。但它们几乎是一样的

编辑:@RohanShah解决了我的问题。在页面底部,您可以看到解决方案

编辑:我是新来的,这就是为什么有时候我的问题可能毫无意义。但请不要降低我的分数。我不会再接受我的问题了。请增加我的分数


Tags: selfdivbrowserbytimetagsleepli
1条回答
网友
1楼 · 发布于 2024-06-01 04:36:39

我在滚动弹出窗口时遇到了完全相同的问题。发生的是你的dialog.click(),当你试图将你的键集中在弹出窗口上时,偶尔会点击一个用户并加载他们的配置文件。当弹出窗口不再出现在屏幕上时,脚本崩溃

在对解决这个问题进行了大量研究之后,我注意到它只发生在长用户名上。无论如何,我实现了一个简单的hack来解决这个问题

  1. 首先,我们得到标准卷轴的url。打开并滚动弹出窗口时,这是我们所在的url。 https://www.instagram.com/some_username/followers/

2.现在我创建了一个函数来保存打开弹出窗口的代码。这将非常有用,因此可以将必要的代码捕获到函数中。(我身上没有类名或xpath,所以请自己定制函数)

    def openPopup():
        self.browser.get(f"https://www.instagram.com/{self.username}")
        global popup # we will need to access this variable outside of the function
        popup = driver.find_element_by_class_name('popupClass') #you don't have to use class_name
        popup.click()
  1. 现在,我们必须告诉我们的while loop在Selenium意外单击用户时不要扫描。我们将使用步骤1中的URL。请确保在循环的顶部插入以下if语句,以便在尝试访问弹出窗口之前,如果出现中断,它将首先处理它
    while True:  
      check_url = self.browser.current_url #returns string with current_url

      if check_url != 'https://www.instagram.com/some_username/followers/':
        #if this code is executed, this means there has been an accidental click
        openPopup() #this will bring back to the page and reopen popup
    
     #the rest of your code
      popup.click() # variable from our function
      action.key_down(Keys.SPACE).key_up(Keys.SPACE).perform()
      time.sleep(3)
      newCount=len(dialog.find_elements_by_tag_name("li"))           
      if followerCount!=newCount or newCount==24:
        print(f"New count:{newCount}")
        time.sleep(3)
        followerCount=newCount
      else:
        break
      check_url = self.browser.current_url #we must recheck the current_url every time the loop runs to see if there has been a misclick

现在,每当循环检测到URL不再是弹出窗口之一时,它将自动调用openPopup(),这将使您返回页面并返回弹出窗口,并且循环将继续,就像什么都没有发生一样

相关问题 更多 >