【问题标题】:How to switch back from iFrame index to the relative parent?如何从 iFrame 索引切换回相对父级?
【发布时间】:2020-08-04 19:57:27
【问题描述】:

如何在 (Python) 中从 iFrame 索引切换回相对父级? 我使用此代码但不适用于我:

def type_order_credit_card1_number(self, credit_card_1_number):
    WebDriverWait(self.driver, 80).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH, '//iframe[1]')))
    WebDriverWait(self.driver, 80).until(EC.element_to_be_clickable((By.XPATH, self.order_credit_card_number_text_id))).send_keys(credit_card_1_number)

    time.sleep(4)

def type_order_credit_card_expiry_date(self, credit_card_1_expiry_date):
    WebDriverWait(self.driver, 80).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH, '//iframe[2]')))
    WebDriverWait(self.driver, 80).until(
        EC.element_to_be_clickable((By.XPATH, self.order_credit_card_expiry_date_text_id))).send_keys(
        credit_card_1_expiry_date)

我想从 [selectFrame index=2] 切换到 [selectFrame relative=parent] 以输入信用卡到期日期和 cvv(卡号可以正常工作)

【问题讨论】:

    标签: python selenium selenium-webdriver pyhook


    【解决方案1】:

    要从子 <iframe> 切换回直接父 <iframe>,您需要使用 switch_to() 语句,如下所示:

    driver.switch_to.parent_frame()
    

    要从任何子或孙<iframe> 切换回最顶层,即您需要使用的默认内容

    driver.switch_to.default_content()
    

    参考

    您可以在以下位置找到一些相关讨论:

    【讨论】:

      【解决方案2】:

      我通过使用 TAB 输入数字来更改我的代码,但总是为第二个选项卡输入一个数字(输入 cvv 编号)它只输入 7 而不是 737!

      WebDriverWait(self.driver, 80).until(
               EC.element_to_be_clickable((By.XPATH, self.order_credit_card_number_text_id))).send_keys(Keys.TAB,'1223', Keys.TAB, '737')
          time.sleep(4)
      

      【讨论】:

        猜你喜欢
        • 2015-07-09
        • 1970-01-01
        • 2021-09-25
        • 2014-12-13
        • 1970-01-01
        • 2012-09-04
        • 1970-01-01
        • 1970-01-01
        • 2023-03-31
        相关资源
        最近更新 更多