【问题标题】:Python Selenium unintentional click problemPython Selenium 无意识点击问题
【发布时间】:2020-12-03 09:20:13
【问题描述】:

我正在尝试使用 python selenium 来吸引追随者。但有时python会自行点击。 我想做一个没有错误的程序。我尝试尝试过“try catch”构造,但没有奏效。这是我的代码:

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 解决了我的问题。在页面底部您可以看到解决方案。

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

【问题讨论】:

  • 不清楚“它自己点击”是什么意思。它点击什么?你所说的“本身”是什么意思?
  • 我的意思是,在获取用户列表时,点击任何用户会转到他们的页面并完成
  • @RohanShah 感谢您的精彩回答。我现在要检查一下。
  • @RohanShah 它正在工作!你真的拯救了我的一天。我很感激。

标签: python selenium instagram


【解决方案1】:

我在滚动弹出窗口时遇到了同样的问题。发生的情况是您的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(),这将使您返回页面并返回弹出窗口,并且您的循环将继续进行,就好像什么都没发生一样。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-10-16
    • 1970-01-01
    • 2022-01-22
    • 2022-01-23
    • 1970-01-01
    • 2017-12-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多