简体   繁体   English

修复元素不可交互 Selenium C#

[英]Fix Element not Interactable Selenium C#

I am trying to create a raffle autocheckout on feature.com using C# and Selenium.我正在尝试使用 C# 和 Selenium 在 feature.com 上创建抽奖自动结账。 I got through everything up to the payment page, where the payment fields (card,expiry, etc) are "not interactable".我完成了支付页面的所有内容,其中支付字段(卡、到期等)是“不可交互的”。 I've tried everything from using tag names, xpath, and nothing.我已经尝试过使用标签名称、xpath 等所有方法,但什么都没有。 This is my code:这是我的代码:

            const string cardName = "John A Doe";
            const string expiry = "1224";
            const string cvv = "001";

driver.FindElement(By.XPath("/html/body/div[3]/main/div[2]/div/div[3]/div[2]/div/div/div[2]/div/form/div[1]/div/input")).SendKeys(card);
           driver.FindElement(By.Id("card-holder")).SendKeys(cardName);
           driver.FindElement(By.Id("card-expiry-date")).SendKeys(expiry);
           driver.FindElement(By.Id("card-cvc")).SendKeys(cvv);

My guess is that since this is a payment, it is probably in an iframe.我的猜测是,由于这是一笔付款,它可能在 iframe 中。 You will need to switch to it and then add the cc info and the switch to main window to continue.您需要切换到它,然后添加 cc 信息和切换到主 window 以继续。

That first xpath needs to be fixed.第一个 xpath 需要修复。 That is a recipe for failure.这是失败的秘诀。 If you post the html we can help you with a better path.如果您发布 html,我们可以为您提供更好的路径。

I just realized you posted the site.我刚刚意识到您发布了该网站。 It is in an iframe and you can't get to the card name because it is in an iframe.它位于 iframe 中,您无法获取卡名称,因为它位于 iframe 中。

Based on this I would used the below but these fields are hidden which is always fun to deal with.基于此,我将使用以下内容,但这些字段是隐藏的,处理起来总是很有趣。

   SwitchToIframeByXpath("//iframe[contains(@id, 'card-fields-number')]");
   driver.FindElement(By.Xpath("//input[@placeholder='Card number']")).SendKeys(cardNum);
   SwitchToMainContent;
   SwitchToIframeByXpath("//iframe[contains(@id, 'card-fields-name')]");
   driver.FindElement(By.Xpath("//input[@placeholder='Name on card']")).SendKeys(cardName)
   SwitchToMainContent;

Rinse repeat for each field.对每个字段重复冲洗。

    public static void SwitchToIframe(string frameName)
    {
            driver.SwitchTo().Frame(frameName);
    }
    

or by xpath或通过 xpath

      public static void SwitchToIframeByXpath(string title)
    {
      driver.SwitchTo().Frame(driver.FindElement(By.XPath(title)));

    }
    

Then switch to main然后切换到主

    public static void SwitchToMainContent()
    {
             driver.SwitchTo().DefaultContent();
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM