[英]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.