Selenium 无法定位元素 (Python) WebScraping [英] Selenium Unable to locate element (Python) WebScraping

查看:36
本文介绍了Selenium 无法定位元素 (Python) WebScraping的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试抓取一个房地产网站以获取房源.它有一个必须在提交前填写的 aspx 表单.

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

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

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

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)

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

When I run this script it gives an error "Can't locate element "ForSalePropertyType". What am I doing wrong here? Thanks in advance.

推荐答案

此元素位于 iframe 内.你必须切换到它的上下文:

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")

返回默认上下文:

driver.switch_to.default_content()

<小时>

作为旁注,请注意免责声明/使用条款中列出的政策,特别是:

您同意您不会:(a) 冒充任何人或实体或歪曲您与任何其他人或实体的从属关系;<强>(b)从事垃圾邮件、泛滥、收集电子邮件地址或其他个人信息,蜘蛛,屏幕抓取,数据库抓取,或任何其他以获取用户列表为目的的活动,或任何其他信息,特别包括房产清单可通过网站获得; (c) 发送连锁信或传销通过网站;或 (d) 试图未经授权访问其他通过网站的计算机系统.您同意不会使用以任何可能损坏、禁用、过载或损害的方式网站或干扰任何其他方的使用和享受网站.

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.

这篇关于Selenium 无法定位元素 (Python) WebScraping的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆