我正在尝试使用selenium执行google搜索,但我似乎无法用它单击google按钮。我正在尝试使用下面的代码
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填充搜索栏,但单击按钮时出现异常:
[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表单代码:
<input class="gNO89b" value="Google Search" aria-label="Google Search" name="btnK" type="submit" data-ved="0ahUKEwjHw5eTuprpAhXGjKQKHd7SCZkQ4dUDCAs">
发布于 2020-05-04 20:24:43
使用返回键似乎是有效的:
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)
发布于 2020-05-04 15:03:52
您是否检查了搜索按钮是否位于不同的iframe中?使用Selenium时,如果要访问的项位于不同的iframe中,则必须首先切换到该iframe,然后才能访问该项。为此您可以使用switchTo()
。
发布于 2020-05-04 15:32:03
在单击按钮之前先诱导一个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'))
https://stackoverflow.com/questions/61595113
复制相似问题