嗨,我正在嘗試使用 C# selenium 來獲取 youtube 視頻標題。這些都行不通。我已經嘗試從其他元素獲取 .Text 并且它們作業正常。所以我縮小到我認為有問題的這些行。非常感謝您的幫助。很想知道我哪里出錯了。
var title = driver.FindElement(By.Id("title")).FindElement(By.ClassName("style-scope ytd-watch-metadata")).Text;
var title = driver.FindElement(By.CssSelector("h1.title yt-formatted-string")).Text;
uj5u.com熱心網友回復:
您的定位器不是唯一的。
嘗試這個:
var title = driver.FindElement(By.CssSelector("#info-contents h1.title yt-formatted-string")).Text;
style-scope ytd-watch-metadata實際上也是2 個類名:style-scope并且ytd-watch-metadatawhileBy.ClassName接受單個類名引數。對于多個類名,請使用 XPath 或 CSS 選擇器。
也不要忘記添加延遲,最好是WebDriverWait預期條件顯式等待。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/507736.html
