謝謝你的時間。有點長,非常感謝。
我找不到元素。我嘗試設定等待,切換??框架,執行js
這是頁面完全加載后的部分 HTML DOM:(現在省略了 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'

我無法驗證我的想法,因為您放置的 HTML 代碼不可互動。
我已經修改了 HTML,在此專案中設定了默認值,但它沒有給出值,但是,我能夠從此 Web 元素中獲取位置和其他值。我建議洗掉 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()
uj5u.com熱心網友回復:
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
uj5u.com熱心網友回復:
感謝大家回答并試圖幫助我!
我用了
html = driver.page_source
print(html)
列印并查看 selenium 實際看到的內容,我發現它是另一個不同的頁面。然后我意識到這是另一個瀏覽器選項卡。
所以我認為它已經解決了。感謝您的幫助
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/411992.html
標籤:
