python - 由于某些广告隐藏了搜索按钮,因此无法点击该按钮

标签 python python-3.x selenium selenium-webdriver web-scraping

我用 python 用 selenium 编写了一个脚本,使用那里可用的搜索框在网页中执行搜索。但是,当我运行我的脚本时,它会抛出一个错误,我将其粘贴在下面。问题是,当通过我的脚本加载网页时,会弹出一个隐藏搜索框的广告。我怎样才能绕过这个问题并获取搜索结果?提前致谢。

链接到该网站:webpage

我正在尝试使用的脚本:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys

driver = webdriver.Chrome()
wait = WebDriverWait(driver, 10)
driver.get("replace_with_above_site")

wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, "input#q"))).send_keys("Camden Medical Centre, 1 Orchard Boulevard 248649")
wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, "input#search_button"))).click() ##error thrown here
driver.quit()

我的回溯:

Traceback (most recent call last):
  File "C:\Users\WCS\AppData\Local\Programs\Python\Python36-32\demo.py", line 12, in <module>
    wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, "input#search_button"))).click()
  File "C:\Users\WCS\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\remote\webelement.py", line 80, in click
    self._execute(Command.CLICK_ELEMENT)
  File "C:\Users\WCS\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\remote\webelement.py", line 628, in _execute
    return self._parent.execute(command, params)
  File "C:\Users\WCS\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 312, in execute
    self.error_handler.check_response(response)
  File "C:\Users\WCS\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: Element <input type="button" id="search_button" onclick="submitSearch();"> is not clickable at point (868, 137). Other element would receive the click: <div id="splash_screen_overlay"></div>

这是隐藏搜索框的广告:

enter image description here

顺便说一句,搜索参数在我的脚本中的 .send_keys() 中可用。填充结果中的任何内容都足够了。

最佳答案

最简单的解决方案是模拟用户应该执行的完全相同的操作:在广告出现时关闭广告:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import NoSuchElementException

driver = webdriver.Chrome()
wait = WebDriverWait(driver, 10)
driver.get("replace_with_above_site")

try:
    wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.btn_close"))).click()
except NoSuchElementException:
    pass

wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, "input#q"))).send_keys("Camden Medical Centre, 1 Orchard Boulevard 248649")
wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, "input#search_button"))).click()
driver.quit()

关于python - 由于某些广告隐藏了搜索按钮,因此无法点击该按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49492762/

相关文章:

使用 "print"语句的 Python IDLE 错误着色

selenium - 如果 Selenium 服务器未运行,如何跳过 PHPUnit 中的测试?

java - Selenium 无法识别 Jenkins 驱动程序

Selenium 无法从 Jenkins 运行 Firefox 浏览器

python - 如果所有值都在同一列中,如何从 csv 文件读取数据?

python - 查找Eulerian Tour的Python代码在一种情况下不起作用。为什么?

python - 下载 OPENSSL 后 pip 显示错误

python - 在网站上查找一个单词并获取其页面链接

python-3.x - 如何使用 Boto3 向 AWS Batch 提交多个命令?

python - 如何使用facet创建条形图并使用seaborn添加标签