Python / Selenium-www.instagram.com에서 '쿠키 허용'버튼을 클릭 할 수 없습니다.

Oct 08 2020

파이썬 셀레늄을 사용하여 인스 타 그램에 로그인하려고합니다. 하지만 계속하려면 쿠키를 수락해야합니다.

이것은 내 코드입니다

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)

문제는 수락 버튼을 클릭 할 때 아무것도하지 않는다는 것입니다. 어떤 아이디어?

답변

ArundeepChohan Oct 08 2020 at 22:58

로그인 수락을 클릭하고 그 뒤의 다음 두 요소를 클릭해야합니다. 요소를 클릭 할 수있을 때까지 기다린 다음 클릭하면됩니다.

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
U_double_G Nov 29 2020 at 13:51

이것을 사용해보십시오 :

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

내 솔루션을 여기에 게시 했습니다 .www.instagram.com에서 Python / Selenium에서 쿠키 오류 허용