【问题标题】:Python selenium - Error when click on Google search buttonPython selenium - 单击 Google 搜索按钮时出错
【发布时间】:2020-05-04 14:52:14
【问题描述】:

我正在尝试使用 selenium python 执行谷歌搜索,但我似乎无法点击谷歌搜索按钮。我正在尝试使用以下代码

browser = webdriver.Chrome(ChromeDriverManager().install())
url = 'https://google.com'
browser.get(url)
time.sleep(2)
name = 'q'
search_el = browser.find_element_by_name("q")
search_el.send_keys("selenium python")
submit_btn_el = browser.find_element_by_css_selector("input[type='submit']")
print(submit_btn_el.get_attribute('name'))
time.sleep(2)
submit_btn_el.click()

它正确地用搜索字符串 selenium python 填充搜索栏,但单击按钮时出现异常:

[24660:18480:0504/185929.817:ERROR:configuration_policy_handler_list.cc(90)] Unknown policy: EnableCommonNameFallbackForLocalAnchors
[24660:18480:0504/185929.881:ERROR:configuration_policy_handler_list.cc(90)] Unknown policy: EnableCommonNameFallbackForLocalAnchors

DevTools listening on ws://127.0.0.1:58996/devtools/browser/7031edca-5982-4156-b093-e03f85607449
[24660:18480:0504/185929.983:ERROR:browser_switcher_service.cc(238)] XXX Init()
Traceback (most recent call last):
  File "c:/Users/apugazhenthy/Documents/30 days of python/Day 16/google.py", line 18, in <module>
    WebDriverWait(browser,10).until(EC.element_to_be_clickable((By.CSS_SELECTOR,"input[type='submit']"))).click()  
  File "C:\Program Files (x86)\Python38-32\lib\site-packages\selenium\webdriver\remote\webelement.py", line 80, in 
click
    self._execute(Command.CLICK_ELEMENT)
  File "C:\Program Files (x86)\Python38-32\lib\site-packages\selenium\webdriver\remote\webelement.py", line 633, in _execute
    return self._parent.execute(command, params)
  File "C:\Program Files (x86)\Python38-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in 
execute
    self.error_handler.check_response(response)
  File "C:\Program Files (x86)\Python38-32\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, 
in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element <input class="gNO89b" value="Google Search" aria-label="Google Search" name="btnK" type="submit" data-ved="0ahUKEwjbtaqk15rpAhVExzgGHS16BW0Q4dUDCAc"> is not clickable at point (431, 521). Other element would receive the click: <div class="fbar">...</div>
  (Session info: chrome=81.0.4044.129) 

这是来自网络的 HTML 表单代码:

<input class="gNO89b" value="Google Search" aria-label="Google Search" name="btnK" type="submit" data-ved="0ahUKEwjHw5eTuprpAhXGjKQKHd7SCZkQ4dUDCAs">

【问题讨论】:

    标签: python selenium


    【解决方案1】:

    使用 RETURN 键似乎有效:

    from selenium import webdriver
    from selenium.webdriver.common.keys import Keys
    
    browser = webdriver.Chrome()
    browser.get('https://google.com')
    
    browser.find_element_by_name("q").send_keys("selenium python"+Keys.RETURN)
    

    【讨论】:

    • 非常感谢您的回复。它的工作就像一个魅力!你能帮我理解一下返回键功能是如何点击搜索的吗?
    • 您有两种方式向 Google 发送查询字符串: 1) 点击Search 按钮; 2) 输入Enter(回车)键。
    【解决方案2】:

    您是否检查过搜索按钮是否位于不同的 iframe 中?使用 Selenium 时,如果您要访问的项目位于不同的 iframe 中,则必须先切换到该 iframe,然后才能访问该项目。你可以使用switchTo()

    【讨论】:

    • 您好,感谢您的回复。不幸的是,我没有找到任何用于搜索按钮的 iframe。
    【解决方案3】:

    在点击按钮之前诱导WebDriverWait

    from selenium import webdriver
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import WebDriverWait
    
    browser = webdriver.Chrome()
    url = 'https://google.com'
    browser.get(url)
    
    name = 'q'
    search_el = browser.find_element_by_name("q")
    search_el.send_keys("selenium python")
    
    WebDriverWait(browser,10).until(EC.element_to_be_clickable((By.XPATH,"//div[@class='aajZCb']//input[@value='Google Search']"))).click()
    
    #submit_btn_el = browser.find_element_by_css_selector("input[type='submit']")
    #print(submit_btn_el.get_attribute('name'))
    

    【讨论】:

    • 您好,感谢您的回复,不幸的是,您的建议我也遇到了同样的问题。 raise exception_class(message, screen, stacktrace) selenium.common.exceptions.ElementClickInterceptedException:消息:元素点击被拦截:元素 在点 (431, 521) 不可点击。其他元素会收到点击:
      ...
    • 奇怪,它对我有用。您可以尝试将我的答案“按原样”复制/粘贴到新文件中并运行它吗?不禁想到还有其他原因导致了这个问题。
    • 我已更改标识字符串以确保唯一性,因为输入[type='submit'] 的元素不止一个。
    • 非常感谢!!在尝试了您的第二个建议后,它按预期工作。:)
    猜你喜欢
    • 2021-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-29
    • 2021-03-18
    相关资源
    最近更新 更多