Selenium Cannot find element (Python) WebScraping

I am trying to clear a real estate site for listings. It has an aspx form, which must be completed before serving.

http://www.cbre.us/PropertyListings/Pages/Properties-for-Sale.aspx

All I care about is multi-unit properties in Oregon. So this was my first attempt:

driver = webdriver.Firefox()

driver.get("http://www.cbre.us/PropertyListings/Pages/Properties-for-Sale.aspx")

#Searching for multifamily residences
selectPropertyType = driver.find_element_by_id("ForSalePropertyType")
selectPropertyType.select_by_value("70")

#In the state of Oregon
selectState = driver.find_element_by_id("ForSaleState_ListBox1")
selectState.select_by_value("OR")

#Submit form
submitBtn = driver.find_element_by_id("ForSaleLooplinkSubmit")
submitBtn.click()

#Wait for results to load
WebDriverWait(driver, 5)

When I run this script, it gives the error “Cannot find the ForSalePropertyType element. What am I doing wrong here? Thanks in advance.

+4
source share
1 answer

This item is located inside iframe. You should switch to context:

driver.switch_to.frame("ctl00_PlaceHolderMain_IFrameContent_IFrameContent")

# searching for multifamily residences
selectPropertyType = driver.find_element_by_id("ForSalePropertyType")
selectPropertyType.select_by_value("70")

To return to the default context:

driver.switch_to.default_content()

, / , :

, : (a) ; () , , , spidering, , , , , , ; (c) ; (d) . , , , , - .

+5

Source: https://habr.com/ru/post/1607864/


All Articles