【问题标题】:Python Selenium click google "I agree" buttonPython Selenium 点击谷歌“我同意”按钮
【发布时间】:2021-03-24 22:44:09
【问题描述】:

我正在尝试抓取一些谷歌数据,但我首先想点击谷歌弹出的“我同意”按钮。这是我用来执行此操作的脚本:

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

search_question = input("Ask a question: ")

driver = webdriver.Chrome("*Your Webdriver location*")
driver.wait = WebDriverWait(driver, 5)

driver.get("https://google.com")

time.sleep(1)
agree = driver.wait.until(EC.presence_of_element_located((By.XPATH, '//*[@id="introAgreeButton"]/span/span')))
agree.click()
# time.sleep(0.2)

search = driver.find_element_by_class_name("gLFyf")
search.send_keys(search_question)
search.send_keys(Keys.ENTER)

问题是 selenium 似乎没有找到按钮,因此我收到超时错误。 (我也尝试过 find_element_by_xpath 并且仍然无法正常工作)。

【问题讨论】:

    标签: python selenium web-crawler


    【解决方案1】:

    问题是它没有改变框架这里是解决方案代码:

    from selenium import webdriver
    from selenium.webdriver.common.keys import Keys
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
    driver = webdriver.Chrome('C:\\Users\\gassp\\OneDrive\\Namizje\\Python.projects\\chromedriver.exe')
    url = 'https://www.google.com/maps/'
    
    driver.get(url)
    
    WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH, '//*[@id="consent-bump"]/div/div[1]/iframe')))
    agree = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="introAgreeButton"]/span/span'))) 
    agree.click()
    
    #back to the main page
    driver.switch_to_default_content()
    
    
    WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="searchboxinput"]'))).send_keys('gostilne')
    driver.submit()
    

    【讨论】:

      【解决方案2】:

      如果您在 devtools 检查器中向上滚动,您会注意到您的元素位于 iframe 中:

      你需要先切换到那个框架,点击你的按钮然后切换回默认内容(主页)

      
      driver.get("https://google.com")
      
      #active the iframe and click the agree button
      WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH, "//iframe")))
      agree = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="introAgreeButton"]/span/span'))) 
      agree.click()
      
      #back to the main page
      driver.switch_to_default_content()
      

      这对我有用。

      仅供参考 - 页面上只有 1 个 iframe,这就是 xpath //iframe 起作用的原因。如果有多个,您需要更准确地识别它。

      【讨论】:

        【解决方案3】:

        如果您已经同意,则不会出现同意按钮。这就是为什么它无法找到给定的 XPath。

        试试这个:

        import time
        from selenium import webdriver
        from selenium.webdriver.common.keys import Keys
        from selenium.webdriver.common.by import By
        from selenium.webdriver.support.ui import WebDriverWait
        from selenium.webdriver.support import expected_conditions as EC
        
        search_question = input("Ask a question: ")
        
        driver = webdriver.Chrome(".\chromedriver.exe")
        driver.wait = WebDriverWait(driver, 5)
        
        driver.get("https://google.com")
        
        time.sleep(3)
        # agree = driver.wait.until(EC.presence_of_element_located((By.XPATH, '//*[@id="introAgreeButton"]/span/span')))
        # agree.click()
        # time.sleep(0.2)
        
        search = driver.find_element_by_class_name("gLFyf")
        search.send_keys(search_question)
        search.send_keys(Keys.ENTER)
        
        

        【讨论】:

        • 不显示弹出窗口。问题是如果我不移除窗口,那么后面返回的数据是不完整的。
        【解决方案4】:

        我在单击弹出窗口时遇到了一些问题,问题出在那个 click() 上。 我不确定是否与 urs 有相同的问题,但请尝试将您的点击更改为:

        agree = driver.find_element_by_xpath('//*[@id="introAgreeButton"]/span/span')
        driver.execute_script("arguments[0].click();", agree)
        

        【讨论】:

        • 是的,我已经尝试过这种方式(抱歉没有提及),但仍然无法正常工作。问题是硒找不到按钮
        猜你喜欢
        • 2022-01-23
        • 2014-02-16
        • 2016-05-08
        • 2021-09-14
        • 2018-05-11
        • 1970-01-01
        • 2021-11-06
        相关资源
        最近更新 更多