0

So I have tried looking at the iframes in the website but cannot figure out where this element falls under. Im trying to access an element by class name. Here is my code below and here is the website.

from requests import get
from bs4 import BeautifulSoup
from selenium.webdriver import Chrome
from selenium.webdriver.chrome.options import Options
from selenium import webdriver

browser = webdriver.Chrome(executable_path= '/Users/abeelcf/Downloads/chromedriver')
browser.get('https://www.redfin.com')
zipcode = input("Enter a zip code to look up: ")
search_form = browser.find_element_by_id('search-box-input')
search_form.send_keys(zipcode)
search_form.submit()

#pg 2
browser.find_element_by_id("MapHomeCard_0")

The URL is https://www.redfin.com/zipcode/20007

The problem is with the last line. It cannot find element MapHomeCard_0 saying the element no such element.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
camab
  • 3
  • 3
  • After you submit the search_form use a wait since the page is loading and will miss finding the element. – Arundeep Chohan Sep 15 '20 at 04:44
  • You can directly go to https://www.redfin.com/zipcode/20007 by using `browser.get("https://www.redfin.com/zipcode/20007")` . The problem is with finding the element before the page has loaded. – Anup Tiwari Sep 15 '20 at 04:47

2 Answers2

0

You need to wait for the page to load after the search_box submit use a web driver wait.

WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, 'MapHomeCard_0')))

Also import the following

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait 
from selenium.webdriver.support import expected_conditions as EC
Arundeep Chohan
  • 9,779
  • 5
  • 15
  • 32
0

To locate the element you need to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following Locator Strategies:

  • Using XPATH:

    driver.get('https://www.redfin.com')
    search_form = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@class='search-input-box' and @id='search-box-input'][@title='City, Address, School, Agent, ZIP']")))
    search_form.send_keys("20007")
    search_form.submit()
    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[@class='homecards']/div[@id='MapHomeCard_0']"))).text)
    
  • Using CSS_SELECTOR:

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.homecards>div#MapHomeCard_0"))).text)
    
  • Console Output:

    LISTED BY REDFIN
    3D WALKTHROUGH
    $464,000
    1 Bed
    1 Bath
    1,014 Sq. Ft.
    2500 Q St NW #412, Washington, DC 20007
    
  • Note : You have to add the following imports :

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

References

You can find a couple of relevant discussions on NoSuchElementException in:

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