【问题标题】:Python/Selenium - Cant click' Accept cookies' button on www.instagram.comPython/Selenium - 无法单击 www.instagram.com 上的“接受 cookie”按钮
【发布时间】:2021-09-11 14:58:28
【问题描述】:

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

这是我的代码

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)

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

【问题讨论】:

    标签: python selenium cookies selenium-chromedriver


    【解决方案1】:

    应该点击接受登录及其后面的两个元素。等到元素变为可点击,然后点击它。

    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
    

    【讨论】:

      【解决方案2】:

      试试这个:

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

      我在这里发布了我的解决方案:Accepting cookies error with Python/Selenium on www.instagram.com

      【讨论】:

        【解决方案3】:

        您可以使用此代码:

        from selenium.webdriver.common.by import By
        from selenium.webdriver.support.wait import WebDriverWait
        from selenium.webdriver import Chrome
        from selenium import webdriver
        
        alert=WebDriverWait(driver, 15).until(EC.element_to_be_clickable((By.XPATH, '//button[contains(text(), "Accept All")]'))).click()
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2022-01-07
          • 1970-01-01
          • 2021-07-28
          • 2023-02-01
          • 2020-07-30
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多