Python/Selenium - 无法点击 www.instagram.com 上的“接受 cookie”按钮

标签 python selenium cookies selenium-chromedriver

我正在尝试使用 python selenium 登录 instagram。但我必须接受 cookie 才能继续。

1

这是我的代码

class InstaBot:
    def __init__(self, username, pw):
        self.driver = webdriver.Chrome()
        self.driver.get('https://www.instagram.com/')
        sleep(2)
 #this is the code that im trying to use, so to click the accept button 
self.driver.find_element_by_xpath("/html/body/div[2]/div/div/div/div[2]/button[1]").click() 
        self.driver.find_element_by_xpath("//input[@name=\"username\"]")\
            .send_keys(username)
        self.driver.find_element_by_xpath("//input[@name=\"password\"]")\
            .send_keys(pw)
        self.driver.find_element_by_xpath("//a[contains(text(), 'Log in')]")\
            .click()
        sleep(4)

问题是当它点击接受按钮时它什么也没做。有什么想法吗?

最佳答案

应该点击接受登录和接下来的两个元素。只需等待元素变为可点击,然后点击它即可。

WebDriverWait(self.driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//button[text()='Accept']"))).click()
#Your code
WebDriverWait(self.driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#loginForm > div > div:nth-child(3) > button"))).click()  
WebDriverWait(self.driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//button[text()='Save Info']"))).click()
WebDriverWait(self.driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//button[text()='Turn On']"))).click()

导入

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

关于Python/Selenium - 无法点击 www.instagram.com 上的“接受 cookie”按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64271467/

相关文章:

python - 如何以编程方式清除 Maya 脚本编辑器?

python - 在 Pandas 中,df ['column' ] 和 df.column 有什么区别?

python - 如何删除列表中的重复值?

python - sentiwordnet python中的词义消歧

python - 如何检测 Chrome 浏览器是否在 selenium 中 headless ?

c# - 释放关键 Selenium

java.lang.ClassNotFoundException : WebDriver API 异常

.htaccess - htaccess 从 www 到非 www,在 session /cookie 中引入变量

android - 从 webview 获取带有路径和过期日期的 cookie

python - 将 Cookie 添加到 ZSI 帖子