如何根据网站在iframe内的搜索框中以文本形式发送邮政编码?

时间:2018-09-04 18:37:21

标签: python selenium-webdriver iframe webdriver webdriverwait

HTML iframe code

我正在使用Selenium进行Web自动化,但是在Dollar Tree网站上输入正确的框架以在某个邮政编码内搜索商店位置时遇到问题。这是我到目前为止所拥有的;

driver = webdriver.Chrome()
driver.get('https://www.dollartree.com/custserv/custserv.jsp?pageName=StoreLocations')

driver.switch_to_frame("Dealer locator")
time.sleep(2)
elem = driver.find_element_by_id("inputaddress")
time.sleep(2)
elem.send_keys(zipcode)

我不确定是否需要更改框架,但是我已经尝试过

driver.find_element_by_id
driver.find_element_by_class_name
driver.find_element_by_xpath

所有这些都给我同样的错误

NoSuchElementException: no such element: Unable to locate element:

任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:0)

根据 HTML ,您已共享将字符序列发送到搜索框,该元素位于文本为查找商店的元素下方您需要切换到所需的 iframe 来诱导 WebDriverWait ,然后再次诱导 WebDriverWait 来使所需的元素可点击您可以使用以下解决方案:

  • 代码块:

    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.webdriver.common.by import By
    
    options = Options()
    options.add_argument("start-maximized")
    options.add_argument("disable-infobars")
    options.add_argument("--disable-extensions")
    driver = webdriver.Chrome(chrome_options=options, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
    driver.get("https://www.dollartree.com/custserv/custserv.jsp?pageName=StoreLocations")
    WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[src='//hosted.where2getit.com/dollartree/']")))
    element = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.addressentry#inputaddress")))
    element.click()
    element.clear()
    element.send_keys("799003")
    
  • 浏览器快照:

dollar_tree