这个脚本为什么不在Instagram上跟随?
import time
from time import sleep
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
browser = webdriver.Chrome('./chromedriver')
"GO INSTAGRAM PAGE FOR LOGIN"
browser.get('https://www.instagram.com/accounts/login/?hl=it')
sleep(2)
"ID AND PASSWORD"
elem = browser.find_element_by_name("username").send_keys('id')
elem = browser.find_element_by_name("password").send_keys('password')
"CLICK BUTTON AND OPEN INSTAGRAM"
good_elem = browser.find_element_by_xpath('//*[@id="react-root"]/section/main/div/article/div/div[1]/div/form/span/button').click()
sleep(2)
browser.get("https://www.instagram.com")
"GO TO PAGE FOR FOLLOW"
browser.get("https://www.instagram.com/test/")
time.sleep(30)
"FOLLOW (DON'T WORK)"
follow = browser.find_element_by_xpath('//*[@id="react-root"]/section/main/div/header/section/div[1]/span/span[1]/button').click()
答案 0 :(得分:0)
点击某个元素时,您需要使用ActionChains
。因此,您的代码将如下所示:
good_elem = browser.find_element_by_xpath('//*[@id="react-root"]/section/main/div/article/div/div[1]/div/form/span/button')
ActionChains(browser).move_to_element(good_elem).click().perform()
对于“跟随”按钮,代码将如下所示:
follow = browser.find_element_by_xpath('//*[@id="react-root"]/section/main/div/header/section/div[1]/span/span[1]/button')
ActionChains(browser).move_to_element(follow).click().perform()