我想從這個頁面上抓取鏈接。 https://www.youtube.com/results?search_query=food recepies
WebElement w1 = driver.findElement(By.xpath("//a[@id=\"video-title\"]"));
WebElement w2 = driver.findElement(By.xpath("//*a[@id='video-title']"));
WebElement w3 = driver.findElement(By.xpath("//a[@class='yt-simple-endpoint style-scope ytd-video-renderer']"));
WebElement w4 = driver.findElement(By.xpath("//h3/a"));
String link = w1.getAttribute("href");
這些都不適合我。
Error: Unable to locate element: //a[@id="video-title"]
uj5u.com熱心網友回復:
這應該適用于標題。
driver.findElement(By.xpath("//a[@id='video-title']"));
但是,其中有 27 個。所以,你可能想使用
List<WebElement> plants = driver.findElements(By.xpath("//a[@id='video-title']"));
另一件事是您可能希望等到頁面上的元素可見、啟用、可點擊等。為此,您需要執行以下操作:
WebElement firstResult = new WebDriverWait(driver, Duration.ofSeconds(10))
.until(ExpectedConditions.elementToBeVisible(By.xpath("//a[@id='video-title']")));
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/438443.html
