Python / Selenium - Không thể nhấp vào nút 'Chấp nhận cookie' trên www.instagram.com

Oct 08 2020

Tôi đang cố gắng đăng nhập trên instagram bằng cách sử dụng python selen. Nhưng tôi phải Chấp nhận cookie để tiếp tục.

Đây là mã của tôi

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)

Vấn đề là khi nó nhấp vào nút chấp nhận, nó không làm gì cả. Có ý kiến ​​gì không?

Trả lời

ArundeepChohan Oct 08 2020 at 22:58

Nên nhấp vào chấp nhận đăng nhập và hai yếu tố tiếp theo sau nó. Chỉ cần đợi cho đến khi phần tử có thể nhấp được và sau đó nhấp vào nó.

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

Nhập khẩu

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

hãy thử sử dụng cái này:

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

Tôi đã đăng giải pháp của mình ở đây: Chấp nhận lỗi cookie với Python / Selenium trên www.instagram.com