Python/Selenium - Cant click' Accept cookies' button on www.instagram.com

筅森魡賤 提交于 2020-12-13 05:38:49

问题


Im trying to login on instagram using python selenium. But i have to Accept the cookies in order to continue.

1

This is my code

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)

The problem is that when it gets to click the accept button it does nothing. Any ideas?


回答1:


Should click the accept the login and the next two elements after it. Just wait till the element becomes clickable and then click it.

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()

Import

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



回答2:


try using this:

self.driver.find_element_by_xpath("//button[text()='Accept']").click()

I posted my solution here: Accepting cookies error with Python/Selenium on www.instagram.com



来源:https://stackoverflow.com/questions/64271467/python-selenium-cant-click-accept-cookies-button-on-www-instagram-com

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!