首页 > 解决方案 > Python selenium - 单击 Google 搜索按钮时出错

问题描述

我正在尝试使用 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">

标签: pythonselenium

解决方案


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


推荐阅读