【问题标题】:How to click on "Not Now" button within Instagram through Selenium and Python如何通过 Selenium 和 Python 在 Instagram 中点击“Not Now”按钮
【发布时间】:2018-12-09 07:03:11
【问题描述】:

我正在尝试使用我目前正在编码的“instagram bot”登录 Instagram。我已经通过登录屏幕,通过“获取应用程序”屏幕以及它希望如何打开通知。两个选项是“打开”和“现在不”。我正在尝试使用与以前相同的方法单击“不是现在”,但它不起作用。代码(在 Firefox 上使用检查元素)说

<button class="aOOlW   HoLwm " tabindex="0">Not Now</button>

我已经尝试将类代码与

一起使用
notNowButton = driver.find_element_by_xpath("//a[@class='aOOlW HoLwm']")
# or
notNowButton = driver.find_element_by_xpath("//a[@tabindex='0']")

但这也不起作用。有什么帮助吗?

通知:

【问题讨论】:

    标签: python selenium xpath css-selectors instagram


    【解决方案1】:

    使用WebDriverWait 和Xpath 匹配文本Not Now

    from selenium import webdriver
    from selenium.webdriver.support.ui import WebDriverWait 
    
    notNowButton = WebDriverWait(driver, 15).until(
        lambda d: d.find_element_by_xpath('//button[text()="Not Now"]')
    )
    notNowButton .click()
    

    【讨论】:

      【解决方案2】:

      所以这对我有用。我已经测试了 10 次,但这可能不是最好的方法。

      #we just set a simple sleep time.it's not complicated we just wait for 5 seconds to make sure the page is fully loaded
      sleep(5)
      #since it's a button we just write //button and then it has to search for the described class which in this scenario it is aOOlW   HoLwm and then it just clicks on it
      not_now_notif=browser.find_element_by_xpath("//button[@class='aOOlW   HoLwm ']")
      
      not_now_notif.click()
      

      顺便说一句,您可以在这里查看。我相信这也可能会有所帮助 https://www.edureka.co/community/575/need-wait-until-page-completely-loaded-selenium-webdriver

      【讨论】:

        【解决方案3】:

        看来你已经很接近了。

        在你的第一次代码试用中:

        driver.find_element_by_xpath("//a[@class='aOOlW HoLwm']")
        

        &lt;tag&gt; 不是&lt;a&gt; 标签而是&lt;button&gt; 标签。所以如下改变它会起作用。

        driver.find_element_by_xpath("//button[@class='aOOlW   HoLwm ']")
        

        现在所需的元素是 JavaScript 启用的元素,因此您必须诱导 WebDriverWait 以使 元素可点击,以下行将完美运行: p>

        WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//button[@class='aOOlW   HoLwm ']"))).click()
        

        您也可以使用 cssSelector,如下所示:

        WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.aOOlW.HoLwm"))).click()
        

        注意:您必须添加以下导入:

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

        【讨论】:

          【解决方案4】:
          browser = webdriver.Chrome('/Users/username/Documents/WebDriver/chromedriver')
          
          
          browser.find_elements_by_xpath("//button[contains(text(), 'Not Now')]")[0].click()
          

          【讨论】:

          • 请不要只发布代码作为答案,而是解释您的代码的作用以及它如何解决问题的问题
          【解决方案5】:

          你必须先使用复制css选择器来复制路径

          Notnow = driver.find_element_by_css_selector("body > div.RnEpo.Yx5HN > div > div > div.mt3GC > button.aOOlW.HoLwm")
          
          Notnow.click()
          

          【讨论】:

            【解决方案6】:

            首先,通过文本找到元素并将其分配给not_now_button

            not_now_button = driver.find_element_by_xpath("//*[text()='Not Now']")
            

            然后点击按钮:

            not_now_button.click()
            

            【讨论】:

              猜你喜欢
              • 2019-01-31
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2014-02-16
              • 2016-05-08
              • 2021-09-14
              相关资源
              最近更新 更多