如何根据网站在iframe内的搜索框中将邮政编码作为文本发送?

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

我正在使用Selenium进行网络自动化,我无法在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:

任何帮助,将不胜感激!

python selenium-webdriver iframe webdriver webdriverwait
1个回答
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

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