如何点击Instagram上的关注按钮?

时间:2020-04-08 07:43:27

标签: python selenium

我正在尝试使用Python Selenium在Instagram上单击关注按钮 https://www.instagram.com/luvly_zuby/?hl=en

我已经尝试了下面的代码,但是没有用。

#click follow
    follow = driver.find_element_by_partial_link_text("Follo")
ActionChains(driver).move_to_element(follow).click().perform()

3 个答案:

答案 0 :(得分:1)

您可以使用简单的硒单击元素,方法是在xpath中使用其文本查找元素,然后对元素进行显式等待。
您可以这样做:

follow_button = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "//button[text()='Follow']")))
follow_button.click()

您需要添加以下导入:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

答案 1 :(得分:0)

这应该为您工作。将WebElement传递给click(element)方法。

from selenium import webdriver
driver = webdriver.Chrome()
driver.get('testUrl')
follow = driver.find_element(By.XPATH, '//button[contains(text(),'Follow')]')
webdriver.ActionChains(driver).move_to_element(follow).click(follow).perform()

答案 2 :(得分:0)

尝试使用此CSS选择器a.BY3EC > button查找元素,然后使用.element_to_be_clickable等待:

follow = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'a.BY3EC > button')))
follow.click()

正在导入:

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC