我曾嘗試查看 Stack Overflow 上的幾個答案,但都無濟于事。當我列印網頁的頁面源時,我只能看到標簽內某個點的源,給或取幾個字符。超出的 HTML 元素永遠不會在頁面源代碼中加載或列印出來。當我嘗試加載應該存在的HTML 元素時(當我在 Chrome 上查看頁面源時它們在那里),我得到 aTimeoutException或NoSuchElementException.
通過多因素身份驗證門戶后,我正在決議動態加載的網站。我已經印刷driver.current_url,以確保我在正確的URL外交部,曾嘗試后sleep(100),并試圖明確地等待EC.url_contains(...),EC.element_to_be_clickable(...)和EC.presence_of_element_located(...)。
這是我的代碼:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
url = "https://brightspace.nyu.edu/d2l/home"
driver = webdriver.Chrome() # should open a Chrome window
driver.get(url) # navigate to brightspace
# MFA Handling Code here #
# Explicitly wait until we reach the Brightspace home page (logged in)
element = WebDriverWait(driver,100).until(EC.url_contains('https://brightspace.nyu.edu/d2l/home'))
print(driver.page_source)
banner = driver.find_element_by_id('bannerTitle') # throws NoSuchElementException
這是輸出的一部分:
<!-- ... previous styles and HTML in <head> ... -->
<style is="custom-style">html {
--d2l-color-woolonardo: var(--d2l-color-sylvite);
.
. lots of colors
.
--d2l-color-olivine-light-1: var(--d2l-color-olivine-plus-1);
--d2l
<!-- ^^ the page source cuts off here, in <head> -->
最后一行給出以下錯誤:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[id="bannerTitle"]"}
uj5u.com熱心網友回復:
而不是banner = driver.find_element_by_id我建議使用WebDriverWait,By和EC。我也會print(driver.page_source)在橫幅找到后放置。我們也可以嘗試向下滾動頁面。下面我注釋掉了您的一些臺詞并添加了我建議的更新。
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
url = "https://brightspace.nyu.edu/d2l/home"
driver = webdriver.Chrome() # should open a Chrome window
driver.get(url) # navigate to brightspace
# MFA Handling Code here #
# Explicitly wait until we reach the Brightspace home page (logged in)
element = WebDriverWait(driver,100).until(EC.url_contains('https://brightspace.nyu.edu/d2l/home'))
# print(driver.page_source)
# banner = driver.find_element_by_id('bannerTitle') # throws NoSuchElementException
##################################
######## NEW SUGGESTIONS #########
##################################
banner = WebDriverWait(self.driver, 100).until(EC.visibility_of_element_located(
(By.ID, "bannerTitle")))
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
print(driver.page_source)
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/361649.html
