試圖從網頁中抓取資料。登錄該站點后,在開發人員工具中能夠搜索 xpath 并找到匹配項。但是,paython 代碼沒有回傳資料。
from datetime import datetime
from sqlite3 import Time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
import requests
from bs4 import BeautifulSoup
import zerodhacred
browser = webdriver.Chrome("C:/Users/SPILLAIP/Downloads/chromedriver_win32/chromedriver.exe")
browser.get(loginURL)
nifty_bank_values_xpath = browser.find_elements(By.XPATH, "//span[contains(@class, 'pane-legend-item-value__main')]")
print("Len of nifty_bank_values_xpath: ",len(nifty_bank_values_xpath))
輸出是
d:\My Personal\2022\Suresh\Learning\Python\zerodha.py:27: DeprecationWarning: executable_path has been deprecated, please pass in a Service object browser = webdriver.Chrome("C:/Users/SPILLAIP/Downloads/chromedriver_win32 /chromedriver.exe")
DevTools listening on ws://127.0.0.1:57153/devtools/browser/74a90941-a12f-4be4-b12a-01b256292a5f [15120:6400:0309/123030.129:ERROR:device_event_log_impl.cc(214)] [12:30:30.129] USB: usb_device_handle_win.cc:1049 Failed to read descriptor from node connection: A device attached to the system is not functioning. (0x1F) [15120:6400:0309/123030.137:ERROR:device_event_log_impl.cc(214)] [12:30:30.136] USB: usb_device_handle_win.cc:1049 Failed to read descriptor from node connection: A device attached to the system is not functioning. (0x1F) Len of nifty_bank_values_xpath: 0
similarly, when tried with find_element
nifty_bank_values_xpath = browser.find_element(By.XPATH, "//span[contains(@class, 'pane-legend-item-value__main')]")
Getting following error:
raise exception_class(message, screen, stacktrace) selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//span[contains(@class , 'pane-legend-item-value__main')]"} (會話資訊: chrome=99.0.4844.51) Stacktrace: Backtrace:
能夠在 Dev Tools->Elements 中找到回傳 6 個匹配項的資料。
圖片
從開發控制臺捕獲的 html
<body class="app-wrapper">
<noscript><strong>We're sorry but kite doesn't work properly without JavaScript enabled. Please enable it to continue.</strong></noscript>
<div id="app" class="app mobile page-tvchart">
<div class="header">
<div class="wrapper">
<!---->
<div class="header-right">
<!----> <!---->
<div class="app-nav mobile"><a href="/marketwatch" class=""><span class="icon icon-bookmark"></span></a> <a href="/dashboard" class=""><span class="icon icon-compass"></span></a> <a href="/orders" class=""><span class="icon icon-book"></span></a> <a href="/holdings" class=""><span class="icon icon-briefcase"></span></a> <a href="/positions" class=""><span class="icon icon-file-text"></span></a> <a href="/funds" class="margins"><span class="icon icon-credit-card"></span></a></div>
<div class="right-nav">
<div class="user-nav perspective">
<a href="" class="dropdown-label">
<div id="avatar-43">
<div class="avatar" style="width: 25px; height: 25px; border-radius: 50%; text-align: center; vertical-align: middle; background-color: rgba(156, 39, 176, 0.1); font-size: 9px; font-weight: 300; color: rgb(156, 39, 176); line-height: 26px;"><span>SS</span></div>
<!---->
</div>
<span class="user-id">ZX8487</span>
</a>
<!---->
</div>
</div>
</div>
</div>
</div>
<div class="container wrapper">
<!---->
<div class="container-right">
<!----> <!---->
<div class="page-content tvchart">
<!---->
<div>
<div class="chart-frame">
<div id="tv_chart_container" class="tv-chart-container" style="height: 547px;"><iframe id="tradingview_e1a6c" name="tradingview_e1a6c" src="/static/tv-chart/static/en-tv-chart.aaac22e21df68f2f7bad.html#symbol=NIFTY BANK:INDICES:260105&interval=1D&widgetbar={"details":false,"watchlist":false,"watchlist_settings":{"default_symbols":[]}}&timeFrames=[{"text":"5y","resolution":"W"},{"text":"1y","resolution":"W"},{"text":"6m","resolution":"120"},{"text":"3m","resolution":"60"},{"text":"1m","resolution":"30"},{"text":"5d","resolution":"5"},{"text":"1d","resolution":"1"}]&locale=en&uid=tradingview_e1a6c&clientId=tradingview.com&userId=ZX8487&chartsStorageUrl=/api/chart/preferences&chartsStorageVer=1.1&customCSS=/static/tv-chart/static/custom_style.css&debug=false&timezone=Asia/Kolkata&theme=Light" frameborder="0" allowtransparency="true" scrolling="no" allowfullscreen="" style="display: block; width: 100%; height: 100%;"></iframe></div>
</div>
<div class="instrument-market-data">
<div class="row">
<div class="three columns">
<div class="label">Open</div>
<div class="value">33278.9</div>
</div>
<div class="three columns">
<div class="label">High</div>
<div class="value">33890.9</div>
</div>
<div class="three columns">
<div class="label">Low</div>
<div class="value">32948.9</div>
</div>
<div class="three columns">
<div class="label">Close</div>
<div class="value">33158.1</div>
</div>
</div>
<div class="row">
<div class="three columns">
<div class="label">Volume</div>
<div class="value">—</div>
</div>
<div class="three columns">
<div class="label">Avg. trade price</div>
<div class="value">—</div>
</div>
<div class="three columns">
<div class="label">Total buy quantity</div>
<div class="value">—</div>
</div>
<div class="three columns">
<div class="label">Total sell quantity</div>
<div class="value">—</div>
</div>
</div>
</div>
<!---->
</div>
</div>
</div>
</div>
<!----> <!---->
<div class="baskets">
<!----> <!----> <!----> <!----> <!----> <!---->
</div>
<!---->
<div>
<!----> <!---->
</div>
<!----> <!----> <!----> <!---->
<div class="orders-basket">
<!---->
</div>
<!----> <!---->
</div>
<script async="">try {
var theme = JSON.parse(localStorage.__storejs_kite_theme);
if (theme) {
document.documentElement.setAttribute("data-theme", theme);
}
} catch (_) {
}
</script><script type="module" src="/static/js/chunk-vendors.ea6114a1.js"></script><script type="module" src="/static/js/app.ae4bb317.js"></script><script>!function(){var e=document,t=e.createElement("script");if(!("noModule"in t)&&"onbeforeload"in t){var n=!1;e.addEventListener("beforeload",function(e){if(e.target===t)n=!0;else if(!e.target.hasAttribute("nomodule")||!n)return;e.preventDefault()},!0),t.type="module",t.src=".",e.head.appendChild(t),t.remove()}}();</script><script src="/static/js/chunk-vendors-legacy.ea6114a1.js" nomodule=""></script><script src="/static/js/app-legacy.cffeb71c.js" nomodule=""></script>
<div class="su-toast-groups">
<div class="su-toast-group su-toast-top-left">
<div></div>
</div>
<div class="su-toast-group su-toast-top-center">
<div></div>
</div>
<div class="su-toast-group su-toast-top-right">
<div></div>
</div>
<div class="su-toast-group su-toast-bottom-left">
<div></div>
</div>
<div class="su-toast-group su-toast-bottom-center">
<div></div>
</div>
<div class="su-toast-group su-toast-bottom-right">
<div></div>
</div>
</div>
<!---->
</body>
uj5u.com熱心網友回復:
你在wait這里少了一個。
在使用方法訪問它們之前,您應該等待元素完全加載find_elements。
這里最好的方法是使用預期條件顯式等待,如下所示:
from datetime import datetime
from sqlite3 import Time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
import requests
from bs4 import BeautifulSoup
import zerodhacred
browser = webdriver.Chrome("C:/Users/SPILLAIP/Downloads/chromedriver_win32/chromedriver.exe")
wait = WebDriverWait(browser, 20)
browser.get(loginURL)
wait.until(EC.visibility_of_element_located((By.XPATH, "//span[contains(@class, 'pane-legend-item-value__main')]")))
time.sleep(0.3) #short pause added to make sure that all the relevant elements are loaded, not only the first one
nifty_bank_values_xpath = browser.find_elements(By.XPATH, "//span[contains(@class, 'pane-legend-item-value__main')]")
print("Len of nifty_bank_values_xpath: ",len(nifty_bank_values_xpath))
UPD
由于該元素位于 iframe 中,因此您必須先切換到該 iframe,然后才能訪問其中的元素,如下所示:
from datetime import datetime
from sqlite3 import Time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
import requests
from bs4 import BeautifulSoup
import zerodhacred
browser = webdriver.Chrome("C:/Users/SPILLAIP/Downloads/chromedriver_win32/chromedriver.exe")
wait = WebDriverWait(browser, 20)
browser.get(loginURL)
wait).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[@id='tradingview']")))
wait.until(EC.visibility_of_element_located((By.XPATH, "//span[contains(@class, 'pane-legend-item-value__main')]")))
time.sleep(0.3) #short pause added to make sure that all the relevant elements are loaded, not only the first one
nifty_bank_values_xpath = browser.find_elements(By.XPATH, "//span[contains(@class, 'pane-legend-item-value__main')]")
print("Len of nifty_bank_values_xpath: ",len(nifty_bank_values_xpath))
完成 iframe 內的元素處理后,要切換到默認內容,您需要執行
driver.switch_to.default_content()
uj5u.com熱心網友回復:
感謝@Prophet 指導我探索iframe。
browser.get(niftybankchartURL)
time.sleep(10)
# jump into iframe
browser.switch_to.frame(browser.find_element_by_tag_name("iframe"))
一旦switch_to進入框架,XPATH作業正常。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/442315.html
