[英]How do I click on the dynamic Twitter follow button with selenium and python?
I am trying to click on twitter's follow button, but it is dynamic and has attributes, which I don't find much about on the selenium documentation.This is the html of the button:我正在尝试单击 twitter 的关注按钮,但它是动态的并且具有属性,我在 selenium 文档中找不到太多相关信息。这是按钮的 html:
This is currently my follow function:这是我目前关注的function:
def follow(driver, username):
driver.get('https://twitter.com/' + username)
wait = WebDriverWait(driver, 10)
follow = wait.until(EC.element_to_be_clickable((By.CLASS_NAME, 'follow-button')))
follow.click()
Try the following Xpath to click on the Follow
试试下面的Xpath点击Follow
wait = WebDriverWait(driver, 10)
follow = wait.until(EC.element_to_be_clickable((By.XPATH, '//span[text()="Follow"]')))
follow.click()
OR或者
wait = WebDriverWait(driver, 10)
follow = wait.until(EC.element_to_be_clickable((By.XPATH, '//span[contains(.,"Follow")]')))
follow.click()
You are getting TabError: inconsistent use of tabs and spaces in indentation
because follow.click() was not inline in your initial code.Try now.您收到TabError: inconsistent use of tabs and spaces in indentation
因为 follow.click() 未在您的初始代码中内联。立即尝试。
def follow(driver, username):
driver.get('https://twitter.com/' + username)
wait = WebDriverWait(driver, 10)
follow = wait.until(EC.element_to_be_clickable((By.XPATH, '//span[text()="Follow"]')))
follow.click()
try this xpath试试这个 xpath
(//span[text()='Follow']/ancestor::div[@role='button'])[1]
Index [1]
will always pick the first Follow button, in case if there are multiple Follow buttons.如果有多个关注按钮,索引[1]
将始终选择第一个关注按钮。
You could try a generic XPath selector that only looks for the presence of 'Follow' text and does not care about the enclosing tag:您可以尝试一个通用的 XPath 选择器,它只查找“关注”文本的存在而不关心封闭标签:
//*[contains(text(), 'Follow')]
Or或者
//*[text()='Follow']
If there are multiple instances of this button, you can try:如果此按钮有多个实例,您可以尝试:
Driver.FindElement(By.XPath("//*[text()='Follow']")[0].Click();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.