1

I've written a script in python with selenium to perform a search in a webpage using search-box available there. However, When I run my script, It throws an error which I'm gonna paste below. The thing is when the webpage is loaded through my script, there is an advertisement pops up hiding the search-box. How can I get around that and fetch the search result? Thanks in advance.

Link to that site: webpage

Script I'm trying with:

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 I'm having:

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>

This is the ad which hides the search box:

enter image description here

Btw, the search parameter is available within .send_keys() in my script. Anything from the populated result will suffice.

SIM
  • 21,997
  • 5
  • 37
  • 109

2 Answers2

2

The simplest solution is to simulate exactly the same action user should do: close an ad in case it appeared:

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()
Andersson
  • 51,635
  • 17
  • 77
  • 129
  • I just tried it sir. It throws an error when it hits the line within `try/except` block. Here is the traceback: `line 242, in check_response raise exception_class(message, screen, stacktrace) selenium.common.exceptions.WebDriverException: Message: unknown error: Element is not clickable at point (809, -13)` – SIM Mar 26 '18 at 14:53
  • When I try to use more specific selector `#oss_body a.btn_close`, it throws timeout exception. Given plus one for your solution. – SIM Mar 26 '18 at 14:58
  • Thanks:) Sorry, but right now I cannot try my code. I'll check it when come back home... if no one provide with better solution till that time – Andersson Mar 26 '18 at 15:10
  • @shayan, I'm afraid I cannot reproduce your issue - I tried this code several times and each time it passed... Also note that Close button and `div#oss_body` are siblings, so `#oss_body a.btn_close` selector won't work... You need `"#oss_layer1 a.btn_close"` – Andersson Mar 26 '18 at 17:53
  • 1
    I'm gonna accept this as an answer. However, I got success only with this line of code and nothing else `wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, "input#q"))).send_keys("Camden Medical Centre, 1 Orchard Boulevard 248649",Keys.RETURN)` – SIM Mar 26 '18 at 18:04
  • 1
    Good workaround. You can also use `driver.execute_script("arguments[0].click();", wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, "input#search_button"))))`, but both approaches contradict the user-like behavior. If it's important for you... If not, it's ok :) – Andersson Mar 26 '18 at 18:22
0

Use below code. This will click the element even if it is hidden beneath ad

((IJavaScriptExecutor)driver).ExecuteScript("arguments[0].scrollIntoView(true)", driver.GetElement(By.Id("someID")));
Rakesh Raut
  • 183
  • 3
  • 12