简体   繁体   English

在 Python 上使用 selenium 单击多个元素

[英]Click on multiple elements using selenium on Python

I would like to create a bot which will retweet all tweet load on an account.我想创建一个机器人,它将转发帐户上的所有推文加载。 First I try to use this method:首先我尝试使用这种方法:

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()

But it didn't work, I wasn't able to wait enought before clicking, even with a high time.sleep().但它没有用,我无法在点击之前等待足够的时间,即使 time.sleep() 很高。 So instead, I try this:所以相反,我试试这个:

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()

Unfortunately this code will only retweet the first tweet.不幸的是,这段代码只会转发第一条推文。 I should modify something in EC.element_to_be_clickable((By.Xpath,"//*[@data-testid='retweet'])) (and in the second EC.element_to_be_clickable) to go to the next tweet for each iteration but I don't know what.我应该将 EC.element_to_be_clickable((By.Xpath,"//*[@data-testid='retweet'])) 中的某些内容(以及第二个 EC.element_to_be_clickable)修改为 go 到每次迭代的下一条推文,但我不知道是什么。

Does anybody know a way to iterate through all my tweets with this method (or another)?有人知道用这种方法(或另一种方法)遍历我所有推文的方法吗? I have been thinking about getting the absolute path of all my element in "rt" but I don't know if I can do this using only Selenium.我一直在考虑在“rt”中获取我所有元素的绝对路径,但我不知道我是否可以仅使用 Selenium 来做到这一点。 I could also use Twitter API but I want to be able to create a bot on others websites.我也可以使用 Twitter API 但我希望能够在其他网站上创建机器人。

Thank you谢谢

Ok, I think I have found a solution to my issue.好的,我想我已经找到了解决问题的方法。 For each click, I try to catch the exception "ElementClickInterceptedException" when it happens;对于每次点击,我都会尝试在它发生时捕获异常“ElementClickInterceptedException”; otherwise I click on my retweet.否则我点击我的转推。

So it gaves something like that:所以它给出了类似的东西:

  for i in rt:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 同时点击多个元素 selenium Python - Click multiple elements at the same time selenium Python 当有多个相同的 class 并且没有使用 Python 的 id 元素时,使用 selenium 创建点击事件 - Create a click event using selenium when there are multiple same class and no id elements using Python 使用 Selenium 和 Python 访问网页时,Selenium 无法单击元素 - Selenium unable to click on elements while accessing a webpage using Selenium and Python 如何鼠标 Hover 多个元素一个接一个,然后使用 Selenium 和 Python 在 OrangeHRM 网站中单击一个元素 - How to Mouse Hover multiple elements one by one and then click on an element within OrangeHRM website using Selenium and Python 如何点击<a>元素</a>列表<ul><a>和</a><li><a>元素与硒使用python?</a> - How to click on the list of the <a> elements in an <ul> and <li> elements with selenium using python? Python Selenium 无法点击元素 - Python Selenium not able to click on elements Python Selenium-无法使用XPATH单击滑块元素 - Python Selenium - Can't click on slider elements using XPATH 如何单击使用 selenium python 弹出阻止的元素? - How to click on elements blocked by pop up using selenium python? 使用Python Selenium Chrome驱动程序在Zomato上单击元素 - Click elements on Zomato using Python Selenium Chrome Driver 在Python中使用Selenium单击具有相同类名的所有元素 - Using Selenium in Python to click through all elements with the same class name
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM