python输入无法定位_Selenium无法定位元素(Python)

1586010002-jmsa.png

I'm trying to scrape a real estate website for listings. It has an aspx form that has to filled out before submission.

All I care about is multifamily properties in Oregon, however. 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 an error "Can't locate element "ForSalePropertyType". What am I doing wrong here? Thanks in advance.

解决方案

This element located inside an iframe. You have to switch to it's 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 get back to default context:

driver.switch_to.default_content()

As a side note, be aware of the policies listed in the Disclaimer / Terms of use, specifically:

You agree that you will not: (a) impersonate any person or entity or

misrepresent your affiliation with any other person or entity; (b)

engage in spamming, flooding, harvesting of e-mail addresses or other

personal information, spidering, screen scraping, database scraping,

or any other activity with the purpose of obtaining lists of users or

any other information, including specifically, property listings

available through the site; (c) send chain letters or pyramid schemes

via the site; or (d) attempt to gain unauthorized access to other

computer systems through the site. You agree that you will not use the

site in any manner that could damage, disable, overburden, or impair

the site or interfere with any other party's use and enjoyment of the

site.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值