gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-30 02:43:37 26 4
gpt4 key购买 nike

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

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com