Selenium无法定位元素(Python)web垃圾

2024-05-19 07:56:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我想在一个房地产网站上搜索房源。它有一个必须在提交前填写的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)

当我运行这个脚本时,它会给出一个错误“cannotlocate element”ForSalePropertyType。我做错什么了?提前谢谢。在


Tags: idhttpforbywwwdriverelementproperties
1条回答
网友
1楼 · 发布于 2024-05-19 07:56:13

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

要返回默认上下文:

^{pr2}$

另外,请注意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.

相关问题 更多 >

    热门问题