Home > Software engineering >  Why does Selenium find element by xpath work in Python and error in Java?
Why does Selenium find element by xpath work in Python and error in Java?

Time:10-29

I made a project with Selenium in python that works and successfully finds an element from the website https://ethermine.org/miners/0fB3583c11320BB9c7F512e06ce9c3A9218568C9/dashboard.

python code

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"""))

When trying to recreate the same thing in java it gives the error: "Exception in thread "main" org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element"

java code

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")));

    }
}

CodePudding user response:

try using better 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")));

}

Output : [[ChromeDriver: chrome on WINDOWS (c4351687a4e43f9e1bf73cc6dccdb73d)] -> xpath: //div[@class='active table-container']//tbody]

CodePudding user response:

Try using this

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")));
  • Related