Home > Mobile >  Can't click on a treeview with Selenium and Java
Can't click on a treeview with Selenium and Java

Time:12-19

I'm basically new to automation testing, been using JAVA SELENIUM, Eclipse.

Been trying to open the "Europe" tree with the geckodriver.exe Webdriver. Been searching for solutions, but to no avail.

This is the page, that needs to be tested. https://js.devexpress.com/Demos/WidgetsGallery/Demo/Common/NavigationOverview/Angular/Light/

My code for opening the tree.

public class Test_suite {
    
    @Test
    public void testAssertFunctions() throws InterruptedException {
        System.setProperty("webdriver.firefox.driver", "C:\\selenium-3.141.59\\draiveri\\geckodriver.exe");
        WebDriver driver = new FirefoxDriver();
driver.navigate().to("https://js.devexpress.com/Demos/WidgetsGallery/Demo/Common/NavigationOverview/Angular/Light/");
        Thread.sleep(10000);
        WebElement fruits = driver.findElement(By.xpath("/html/body/div/demo-app/div/div[1]/dx-tree-view/div[2]/div/div/div[1]/ul/li[4]/div[2]"));
        fruits.click();
        }
    }

It seems I can't locate the element, have tried multiple findElement.by options, but to no help. Only solution that sort of worked was using Selenium IDE, which did the click to expand the tree, and it shows xpath=//li[4]/div[2], but after copying that path to eclipse, still does not find the element.

CodePudding user response:

Please try this:

public class Test_suite {
    @Test
    public void testAssertFunctions() throws InterruptedException {
    System.setProperty("webdriver.firefox.driver", "C:\\selenium- 
    3.141.59\\draiveri\\geckodriver.exe");
    WebDriver driver = new FirefoxDriver();
    driver.navigate().to("https://js.devexpress.com/Demos/WidgetsGallery/Demo/Common/NavigationOverview/Angular/Light/");
    WebDriverWait wait = new WebDriverWait(driver, 20);
    wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//iframe[@id='demoFrame']")));
    driver.switchTo().frame(driver.findElement(By.xpath("//iframe[@id='demoFrame']")));
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//span[text()='Europe']"))).click();
    }
}

CodePudding user response:

A couple of things:

  • With Selenium v3.x onward as using GeckoDriver is mandatory, so instead of:

    webdriver.firefox.driver
    

    we need to use:

    webdriver.gecko.driver
    
  • Instead of an absolute xpath you need to use a relative xpath.

  • The desired WebElement is also within an iframe

  • Ideally to click() on any clickable element you need to induce WebDriverWait for the elementToBeClickable() and you can use the following Locator Strategy.

  • Your effective code block will be:

    @Test
    public void testAssertFunctions() throws InterruptedException {
        System.setProperty("webdriver.gecko.driver", "C:\\selenium-3.141.59\\draiveri\\geckodriver.exe");
        WebDriver driver = new FirefoxDriver();
        driver.navigate().to("https://js.devexpress.com/Demos/WidgetsGallery/Demo/Common/NavigationOverview/Angular/Light/");
        new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//iframe[@class='demo-frame' and @name='demo-frame']")));
        WebElement element = new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//span[text()='Europe']")));
        new Actions(driver).moveToElement(element).doubleClick(element).build().perform();
    }
    
  • Related