【问题标题】:Python-Selenium does not find existing element by idPython-Selenium 没有通过 id 找到现有元素
【发布时间】:2021-08-19 14:04:34
【问题描述】:

我有一个包含以下元素的页面(我无法共享 URL,因为该页面是私有的):

<input readonly="" type="text" class="form-control" id="submissionKey" value="{{ token }}" style="font-size:1.2em;color:black; width: 400px">

我正在尝试通过以下方法获得:

element = driver.find_element_by_id("submissionKey")

但这只会报错:

selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: [id="submissionKey"]

我也尝试像这样实现一些等待:

wait = WebDriverWait(driver, 10)
element = wait.until(EC.element_to_be_clickable((By.ID, 'submissionKey')))

然后报错

selenium.common.exceptions.TimeoutException: Message: 

我在这里错过了什么?

其他信息

  • 看起来不像是时区问题
  • 不涉及 iframe。

完整的页面正文:

<body style="margin:20px">
    
    <p>Go to <b>Jupyter Notebook</b> and complete the exercise.</p>

    <form class="form-inline">
        <div class="input-group">
            <div class="input-group-addon" style="width:40px;font-size:1.2em"><i class="fa fa-key" aria-hidden="true"></i></div>
            <input readonly="" type="text" class="form-control" id="submissionKey" value="{{ token }}" style="font-size:1.2em;color:black; width: 400px">
        </div>
    </form>

    <p style="margin-left:55px"><small>Use this key to fetch the simulation results in the notebook in order to complete the exercise.</small></p>

    <script>
        console.log('window', window.location.search);

        var queryString = window.location.search; // Returns:'?q=123'
        let params = new URLSearchParams(queryString);
        let auth = params.get("auth");

        var input = document.getElementById('submissionKey');
        input.value = auth;
    </script>

</body>

【问题讨论】:

  • 我猜它的时区问题,再次检查并判断它是否不工作,伙计
  • 你检查过 iframe 吗?可以分享一下网址吗?
  • 无时区问题,不涉及 iframe
  • 你能分享页面网址吗?我怀疑会有多个元素。
  • 不,我不能。它不公开

标签: python selenium


【解决方案1】:

事实证明,selenium 再次没有“跟随”用户正在做的事情。使用 selenium,如果您自己在 iframe 中,则必须小心(selenium 仅适用于单个 iframe,而不适用于用户看到的整个页面),也不会在浏览器打开新选项卡时自动切换。本例中就是这种情况。

一些测试打开了一个新标签,你必须手动切换到它。对于 python-selenium,您可以使用

driver.switch_to.window(driver.window_handles[-1])

您可以使用关闭该选项卡

driver.close()

【讨论】:

    猜你喜欢
    • 2021-06-22
    • 2021-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-20
    • 1970-01-01
    • 2022-07-28
    • 1970-01-01
    相关资源
    最近更新 更多