python - Selenium 无法定位元素(Python)WebScraping

标签 python selenium selenium-webdriver web-scraping

我正在尝试抓取一个房地产网站的列表。它有一个 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)

当我运行此脚本时,出现错误“无法找到元素“ForSalePropertyType”。我在这里做错了什么?提前致谢。

最佳答案

此元素位于 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")

回到默认上下文:

driver.switch_to.default_content()

作为旁注,请注意 Disclaimer / Terms of use 中列出的政策,具体来说:

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.

关于python - Selenium 无法定位元素(Python)WebScraping,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32636453/

相关文章:

python - 带有 Rails API 的 Django 应用程序

python - Pandas 数据框中每两列的总和

selenium - 如何使用 Selenium Webdriver 捕获特定元素而不是整个页面的屏幕截图?

java - 如何解决线程 "main"java.lang.NoSuchFieldError : INSTANCE?中的异常

Selenium EdgeDriver 禁用保存密码对话框

python - 关闭 python tkinter 中的所有窗口

python - "Empty reply from server"用于 Flask + uWSGI 设置

java - {"error":"unsupported_grant_type","error_description":"grant type not supported"} 销售队伍 Java

javascript - Selenium javascript 自动化 Gmail 登录

python - 如何通过 Selenium 和 Python 点击​​某个元素