![](/img/trans.png)
[英]Why isn't python's selenium button.click() working on a twitter follow button?
[英]How do I click on the dynamic Twitter follow button with selenium and python?
我正在尝试单击 twitter 的关注按钮,但它是动态的并且具有属性,我在 selenium 文档中找不到太多相关信息。这是按钮的 html:
这是我目前关注的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()
试试下面的Xpath点击Follow
wait = WebDriverWait(driver, 10)
follow = wait.until(EC.element_to_be_clickable((By.XPATH, '//span[text()="Follow"]')))
follow.click()
或者
wait = WebDriverWait(driver, 10)
follow = wait.until(EC.element_to_be_clickable((By.XPATH, '//span[contains(.,"Follow")]')))
follow.click()
您收到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()
试试这个 xpath
(//span[text()='Follow']/ancestor::div[@role='button'])[1]
如果有多个关注按钮,索引[1]
将始终选择第一个关注按钮。
您可以尝试一个通用的 XPath 选择器,它只查找“关注”文本的存在而不关心封闭标签:
//*[contains(text(), 'Follow')]
或者
//*[text()='Follow']
如果此按钮有多个实例,您可以尝试:
Driver.FindElement(By.XPath("//*[text()='Follow']")[0].Click();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.