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

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

问题描述

我正在尝试抓取一个房地产网站上的商品.它有一个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")

要返回默认上下文,请执行以下操作:

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:

您同意不会:(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)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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