Selenium:无法找到设置了等待超时的元素

Selenium: Can't locate element with wait timeout set

感谢您的宝贵时间。有点长,真心感谢。

我找不到该元素。我尝试设置等待、切换框架、执行 js

这里是页面完全加载后的HTMLDOM部分:(iframe里面的DOM现在省略了)

<body class="ng1 nu-theme-neutrino nu-light-theme nu-nav-style-standard nu-responsive-mini nu-responsive-small">
        <div id="display" class="flex-centered">
            <div class="loading flex-centered" style="min-height: 300px">
                <f-icon class="fa-loading icon-xl"></f-icon>
            </div>
        <iframe style="border: 0px; height: 100%; position: absolute; width: 100%;">#document</iframe>
<div class="prompt legacy-prompt">
        <div class="content">
                <div class="flex-column-centered">
                        <p>Enter your credentials</p>
                        <input id="user" type="text" placeholder="Username">
                        <input id="pass" type="password" placeholder="Password">
                </div>
                <div class="button-actions">
                        <button class="primary" type="button">Login</button>
                        <button type="button">Close Window</button>
                </div>
        </div>
</div>
</div>
<script src="/4517d062d20772b8f56b8a652474eaed/js/legacy_theme_setup.js"></script></body>

这是我想在 python

中实现的目标
ele = driver.find_element(By.CSS_SELECTOR, "#user")

错误:

selenium.common.exceptions.NoSuchElementException: Message: no such element: 
Unable to locate element: {"method":"css selector","selector":"#user"}

然后我尝试设置等待

ele = WebDriverWait(driver, 60).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#user")))
#or
ele = WebDriverWait(driver, 60).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "#user")))

两者得到相同的输出:

[22112:23972:0111/131341.033:ERROR:chrome_browser_main_extra_parts_metrics.cc(227)] START: ReportBluetoothAvailability(). If you don't see the END: message, this is crbug.com/1216328.
[22112:23972:0111/131341.033:ERROR:chrome_browser_main_extra_parts_metrics.cc(230)] END: ReportBluetoothAvailability()
[22112:23972:0111/131341.034:ERROR:chrome_browser_main_extra_parts_metrics.cc(235)] START: GetDefaultBrowser(). If you don't see the END: message, this is crbug.com/1216328.
[22112:1616:0111/131341.059:ERROR:device_event_log_impl.cc(214)] [13:13:41.059] Bluetooth: bluetooth_adapter_winrt.cc:1075 Getting Default Adapter failed.
[22112:23972:0111/131341.131:ERROR:chrome_browser_main_extra_parts_metrics.cc(239)] END: GetDefaultBrowser()

Traceback (most recent call last):
  File "C:\Users\a.py", line 60, in <module>
    ele = WebDriverWait(driver, 60).until(EC.element_to_be_clickable(
  File "C:\Users\...\wait.py", line 89, in until
    raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException

说超时,所以在限定时间(60s)内没有找到元素

然后我尝试在浏览器js控制台中定位元素,成功了。

document.getElementById('pass')
<input id=​"pass" type=​"password" placeholder=​"Password">​
document.getElementById('pass').value = '123456'
'123456'

browser js console screenshot

所以我尝试执行 js

    driver.execute_script("document.getElementById('pass').value = '123456'")

还是找不到

Traceback (most recent call last):
  File "C:\Users\a.py", line 54, in <module>
    driver.execute_script("document.getElementById('pass').value = '123456'")
...
   File "C:\Users\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\selenium\webdriver\remote\errorhandler.py", line 247, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.JavascriptException: Message: 
    javascript error: Cannot set properties of null (setting 'value')

我也试过定位框架,似乎框架加载不正确

DOM 在 iframe 内:

<html>
<head>
    <style>
        /* some style */
    </style>
</head>
<body style="margin: 0px; padding: 0px; height: 100%; width: 100%; overflow: hidden; cursor: text; user-select: none;">
    <x-screen role="textbox" tabindex="-1">
        <div
            style="display: block; position: fixed; overflow: hidden; user-select: text; width: 342px; height: 864px; left: 0px; top: 0px;">
            <x-fold style="display: block; margin-bottom: 0px;"></x-fold>
            <x-row>Server closed connection.</x-row>
            <x-fold style="display: block;"></x-fold>
        </div>
        <div style="visibility: hidden; height: 870px;"></div><textarea tabindex="-1" contenteditable="true"></textarea>
    </x-screen>
    <div class="cursor-node" focus="false" title="(0, 25)"></div>
    <div style="position: absolute; top: -99px; display: block; width: 10px; height: 10px;"></div>
</body>
</html>

代码:

    ele = WebDriverWait(driver, 15).until(EC.frame_to_be_available_and_switch_to_it(
        (By.CSS_SELECTOR, "iframe")))
    print("iframe", ele) 
#output: iframe True

    ele = driver.find_element(By.CSS_SELECTOR, "html")
    print("html", ele.get_attribute('innerHTML'))
#output: html <head></head><body></body>

    ele = driver.find_element(By.CSS_SELECTOR, "div.cursor-node")
#output:no such element: Unable to locate element: {"method":"css selector","selector":"div.cursor-node"}

    ele = driver.find_element(By.CSS_SELECTOR, "#pass")
#output: no such element: Unable to locate element: {"method":"css selector","selector":"#pass"}

    driver.switch_to.default_content()
    ele = driver.find_element(By.CSS_SELECTOR, "#pass")
#output: no such element: Unable to locate element: {"method":"css selector","selector":"#pass"}

非常感谢!

尝试查看 switch_to_frame,请参阅:

https://selenium-python.readthedocs.io/navigating.html#moving-between-windows-and-frames

iframe = driver.find_element_by_css_selector("iframe")
driver.switch_to_frame(iframe)

elem = driver.find_element_by_css_selector("#user")

据我所知,没有必要切换框架,因为这个元素不在其中。

我无法验证我的想法,因为您放置的 HTML 代码不可交互。

我已经修改了 HTML 在这个项目中设置一个默认值并且它没有给出值,但是,我能够从这个网络元素中获取位置和其他值。我建议删除 switch 方法,然后仔细检查。

import time
from selenium import webdriver
from selenium.webdriver.common.by import By

driver = webdriver.Chrome('chromedriver.exe')  # Optional argument, if not specified will search path.
try:
    driver.get('C:\Users\user\IdeaProjects\chrome-selenium-python\index.html')
    time.sleep(2)  # Let the user actually see something!

    ele = driver.find_element(By.CSS_SELECTOR, "#user")
    ele.send_keys("Example")
    time.sleep(2)  
    print("Location", ele.location)
    print("Tag", ele.tag_name)
except Exception as a:
    print(a)
finally:
    driver.quit()
wait=WebDriverWait(driver,30)                                 
wait.until(EC.presence_of_element_located((By.ID,"id"))).send_keys('user')

尝试存在感。

进口:

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait 
from selenium.webdriver.support import expected_conditions as EC

感谢大家的回答和帮助我!

我用过

html = driver.page_source
print(html)

打印看看selenium实际看到的是什么,我发现这是另一个不同的页面。然后我意识到这是另一个浏览器标签。

所以我认为它已经解决了。感谢您的帮助