【问题标题】:Python Selenium Automatic Follow InstagramPython Selenium 自动关注 Instagram
【发布时间】:2018-08-19 10:58:55
【问题描述】:

为什么这个脚本没有在 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()

【问题讨论】:

  • 老兄,我认为您用于跟随按钮的 xpath 是错误的,您能否再次检查是否正确复制它,因为我得到 //*[@id="react-root"]/ section/main/div/header/section/div[1]/a/button
  • 不要工作,伙计。
  • 奇怪..因为它对我有用..我在 instagram 上没有帐户,但它确实将我带到了登录页面...请试试这个...options = webdriver.Chrome (chrome 驱动程序 exe 的路径) options.get('instagram.com/test') options.find_element_by_xpath('//*[@id="react-root"]/section/main/div/header/section/div[1] /a/button').click()
  • 或者我认为登录后xpath可能会改变..所以尝试使用类名.....options.find_element_by_class_name('BY3EC').click()
  • 谢谢你现在的工作。

标签: python selenium instagram


【解决方案1】:

点击元素时需要使用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()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多