我基本上是自動化測驗的新手,一直在使用 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 后,仍然找不到該元素。
uj5u.com熱心網友回復:
請試試這個:
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();
}
}
uj5u.com熱心網友回復:
幾件事:
從Selenium v3.x開始,因為使用GeckoDriver是強制性的,所以不是:
webdriver.firefox.driver我們需要使用:
webdriver.gecko.driver您需要使用相對 xpath而不是絕對xpath。
所需的WebElement也在iframe 中
理想的情況下,以點擊()任何在點擊你需要引起元素WebDriverWait的
elementToBeClickable(),你可以使用下面的定位策略。您的有效代碼塊將是:
@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(); }
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/385887.html
