-1

我正在尝试构建一个函数来使用 Python 中的 Selenium 在以下网站上运行信息:https ://www.smartystreets.com/products/single-address 。给定参数中名为 address_to_search 的单行地址变量,该函数应执行以下操作:

  1. 使用 Selenium 加载网页 url
  2. 将第 2 步中的查找下拉菜单更改为“自由格式地址”</li>
  3. 在步骤 3 中将 address_to_search 插入文本框中
  4. 返回结果地址信息(现在作为文本文件很好)

这是不完整的功能代码:

def get_property_info(address_to_search):
    url = 'https://www.smartystreets.com/products/single-address'
    driver = webdriver.Chrome('chromedriver')
    driver.get(url)
    return driver.page_source

但是,每当我加载 url 时,我都没有在驱动程序的页面源中看到步骤 2 的下拉框,以便能够更改它或步骤 3 中的地址字段以插入给定的搜索。如果无法更改上面 2 中的下拉菜单,我可以更改输入以分别加载每个地址项(地址行 1、地址行 2、城市、州、邮政编码),即使这种方法不太理想,但无论哪种方式我无论如何都无法访问第 3 步中的项目。

您对如何在网页上定位这些项目并以其他方式构建功能有什么建议吗?

4

2 回答 2

0

正如我在评论中提到的那样,您被卡住了,因为它是一个单独的 iframe。只需导航到该 iframe 并施展魔法。如果您的地址和我的地址一样末尾有“\n”,您会看到结果显示(换行符充当回车键)。我还添加了驱动程序参数,因为我在外面声明了它。您可以根据需要对其进行修改。

address = "3301 South Greenfield Rd Gilbert, AZ 85297\n"

def get_property_info(driver,address_to_search):
    url = 'https://www.smartystreets.com/products/single-address-iframe'
    driver.get(url)
    driver.find_element_by_id("lookup-select-button").click()
    driver.find_element_by_id("lookup-select").find_element_by_id("address-freeform").click()
    driver.find_element_by_id("freeform-address").send_keys(address_to_search)

   #return driver.page_source

get_property_info(driver,address)
于 2021-09-01T19:49:36.873 回答
0

如前所述,有一个 iframe,我认为您应该通过 selenium 最大化您的窗口,也应该使用显式等待:-

以下是具有魔力的代码:-

driver = webdriver.Chrome(driver_path)
driver.maximize_window()
driver.implicitly_wait(50)
driver.get("https://www.smartystreets.com/products/single-address")
wait = WebDriverWait(driver, 20)
wait.until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR, "iframe[title='iframe']")))
driver.execute_script("window.scrollTo(0, 500)")
ele = wait.until(EC.element_to_be_clickable((By.ID, "lookup-select")))
driver.execute_script("arguments[0].scrollIntoView(true);", ele)
ele.click()
time.sleep(5)
wait.until(EC.element_to_be_clickable((By.ID, "address-freeform"))).click()
wait.until(EC.element_to_be_clickable((By.ID, "freeform-address"))).send_keys("3503 Adonais Way, Norcross")
wait.until(EC.element_to_be_clickable((By.ID, "submit-request"))).click()

进口:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
于 2021-09-02T07:47:41.567 回答