首页 > 解决方案 > 如何使用 Selenium 和 python 编程在亚马逊中处理没有 iframe 的“邮政编码”窗口弹出

问题描述

我几乎尝试使用编程语言与弹出窗口相关的所有代码是 python 编码和 selenium,但它们都不是send.key("XXXXX")邮政编码中的工作命令。因为它们大多与iframe页面源相关,所以它可以使用 windowswitch_to.frame()switch_to.window. 在我的情况下iframe,页面源代码中没有,并且弹出窗口不会在该输入邮政编码中发送密钥。所以我需要帮助来处理这些类型的窗口弹出 python 和 selenium 编程来处理弹出窗口。

driver.switch_to.frame()
driver.switch_to.window()
driver.switch_to.alert()


    browser.get('www.amazon.com')
        browser.find_element_by_xpath("""//*[@id="nav-global-location-popover-link"]""").click()
        browser.set_page_load_timeout(1000)
        browser.switch_to.active_element()
        browser.find_element_by_xpath('//*[@id="GLUXZipUpdateInput"]').send_keys("85224")

标签: iframeselenium-chromedriverpopupwindowzipcodeweb-scraping-language

解决方案


您需要单击交货位置栏并以编程方式设置密码,如下所示

loc = str(driver.find_element_by_id("glow-ingress-line2").text).strip().replace(" ", "")
        if loc == "India" in driver.page_source:
            WebDriverWait(driver, 30).until(
                EC.presence_of_element_located((By.ID, "nav-packard-glow-loc-icon")))
            driver.find_element_by_id("nav-packard-glow-loc-icon").click()
            WebDriverWait(driver, 30).until(
                EC.presence_of_element_located((By.ID, "GLUXZipUpdateInput")))
            driver.find_element_by_id("GLUXZipUpdateInput").send_keys(10001)

            ActionChains(driver).send_keys(Keys.SHIFT, Keys.ENTER).perform()

            WebDriverWait(driver, 30).until(
                EC.presence_of_element_located((By.ID, "GLUXConfirmClose")))
            driver.refresh()


推荐阅读