填写第一个卡号字段后,我在页面上找不到任何 iframe。如何解决这个问题

After filling first card number field I can't find any iframes on the page. How to solve this

class TestAplazame(unittest.TestCase):

@classmethod
def setUpClass(cls, headless=False) -> None:
    cls.chrome_options = webdriver.ChromeOptions()
    if headless:
        cls.chrome_options.headless = True
    else:
        cls.chrome_options.headless = False

    cls.chrome_options.add_argument(
        "user-agent=Mozilla/5.0 (Windows Phone 10.0; Android 4.2.1; Microsoft; Lumia 640 XL LTE) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Mobile Safari/537.36 Edge/12.10166")
    cls.chrome_options.add_argument('--disable-extensions')
    cls.chrome_options.add_argument('--no-sandbox')
    cls.chrome_options.add_experimental_option('excludeSwitches',
                                               ['enable-automation'])
    cls.driver = webdriver.Chrome(executable_path=r"driver/chromedriver.exe", options=cls.chrome_options)
    cls.driver.maximize_window()  # maximize window if headless is not running
    cls.driver.implicitly_wait(10)
    cls.wait = WebDriverWait(cls.driver, 20)

def test_checkout(self):
    self.driver.get("https://cdn.aplazame.com/widgets/demo/")
    self.wait.until(EC.element_to_be_clickable((By.CLASS_NAME, "pay-with-aplazame"))).click()
    self.wait.until(EC.frame_to_be_available_and_switch_to_it((By.ID, "aplazame-checkout-iframe")))
    self.wait.until(EC.element_to_be_clickable((By.NAME, "accepts_gdpr"))).click()
    self.wait.until(EC.element_to_be_clickable((By.CLASS_NAME, "btn"))).click()
    time.sleep(3)
    self.wait.until(EC.element_to_be_clickable((By.CLASS_NAME, "btn"))).click()
    self.wait.until(EC.element_to_be_clickable((By.NAME, "document_id"))).send_keys("X0345345 T")
    self.wait.until(EC.element_to_be_clickable((By.NAME, "birthday"))).send_keys("22121995")
    time.sleep(0.2)

    self.driver.find_element_by_name("job_status").click()  # profession selector
    self.wait.until(EC.element_to_be_clickable((By.CLASS_NAME, "apz_typeahead__prediction_content"))).click()
    time.sleep(0.5)

    job = self.driver.find_element_by_name("job_sector")
    job.send_keys(Keys.ARROW_DOWN)
    job.send_keys(Keys.ENTER)

    nation = self.driver.find_element_by_name("nationality")
    nation.send_keys(Keys.ARROW_DOWN)
    nation.send_keys(Keys.ENTER)

    self.driver.find_element_by_name("checkout_checkboxes[aplazame_conditions]").click()  # checkbox check
    time.sleep(0.2)
    self.wait.until(EC.element_to_be_clickable((By.CLASS_NAME, "btn"))).click()

Look from here

    iframes = self.driver.find_elements_by_xpath("//iframe[contains(@name,'__privateStripeFrame')]")

    self.driver.switch_to.frame(iframes[0])
    self.wait.until(EC.visibility_of_element_located((By.NAME, "cardnumber")))
    card_number = self.driver.find_element_by_name("cardnumber")
    card_number.send_keys("4074655237184431")

iframes are only available to the top block iframes elements are not available to the bottom block. included error message at the bottom

    self.driver.switch_to.frame(iframes[1])  # frame switch to exp date
    self.wait.until(EC.visibility_of_element_located((By.NAME, "exp-date")))
    exp_field = self.driver.find_element_by_name("exp-date")
    exp_field.send_keys(1023)

    self.driver.switch_to.frame(iframes[2])
    self.wait.until(EC.visibility_of_element_located((By.NAME, "cvc")))
    exp_field = self.driver.find_element_by_name("cvc")
    exp_field.send_keys(657)

@classmethod
def tearDownClass(cls) -> None:
    cls.driver.close()
    cls.driver.quit()

if name == 'main': unittest.main()

连我都找不到相同的 iframe 了。问题是什么。如何修复它。这是一个支付网关测试。我相信网关来自条纹。 这是错误消息:

selenium.common.exceptions.StaleElementReferenceException:消息:过时的元素引用:元素未附加到页面文档

当我在填充卡片字段后访问 iframe 变量而不是切换到下一个 iframe“StaleElementReferenceException”时,抛出此异常。

如有任何帮助,我们将不胜感激。

所有三个卡片字段都是不同的 iframe:

请找到工作代码 - 对于有效期,您可以继续卡号本身,对于 cvc,您必须找到该元素并填写它。

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait

driver = webdriver.Chrome()
wait = WebDriverWait(driver, 10)

driver.get('https://stripe-payments-demo.appspot.com/')

CardFrame = driver.find_element_by_xpath(
    "//iframe[contains(@name,\"privateStripeFrame\") and @title=\"Secure card payment input frame\"]")
driver.switch_to.frame(CardFrame)

CardNumber = wait.until(EC.visibility_of_element_located((By.XPATH, "//input[@name=\"cardnumber\"]")))
CardNumber.send_keys("40746552371844310122")

CVC = wait.until(EC.visibility_of_element_located((By.XPATH, "//input[@name=\"cvc\"]")))
CVC.send_keys("222")

如果解决了您的问题,请标记为答案。

看来你还在之前的iframe你需要切换到default_content()然后再移动到另一个iframe.

self.driver.switch_to.default_content()

然后切换到另一个框架。

self.wait.until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR, "iframe[name*='privateStripeFrame']")))
self.wait.until(EC.visibility_of_element_located((By.NAME, "exp-date")))
exp_field = self.driver.find_element_by_name("exp-date")
exp_field.send_keys(1023)

让我知道这是怎么回事。