gpt4 book ai didi

python - 如何通过 Selenium 根据 url 将文本发送到地理位置搜索框

转载 作者:行者123 更新时间:2023-12-01 09:05:10 24 4
gpt4 key购买 nike

HTML Code

我正在尝试在商店定位器中输入输入(邮政编码)。

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 方法。有人能把我推向正确的方向吗?我过去经常使用 Selenium ,这在这个特定的网站上是不可能的吗?感谢您的帮助!

最佳答案

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

  • 首先诱导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

关于python - 如何通过 Selenium 根据 url 将文本发送到地理位置搜索框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52121142/

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