0

I'm trying to get rid of the pop-up window showing up when first visiting this page: https://www.realestate.com.kh/buy/?listing_type=sale&categories=Condo&order_by=relevance&is_certified=1&private_seller=1#page-10

According to the html code, the id of the "x" element which I'd like Selenium to "click" is "leadpages-close-button"

enter image description here

So I'm trying to click on it with this code:

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait

driver = webdriver.Safari()
driver.get("https://www.realestate.com.kh/buy/?listing_type=sale&categories=Condo&order_by=relevance&is_certified=1&private_seller=1#page-10")
driver.find_element_by_id("leadpages-close-button").close()

But it results in an error:

  File "Untitled.py", line 6, in <module>
    driver.find_element_by_id("leadpages-close-button")
  File "/usr/local/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 359, in find_element_by_id
    return self.find_element(by=By.ID, value=id_)
  File "/usr/local/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 966, in find_element
    'value': value})['value']
  File "/usr/local/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 320, in execute
    self.error_handler.check_response(response)
  File "/usr/local/lib/python3.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: 

What am I doing wrong?

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Lucien S.
  • 5,123
  • 10
  • 52
  • 88

4 Answers4

2

That element is contained in an iframe so you will have to switch to iframe first. Try:

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait

driver = webdriver.Safari()
driver.get("https://www.realestate.com.kh/buy/?listing_type=sale&categories=Condo&order_by=relevance&is_certified=1&private_seller=1#page-1")
WebDriverWait(driver,20).until(EC.frame_to_be_available_and_switch_to_it(driver.find_element_by_tag_name("iframe")))
#driver.switch_to.frame(driver.find_element_by_tag_name("iframe")) # OR driver.switch_to.frame(0) # switch by iframe index
WebDriverWait(driver,20).until(EC.element_to_be_clickable(driver.find_element_by_id("leadpages-close-button")))
driver.find_element_by_id("leadpages-close-button").click()
driver.switch_to.default_content()

Also see this: https://www.guru99.com/handling-iframes-selenium.html

theGuy
  • 683
  • 5
  • 15
  • Strangely, this doesn't seem to work (no error message, but the iframe doesn't go away). Is there something I'm missing regarding the id of the iframe, maybe? – Lucien S. Aug 30 '18 at 19:34
  • 1
    @Rodolphe , this is because iframe is generated dynamically. Check [how to handle dynamic frame](https://stackoverflow.com/questions/7534622/selecting-an-iframe-using-python-selenium/48729644#48729644) – Andersson Aug 30 '18 at 19:41
  • Have you tried your solution as is @theGuy? I can't make it work for me... – Lucien S. Aug 30 '18 at 20:32
  • it does work for me, however I noticed iframe disappears from the screen if I use url with 'page-10' at the end. I was trying with 'page-1' at the end and it worked fine for me. Don't forget to add wait time as Andersson suggested. I will update my answer with what I tried. Another thing to note, I have tried it in Firefox not Safari, you may have check wait time if its still not working for you. – theGuy Aug 31 '18 at 13:57
1

Try below code to switch to dynamic iframe and click on Close button

from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait as wait

driver.get("https://www.realestate.com.kh/buy/?listing_type=sale&categories=Condo&order_by=relevance&is_certified=1&private_seller=1#page-10")
wait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it(driver.find_element_by_tag_name("iframe")))
driver.find_element_by_id("leadpages-close-button").click()
Andersson
  • 51,635
  • 17
  • 77
  • 129
  • Still no luck. As soon as the page is loaded, it closes (when using 1 instead of 10 after "page=", it closes almost instantly). Might this have something to do with the fact that I'm using Safari? – Lucien S. Aug 30 '18 at 21:23
  • Can you clarify little more: what *closes*: that frame with screaming man, main page, script? Share exception log – Andersson Aug 31 '18 at 05:14
1

To close the pop-up window showing up when first visiting the url https://www.realestate.com.kh/buy/?listing_type=sale&categories=Condo&order_by=relevance&is_certified=1&private_seller=1#page-10 as the popup is within an <iframe>:

  • First you have to induce WebDriverWait for the desired frame to be available and switch to it
  • Next you have to induce WebDriverWait again for the desired element to be clickable
  • Finally invoke click() on the desired element.
  • Code Block:

    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.chrome.options import Options
    
    options = Options()
    options.add_argument("start-maximized")
    options.add_argument("disable-infobars")
    options.add_argument("--disable-extensions") 
    driver=webdriver.Chrome(chrome_options=options, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
    driver.get("https://www.realestate.com.kh/buy/?listing_type=sale&categories=Condo&order_by=relevance&is_certified=1&private_seller=1#page-10")
    WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH, "//iframe[contains(@src,'https://realestatekh.lpages.co/leadbox')]")))
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//form[contains(@action,'https://realestatekh.activehosted.com/proc.php')]/a[@id='leadpages-close-button']"))).click()
    

Browser Snapshot:

realestate_com

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
-1

Try :

driver.get("https://www.realestate.com.kh/buy/?listing_type=sale&categories=Condo&order_by=relevance&is_certified=1&private_seller=1#page-10")
time.sleep(1)
driver.find_element_by_id("leadpages-close-button").close()
Hitchman34
  • 11
  • 1
  • 2
    this is not guaranteed to work. It might not sleep long enough, or it might sleep too long. There are selenium functions explicitly for waiting. – Bryan Oakley Aug 30 '18 at 19:24