Selenium无法定位元素(Python)WebScraping

Joh*_*Doe 4 python selenium web-scraping selenium-webdriver

我正试图抓住一个房地产网站上市.它有一个aspx表单,必须在提交之前填写.

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

然而,我所关心的只是俄勒冈州的多户住宅物业.所以这是我的第一次尝试:

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)
Run Code Online (Sandbox Code Playgroud)

当我运行这个脚本时,它会给出一个错误"找不到元素"ForSalePropertyType".我在这里做错了什么?提前谢谢.

ale*_*cxe 5

这个元素位于一个iframe.你必须切换到它的上下文:

driver.switch_to.frame("ctl00_PlaceHolderMain_IFrameContent_IFrameContent")

# searching for multifamily residences
selectPropertyType = driver.find_element_by_id("ForSalePropertyType")
selectPropertyType.select_by_value("70")
Run Code Online (Sandbox Code Playgroud)

要返回默认上下文:

driver.switch_to.default_content()
Run Code Online (Sandbox Code Playgroud)

作为附注,请注意免责声明/使用条款中列出的政策,具体为:

您同意不会:(a)冒充任何人或实体或歪曲您与任何其他人或实体的关系; (b)从事垃圾邮件,洪水,收集电子邮件地址或其他个人信息,抓取,屏幕抓取,数据库抓取或任何其他活动,目的是获取用户列表或任何其他信息,包括具体的财产列表通过网站提供; (c)通过该网站发送连锁信或金字塔计划; 或(d)试图通过该网站未经授权访问其他计算机系统.您同意不会以任何可能损坏,禁用,过载或损害网站或干扰任何其他方使用和享受本网站的方式使用本网站.