0

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.

Prophet
  • 32,350
  • 22
  • 54
  • 79

2 Answers2

0

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();
    }
}
Prophet
  • 32,350
  • 22
  • 54
  • 79
  • Tried this solutions but now I get the timeoutException, I even changed the wait to 30, but it just can't find that element. Caused by: org.openqa.selenium.NoSuchElementException: Unable to locate element: //span[text()='Europe'] – Pēteris Spura Dec 18 '21 at 21:54
  • I'm sorry. That element is inside an iframe. So you have to switch to that iframe first in order to access this element. See my updated answer. – Prophet Dec 18 '21 at 22:33
  • The updated version worked, but as the upper answer, if the Europe button is clicked, it needs a double click to open the tree. Is there no way to trigger the triangle expansion button, then it would only take one click. – Pēteris Spura Dec 18 '21 at 22:43
0

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();
    }
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352