【问题标题】:Use selenium to click buttons on Instagram使用 selenium 点击 Instagram 上的按钮
【发布时间】:2021-02-10 19:22:12
【问题描述】:

几个月前才开始学习 Python,并尝试使用 selenium 构建 iG 机器人。

我很难让代码点击过去的登录按钮和“不是现在”按钮。

from time import sleep
from selenium import webdriver

browser = webdriver.Firefox()
browser.get("https://www.instagram.com/")

sleep(1)

#Cookie popup
accept_cookies = browser.find_element_by_xpath("//button[text()='Accept']").click()

username_input = browser.find_element_by_css_selector("input[name='username']")
password_input = browser.find_element_by_css_selector("input[name='password']")

username_input.send_keys("")
password_input.send_keys("")

#xpath on button
login_link = browser.find_element_by_xpath("/html/body/div[1]/section/main/article/div[2]/div[1]/div/form/div/div[3]/button").click()

#Notification popup

turnoff_notif = browser.find_element_by_xpath("//button[text()='Not Now']").click()

我的"//button[text()='Accept']").click() 推送过去了接受 cookie 弹出窗口,但为什么相同的命名法不适用于在以下弹出通知中按“不立即”????

附言主页上最初的“登录”按钮也是如此,所以我不得不发布直接的 xpath URL,但如果可能的话我想避免这种情况。

任何帮助将不胜感激。

【问题讨论】:

    标签: python selenium xpath instagram


    【解决方案1】:

    您需要先加载页面。我建议您在每次与页面交互时设置一些睡眠:

    login_link = browser.find_element_by_xpath("/html/body/div[1]/section/main/article/div[2]/div[1]/div/form/div/div[3]/button").click()
    
    sleep(7)
    
    turnoff_notif = browser.find_element_by_xpath("//button[text()='Not Now']").click()
    

    另一种选择是使用 Selenium 隐式/显式等待。请参阅here 了解更多信息。

    【讨论】:

    • 老兄,谢谢!就是这样......有一个虚拟的兄弟?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-24
    • 2022-01-01
    • 1970-01-01
    • 2014-02-16
    相关资源
    最近更新 更多