首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Python selenium -单击Google搜索按钮时出错

Python selenium -单击Google搜索按钮时出错
EN

Stack Overflow用户
提问于 2020-05-04 14:52:14
回答 3查看 794关注 0票数 0

我正在尝试使用selenium执行google搜索,但我似乎无法用它单击google按钮。我正在尝试使用下面的代码

代码语言:javascript
运行
复制
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填充搜索栏,但单击按钮时出现异常:

代码语言:javascript
运行
复制
[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) 

这是来自web的HTML表单代码:

代码语言:javascript
运行
复制
<input class="gNO89b" value="Google Search" aria-label="Google Search" name="btnK" type="submit" data-ved="0ahUKEwjHw5eTuprpAhXGjKQKHd7SCZkQ4dUDCAs">
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2020-05-04 20:24:43

使用返回键似乎是有效的:

代码语言:javascript
运行
复制
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)
票数 0
EN

Stack Overflow用户

发布于 2020-05-04 15:03:52

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

票数 0
EN

Stack Overflow用户

发布于 2020-05-04 15:32:03

在单击按钮之前先诱导一个WebDriverWait

代码语言:javascript
运行
复制
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'))
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61595113

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档