1

I tried to build a code to repeat the search on a website. I follow a number of method from the forum but still failed to resolve the below issue. Below error message was popped up when I tried to click the checkbox "Ensuite". See if anyone knows how to resolve it.

Error Message selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element ... is not clickable at point (323, 690). Other element would receive the click: ...

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 import webdriver
web = webdriver.Chrome()
url = 'https://www.spareroom.co.uk/flatshare/search.p'
wait = WebDriverWait(web, 20)
web.get(url)
wait.until(EC.element_to_be_clickable((By.ID, 'onetrust-accept-btn-handler'))).click()
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'div.panel-tab > a:nth-child(2)'))).click()
web.find_element(By.ID, "search_by_location_field").clear()
web.find_element(By.ID, "search_by_location_field").send_keys("KT5")
web.find_element(By.ID,"search-button").click()
web.maximize_window()

#To check the "Ensuite" checkbox ==> failed to select the option and with error
wait.until(EC.element_to_be_clickable((By.XPATH, '/html[1]/body[1]/main[1]/div[2]/aside[1]/form[1]/div[1]/div[1]/section[7]/div[3]/div[1]'))).click()

#To click the button "Apply filters" 
wait.until(EC.element_to_be_clickable((By.XPATH, '/html[1]/body[1]/main[1]/div[2]/aside[1]/form[1]/div[1]/div[1]/div[1]/div[1]/button[1]'))).click()
good fit
  • 11
  • 2

3 Answers3

0

It looks you are using the absolute path that causing the error

Use the below xpath to click on Yes for Ensuite checkbox

//input[@id='en-suite']

Your code will be like

wait.until(EC.element_to_be_clickable((By.XPATH, "//input[@id='en-suite']"))).click()

OR

wait.until(EC.element_to_be_clickable((By.XPATH, "//label[@title='Only show rooms with an En-suite bathroom']"))).click()
Akzy
  • 1,817
  • 1
  • 7
  • 19
  • I tried both codes but still failed and with the error message "selenium.common.exceptions.TimeoutException: Message:" – good fit Jun 26 '22 at 19:45
0

To make it work, I used SeleniumBase. Here's a script that you can run with pytest once you have the latest version of seleniumbase installed (today it's 3.3.2).

from seleniumbase import BaseCase

class MyTestClass(BaseCase):
    def test_base(self):
        self.open("https://www.spareroom.co.uk/flatshare/search.p")
        self.click("button#onetrust-accept-btn-handler")
        self.click('a[href="/flatshare/search.pl?searchtype=advanced"]')
        self.type("input#search_by_location_field", "KT5\n")
        self.check_if_unchecked("input#enSuite")
        self.click('button:contains("Apply filters")')

After running that, here's what the page looked like:

enter image description here

Michael Mintz
  • 9,007
  • 6
  • 31
  • 48
  • I run the code "seleniumbase" under Pycharm but no webpage pop up. Is it I miss out somethig. – good fit Jun 26 '22 at 19:48
  • Use “pytest” for running SeleniumBase tests. – Michael Mintz Jun 26 '22 at 20:33
  • I run pytest under the pycharam terminal. But the webpage was disappeared after execute the code. Can I stay in the webpage so that I can check the result? – good fit Jun 26 '22 at 22:44
  • Put in a breakpoint: ``import ipdb; ipdb.set_trace()`` Or put in a sleep: ``self.sleep(30)`` – Michael Mintz Jun 27 '22 at 00:04
  • Many thank. It works now and I like your code as it is more concise. Actually, my final product would like to repeat the function with different location (eg. KT1, KT2, KT5) and appear on different tab in one browser. Could you advise how to achieve it? – good fit Jun 27 '22 at 11:18
  • For repeating actions, you might be better off with using a [parameterized test](https://github.com/seleniumbase/SeleniumBase/blob/master/examples/parameterized_test.py). But if you need everything on a different tab, then read about Python loops, and use ``self.open_new_tab()`` to open a new tab with SeleniumBase. – Michael Mintz Jun 27 '22 at 12:41
  • Hi Michael. I have figured out how to do it now. Thanks again for your help. – good fit Jun 27 '22 at 13:06
  • '@Michael, I try to select the option "sort by" with below code but it failed. self.select_option_by_text("select.sort_by", "Newest Ads"). Do you have any idea? As I want to open the multiple tab with different location like "KT1, KT2 .." but it was failed when I use the self.open_tab(). I have google any example but I can't find any similar case. May I have you advise on it. – good fit Jul 06 '22 at 14:15
  • You could try using [Recorder Mode](https://github.com/seleniumbase/SeleniumBase/blob/master/help_docs/recorder_mode.md) to generate your script. – Michael Mintz Jul 06 '22 at 17:05
  • '@Michael, The record mode is very useful. May I know how to modify your previous code to accept a parameter so that I can call the function "test_base" and change the value like "KT1\n", "KT2\n"? – good fit Jul 06 '22 at 22:40
  • See [parameterized_test.py](https://github.com/seleniumbase/SeleniumBase/blob/master/examples/parameterized_test.py) for an example of parameterizing tests. – Michael Mintz Jul 06 '22 at 22:55
0

Finally, I solve the problem by ensuring the checkbox "Ensuite" is visible on the screen before I trigger the click. To do this, I have to locate an element and then trigger the key "PAGE_DOWN" first. It works but is very dumb.

 wait.until(EC.element_to_be_clickable((By.XPATH, '/html[1]/body[1]/main[1]/div[2]/aside[1]/form[1]/section[1]/div[1]/div[3]/input[1]'))).send_keys(Keys.PAGE_DOWN)

Below are the complete code for reference. However, I found it may not work every time.

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 import webdriver
from selenium.webdriver.common.keys import Keys

web = webdriver.Chrome()
url = 'https://www.spareroom.co.uk/flatshare/search.p'
wait = WebDriverWait(web, 20)
web.get(url)
wait.until(EC.element_to_be_clickable((By.ID, 'onetrust-accept-btn-handler'))).click()
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'div.panel-tab > a:nth-child(2)'))).click()
web.find_element(By.ID, "search_by_location_field").clear()
web.find_element(By.ID, "search_by_location_field").send_keys("KT5")
web.find_element(By.ID,"search-button").click()
web.maximize_window()

web.find_element(By.ID, "maxRent").send_keys("900")

# press page down in order to ensure "Ensuite" checkbox is visuable before click the checkbox
wait.until(EC.element_to_be_clickable((By.XPATH, '/html[1]/body[1]/main[1]/div[2]/aside[1]/form[1]/section[1]/div[1]/div[3]/input[1]'))).send_keys(Keys.PAGE_DOWN)
# To check the "Ensuite" checkbox
wait.until(EC.element_to_be_clickable((By.XPATH, '/html[1]/body[1]/main[1]/div[2]/aside[1]/form[1]/div[1]/div[1]/section[7]/div[3]/div[1]'))).click()

# To click the button "Apply filters"
wait.until(EC.element_to_be_clickable((By.XPATH, '/html[1]/body[1]/main[1]/div[2]/aside[1]/form[1]/div[1]/div[1]/div[1]/div[1]/button[1]'))).click()
web.find_element(By.ID, "sort_by").send_keys("Newest Ads")
Alessandro Togni
  • 680
  • 1
  • 9
  • 24
good fit
  • 11
  • 2