我正在尝试使用Selenium python执行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 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)
这是Web上的HTML表单代码:
<input class="gNO89b" value="Google Search" aria-label="Google Search" name="btnK" type="submit" data-ved="0ahUKEwjHw5eTuprpAhXGjKQKHd7SCZkQ4dUDCAs">
答案 0 :(得分:0)
您是否检查过搜索按钮是否位于其他iframe中?使用Selenium时,如果要访问的项目位于其他iframe中,则必须先切换到该iframe,然后才能访问该项目。您可以使用switchTo()
。
答案 1 :(得分:0)
在点击按钮之前先产生一个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'))
答案 2 :(得分:0)
使用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)