代码试验:
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import org.testng.annotations.Test;
public class DragDrop {
WebDriver driver;
@Test
public void dragdrop() throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "./Drivers/chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://dhtmlx.com/docs/products/dhtmlxTree/");
//Bring the element to focus
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("scroll(0,600)");
Actions act = new Actions(driver);
Thread.sleep(5000);
//driver.findElement(By.xpath("//span[text()='Nancy Atherton']//parent::div//parent::li"));
//The below web element is where the issue is and unable to locate the web element
WebElement allBooks = driver.findElement(By.xpath("//span[text()='Nancy Atherton']"));
WebElement myLib = driver.findElement(By.xpath("//ul[@role='listbox']"));
act.dragAndDrop(allBooks, myLib).perform();
}
}
错误信息:
FAILED: dragdrop
org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//span[text()='Nancy Atherton']"}
(Session info: chrome=109.0.5414.75)
For documentation on this error, please visit: https://selenium.dev/exceptions/#no_such_element
我无法找到元素,即使尝试不同的定位器,如xpath, css等。我还注意到有iframes标签呈现在层次结构中。我甚至尝试通过识别和移动到特定的iframe,然后定位元素,但它没有帮助。
如果有人能看一下程序,告诉我哪里出错了,我将非常感激。所需元素在iframe所以你必须:
-
诱导WebDriverWait等待所需的frameToBeAvailableAndSwitchToIt。
-
诱导WebDriverWait等待所需的elementToBeClickable.
-
您可以使用以下定位器策略:
driver.get("https://dhtmlx.com/docs/products/dhtmlxTree/"); WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(5)); wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.cssSelector("iframe[src*='//dhtmlxcode.com']"))); WebElement books = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[@class='dhx_tree-label' and text()='All Books']"))); JavascriptExecutor js = (JavascriptExecutor)driver; js.executeScript("return arguments[0].scrollIntoView(true)", books); WebElement dragMe = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//span[@class='dhx_tree-list-item__text' and text()='Nancy Atherton']"))); WebElement dropHere = wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("div#treeTarget>ul"))); Actions actions = new Actions(driver); actions.dragAndDrop(dragMe, dropHere).build().perform();
public class DragDrop {
WebDriver driver;
@Test(description="This is drag and drop scenario..")
public void dragdrop() throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "./Drivers/chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://dhtmlx.com/docs/products/dhtmlxTree/");
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("scroll(0,500)");
WebElement frame_ele = driver.findElement(By.cssSelector("iframe[src*='//dhtmlxcode.com']"));
driver.switchTo().frame(frame_ele);
WebElement allBooks = driver.findElement(By.xpath("//span[text()='Nancy Atherton']"));
WebElement myLib = driver.findElement(By.xpath("//ul[@role='listbox']"));
Actions act = new Actions(driver);
act.dragAndDrop(allBooks, myLib).perform();
Thread.sleep(3000);
driver.quit();
}
}