Python 使用selenium单击Instagram上的按钮

Python 使用selenium单击Instagram上的按钮,python,selenium,xpath,instagram,Python,Selenium,Xpath,Instagram,几个月前才开始学习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

几个月前才开始学习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()
My
“//按钮[text()='Accept']”。单击()
会推过Accept cookies弹出窗口,但为什么在以下弹出窗口上按'not Now'进行通知时,相同的命名法不起作用

p、 这与主页上的初始“登录”按钮相同,因此我必须发布直接xpath URL,但如果可能的话,我希望避免这样做


任何帮助都将不胜感激。

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

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 Implicit/Explit Wait。有关更多信息,请参阅。

伙计,谢谢!就这些。。。。。。。虚拟拳手