无法使用 Selenium 和 Java 单击树视图

Can't click on a treeview with Selenium and Java

我基本上是自动化测试的新手,一直在使用 JAVA+SELENIUM、Eclipse。

一直在尝试使用 geckodriver.exe Webdriver 打开“欧洲”树。一直在寻找解决方案,但无济于事。

这是需要测试的页面。 https://js.devexpress.com/Demos/WidgetsGallery/Demo/Common/NavigationOverview/Angular/Light/

我的开树代码。

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();
        }
    }

我似乎找不到该元素,尝试了多个 findElement.by 选项,但无济于事。唯一可行的解​​决方案是使用 Selenium IDE,它单击以展开树,并显示 xpath=//li[4]/div[2],但将该路径复制到 Eclipse 后,仍然找不到该元素。

请试试这个:

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();
    }
}

几件事:

  • v3.x onward as using GeckoDriver 是强制性的,所以而不是:

    webdriver.firefox.driver
    

    我们需要使用:

    webdriver.gecko.driver
    
  • 您需要使用相对xpath.

    而不是绝对xpath
  • 想要的 is also within an

  • 最好是 on any clickable element you need to induce for the elementToBeClickable() and you can use the following .

  • 您的有效代码块将是:

    @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();
    }