如何通过Selenium将文本发送到地理定位搜索框中

问题描述 投票:1回答:2

HTML Code

我正在尝试将输入(zipcode)输入到商店定位器中。

zipcode = input("What zip code would you like to search? ")

driver = webdriver.Firefox()
driver.get('http://www2.dollargeneral.com/About-Us/pages/storelocator.aspx')
driver.maximize_window()
print "page accesses"

inputElement = driver.find_element_by_xpath("//*[@id='address']")
inputElement.send_key(zipcode)
inputElement.send_keys(Keys.ENTER)

我也在WebDriver中尝试了多种不同的select_by方法。任何人都可以朝着正确的方向推动我吗?我过去经常使用过硒,难道这在这个特定的网站上是不可能的吗?谢谢你的帮助!

python selenium selenium-webdriver iframe webdriverwait
2个回答
0
投票

smit9234是正确的 - 你需要切换帧。这有效:

from selenium import webdriver

zipcode = "91307" #input("What zip code would you like to search? ")

driver = webdriver.Chrome()
driver.get('http://www2.dollargeneral.com/About-Us/pages/storelocator.aspx')


driver.switch_to_frame("StoreLocator") 
el = driver.find_element_by_id("address")

el.send_keys(zipcode)

1
投票

要在商店定位器搜索框中发送字符序列(zipcode),您需要:

  • 首先引导WebDriverWait使所需的帧可用并切换到它
  • 然后再次引导WebDriverWait以使所需元素可单击并调用click()
  • 最后再次引导WebDriverWait使所需元素可单击并调用send_keys()
  • 代码块: from selenium import webdriver from selenium.webdriver.firefox.options import Options from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC options = Options() options.binary_location = r'C:\Program Files\Firefox Nightly\firefox.exe' driver = webdriver.Firefox(firefox_options=options, executable_path=r'C:\WebDrivers\GeckoDriver\geckodriver-v0.20.1-win64\geckodriver.exe') driver.get("http://www2.dollargeneral.com/About-Us/pages/storelocator.aspx") WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.ID,"StoreLocator"))) WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.closeBtn>img[src='images/dollargeneral_new_bubble_close.png']"))).click() zipcode = input("What zip code would you like to search? ") WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.form-control#address"))).send_keys(zipcode)
  • 浏览器快照:

zip

© www.soinside.com 2019 - 2024. All rights reserved.