我在 python 中使用 Selenium 創建了一個專案,該專案有效并成功地從網站https://ethermine.org/miners/0fB3583c11320BB9c7F512e06ce9c3A9218568C9/dashboard 中找到了一個元素。
蟒蛇代碼
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from time import sleep
driver = webdriver.Chrome(ChromeDriverManager().install())
driver.get("https://ethermine.org/miners/0fB3583c11320BB9c7F512e06ce9c3A9218568C9/dashboard")
sleep(5)
print(driver.find_element_by_xpath("""//*[@id="app"]/div[4]/main/div/div[2]/div/div[2]/div[3]/div/div[2]/div/div/table/tbody"""))
當嘗試在 java 中重新創建相同的東西時,它會給出錯誤:“執行緒“main”org.openqa.selenium.NoSuchElementException 中的例外:沒有這樣的元素:無法定位元素”
代碼
package com.traptricker;
import org.openqa.selenium.By;
import org.openqa.selenium.chrome.ChromeDriver;
public class Main {
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "chromedriver.exe");
ChromeDriver driver = new ChromeDriver();
driver.get("https://ethermine.org/miners/0fB3583c11320BB9c7F512e06ce9c3A9218568C9/dashboard");
Thread.sleep(5000);
System.out.println(driver.findElement(By.xpath("//*[@id=\"app\"]/div[4]/main/div/div[2]/div/div[2]/div[3]/div/div[2]/div/div/table/tbody")));
}
}
uj5u.com熱心網友回復:
嘗試使用更好的 xpath ://div[@class='active table-container']//tbody
public static void main(String[] args) throws InterruptedException {
WebDriverManager.chromedriver().setup();
ChromeDriver driver = new ChromeDriver();
driver.get("https://ethermine.org/miners/0fB3583c11320BB9c7F512e06ce9c3A9218568C9/dashboard");
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
// System.out.println(driver.findElement(By.xpath("//*[@id=\"app\"]/div[4]/main/div/div[2]/div/div[2]/div[3]/div/div[2]/div/div/table/tbody")));
System.out.println(driver.findElement(By.xpath("//div[@class='active table-container']//tbody")));
}
輸出:[[ChromeDriver: WINDOWS 上的 chrome (c4351687a4e43f9e1bf73cc6dccdb73d)] -> xpath: //div[@class='active table-container']//tbody]
uj5u.com熱心網友回復:
嘗試使用這個
System.out.println(driver.findElement(By.xpath("//*[@id='app']/div[4]/main/div/div[2]/div/div[2]/div[3]/div/div[2]/div/div/table/tbody")));
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/340882.html
