如何使用Selenium识别iframe?

发布于 2025-01-15 01:03:57 字数 449 浏览 1 评论 0原文

HTML:

<iframe allowpaymentrequest="true" allowtransparency="true" src="https://shopify.wintopay.com/
cd_frame_id_="ca9e4ad6a1559de159faff5c1f563d59"
name="WinCCPay"
id="win-cc-pay-frame" 

我正在尝试在抄送字段中输入文本。显然它在一个 iframe 中,我选择了 HTML 中的最后一个,并尝试从上面的标识符中选择它,但我不断收到无法找到该元素的信息

iframe= wd.find_element_by_id("win-cc-pay-frame")    

wd.switch_to.frame(iframe)

该框架当前正在浏览器中显示,因此不需要隐式等待。

HTML:

<iframe allowpaymentrequest="true" allowtransparency="true" src="https://shopify.wintopay.com/
cd_frame_id_="ca9e4ad6a1559de159faff5c1f563d59"
name="WinCCPay"
id="win-cc-pay-frame" 

I'm trying to input text in a CC field. Apparently its in an iframe I picked the last one in the HTML and tried to select it from the identifiers above but I keep getting the element couldn't be found

iframe= wd.find_element_by_id("win-cc-pay-frame")    

wd.switch_to.frame(iframe)

The frame is currently being shown in the browser so no need for implicit wait.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

も让我眼熟你 2025-01-22 01:03:57

要识别 ,因此您必须:

  • 归纳 WebDriver等待所需的框架可用并切换到它。

  • 您可以使用以下任一定位器策略

    • 使用CSS_SELECTOR

      WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe#win-cc-pay-frame[name='WinCCPay'][src^='https:// shopify.wintopay.com']")))
      
    • 使用XPATH

      WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[@id='win-cc-pay-frame' and @name='WinCCPay'][开始于(@src,'https://shopify.wintopay.com')]“)))
      
  • 注意:您必须添加以下导入:

     from selenium.webdriver.support.ui import WebDriverWait
     从 selenium.webdriver.common.by 导入
     从 selenium.webdriver.support 导入预期条件作为 EC
    

To identify the <iframe> so you have to:

  • Induce WebDriverWait for the desired frame to be available and switch to it.

  • You can use either of the following Locator Strategies:

    • Using CSS_SELECTOR:

      WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe#win-cc-pay-frame[name='WinCCPay'][src^='https://shopify.wintopay.com']")))
      
    • Using XPATH:

      WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[@id='win-cc-pay-frame' and @name='WinCCPay'][starts-with(@src, 'https://shopify.wintopay.com')]")))
      
  • Note : You have to add the following imports :

     from selenium.webdriver.support.ui import WebDriverWait
     from selenium.webdriver.common.by import By
     from selenium.webdriver.support import expected_conditions as EC
    
最丧也最甜 2025-01-22 01:03:57

问题可能是元素的名称和 id 是动态的,并且会针对每个唯一的结帐窗口进行更改?您可以检查是否在 iframe 标记中添加 class 属性并通过该属性查找元素吗?

它一定类似于:

iframe = wd.find_element_by_class_name('card-pay-iframe')
wd.switch_to.frame(iframe)
...
wd.switch_to.default_content()

良好的编码! ˙_(ツ)_/˙

The problem can be that the name and id of the element are dynamic and change for each unique checkout window? Can you check if adding class attribute at iframe tag and find element by this attribute?

It must be similar to:

iframe = wd.find_element_by_class_name('card-pay-iframe')
wd.switch_to.frame(iframe)
...
wd.switch_to.default_content()

good coding! ¯_(ツ)_/¯

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文