无法检测元素并将输入通过Selenium发送到“信用卡号”字段

时间:2018-10-05 15:23:01

标签: python selenium selenium-webdriver css-selectors webdriverwait

我正在尝试自动化this website上的结帐过程。我处于第4阶段,您单击“付款信息”中的“信用卡”选项,而我正尝试send_keys输入我的信用卡号。

但是,我注意到单击CC选项后,页面加载了一段时间,因此我使用了显式等待该元素的方法,但该方法不起作用。任何帮助将不胜感激。

ccNumber = session.find_element_by_css_selector('input[name=credit-card-number]')
wait = WebDriverWait(session, 100)
wait.until(EC.element_to_be_selected(ccNumber))

这是错误:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"input[name=credit-card-number]"}

2 个答案:

答案 0 :(得分:1)

信用卡编号的androidx.recyclerview:RecyclerView:1.0.0字段位于<input>之内,因此您必须:

  • 促使WebDriverWait提供所需的框架,并切换到该框架
  • 促使WebDriverWait获得所需的元素可点击,您可以使用以下解决方案:
  • 代码块:

    <iframe>

答案 1 :(得分:0)

您可以等到“加载”微调器不再出现在页面上,然后再检查信用卡输入。我们有一个C#解决方案,您可以尝试适应python,在其中循环等待显示元素,捕获异常直到达到超时。

    //Assert an element is displayed before the timeout milliseconds run out.
    public void AssertElementIsDisplayed(int timeout, IWebElement element, string elementName = "element")
    {
        Action<IWebElement> target = delegate (IWebElement e) {
            if (e == null)
            {
                throw new AssertionException("Failed to find " + elementName + ". It is null");
            }
            if (!e.Displayed)
            {
                elementName = (elementName == "element" && !String.IsNullOrEmpty(e.GetAttribute("title"))) ? e.GetAttribute("title") : elementName;
                throw new AssertionException("Expected (" + elementName + ") to be displayed but it was not");
            }
        };
        AssertInLoop(element, (long)timeout, 100L, target);
    }

    //Assert some Action on a WebElement for as long as the timeoutMillis allow.
    private void AssertInLoop(IWebElement element, long timeoutMillis, long millisBetweenAttempts, Action<IWebElement> callable)
    {
        AssertionException lastAssertionError = null;
        WebDriverException lastWebDriverException = null;

        long startTime = DateTimeOffset.Now.Ticks / TimeSpan.TicksPerMillisecond;
        if (timeoutMillis < 500 || timeoutMillis > 120 * 1000)
        {
            throw new ArgumentException("Timeout outside expected range. timeout_millis=" + timeoutMillis);
        }

        long millisLeft = timeoutMillis;

        while (millisLeft >= 1)
        {
            long lastAttemptStartMillis = DateTimeOffset.Now.Ticks / TimeSpan.TicksPerMillisecond;
            try
            {
                callable(element);
                return;
            }
            catch (AssertionException e)
            {
                lastAssertionError = e;
                lastWebDriverException = null;
            }
            catch (StaleElementReferenceException e)
            {
                lastAssertionError = null;
                lastWebDriverException = e;
            }
            catch (NotFoundException e)
            {
                lastAssertionError = null;
                lastWebDriverException = e;
            }
            catch (SystemException e)
            {
                throw e;
            }


            long elapsedMillis = (DateTimeOffset.Now.Ticks / TimeSpan.TicksPerMillisecond) - startTime;
            millisLeft = timeoutMillis - elapsedMillis;

            if (millisLeft >= 1)
            {
                long millisElapsedDuringThisAttempt = (DateTimeOffset.Now.Ticks / TimeSpan.TicksPerMillisecond) - lastAttemptStartMillis;
                long millisToSleep = millisBetweenAttempts - millisElapsedDuringThisAttempt;
                if (millisToSleep > 0)
                {
                    Thread.Sleep((int)millisToSleep);
                }
            }
        }

        if (lastAssertionError != null)
        {
            throw lastAssertionError;
        }
        else if (lastWebDriverException != null)
        {
            throw lastWebDriverException;
        }
    }