【问题标题】:Click on multiple elements using selenium on Python在 Python 上使用 selenium 单击多个元素
【发布时间】:2020-05-15 04:24:21
【问题描述】:

我想创建一个机器人,它将转发一个帐户上的所有推文加载。 首先我尝试使用这种方法:

rt = self.driver.find_elements_by_xpath("//div[@data-testid='retweet']")

for i in rt:

        time.sleep(1)
        i.click()

        """Confirm the retweet"""
        time.sleep(1)
        self.driver.find_element_by_xpath("//div[@data-testid='retweetConfirm']").click()

但它没有用,我无法在点击之前等待足够长的时间,即使 time.sleep() 很长。 所以相反,我试试这个:

for i in rt:

    z=WebDriverWait(self.driver, 10).until(EC.element_to_be_clickable((By.XPATH,"//div[@data-testid='retweet']")))
    z.click()

    #Confirm the retweet
    b=WebDriverWait(self.driver, 10).until(EC.element_to_be_clickable((By.XPATH,"//div[@data-testid='retweetConfirm']")))
    b.click()

不幸的是,这段代码只会转发第一条推文。我应该修改 EC.element_to_be_clickable((By.Xpath,"//*[@data-testid='retweet'])) (以及第二个 EC.element_to_be_clickable)中的某些内容,以便每次迭代都转到下一条推文,但我不知道是什么。

有人知道用这种方法(或其他方法)遍历我所有推文的方法吗?我一直在考虑在“rt”中获取我所有元素的绝对路径,但我不知道我是否可以只使用 Selenium 来做到这一点。我也可以使用 Twitter API,但我希望能够在其他网站上创建机器人。

谢谢

【问题讨论】:

    标签: python selenium selenium-webdriver bots


    【解决方案1】:

    好的,我想我已经找到了解决问题的方法。 对于每次点击,我都会尝试在它发生时捕获异常“ElementClickInterceptedException”;否则我点击我的转推。

    所以它给出了类似的东西:

      for i in rt:
    
            first_clique=False
            while first_clique==False:
                try:
                    i.click()
                except ElementClickInterceptedException:
                    pass
                else:
                    first_clique= True
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-14
      • 1970-01-01
      • 1970-01-01
      • 2023-02-08
      • 2018-05-26
      • 2021-03-31
      相关资源
      最近更新 更多