英文:
Can I locate element by pixel (css position)? Java, Selenium
问题
public void ReorderColumns() {
// 需要拖拽的元素。
WebElement From = driver.findElement(By.cssSelector(
"body > div:nth-child(3) > div > div.dynamic > div > div:nth-child(2) > div > div.dynamic.dynamic > svg"));
Actions act = new Actions(driver);
// 通过像素拖拽。
act.dragAndDropBy(From, 146, 214).build().perform();
}
我需要拖动并放置一个图标以重新排列网页上的列标题,每个标题都有自己的重新排序图标,并且无法通过任何元素定位方法(xpath/CSS选择器/link等)来指定它。
是否有办法按像素(CSS位置)来定位元素?
上面是我的代码:
1)在CSS选择器中,“dynamic”表示动态生成的元素,因此我不想使用它。
2)但要执行`act.dragAndDropBy`,我需要一个WebElement参数:
`dragAndDrop(source, target)`
`dragAndDropBy(source, xOffset, yOffset)`
3)146、214是我想要将第一个重新排序图标拖动到的位置。
英文:
public void ReorderColumns() {
// Element which needs to drag.
WebElement From = driver.findElement(By.cssSelector(
"body > div:nth-child(3) > div > div.dynamic > div > div:nth-child(2) > div > div.dynamic.dynamic > svg"));
Actions act = new Actions(driver);
// Drag and Drop by Pixel.
act.dragAndDropBy(From, 146, 214).build().perform(); }
I need to drag and drop an icon to reorder the column header on the web page, each header has its own reorder icon and there is no way to specify it by any Element locate methods(xpath/CSS selector/link..etc)
Is there any way to locate elements by the pixel(CSS position)?
Above is my code:
- In CSS selector "dynamic" means those generated dynamically so that I don't want to use it.
- But to perform the
act.dragAndDropBy
I need a WebElement parameter:
dragAndDrop(source, target)
dragAndDropBy(source, xOffset, yOffset)
- 146, 214 is the position I want to drag the first reorder icon to.
答案1
得分: 2
我认为这可以帮助您解决问题:
WebElement a = driver.findElement(By.cssSelector("your_selector"));
WebElement b = driver.findElement(By.cssSelector("your_selector"));
int x = b.getLocation().x;
int y = b.getLocation().y;
Actions actions = new Actions(driver);
actions.moveToElement(a)
.pause(Duration.ofSeconds(1))
.clickAndHold(a)
.pause(Duration.ofSeconds(1))
.moveByOffset(x, y)
.moveToElement(b)
.moveByOffset(x, y)
.pause(Duration.ofSeconds(1))
.release().build().perform();
英文:
I think this can help you to solve the problem:
WebElement a = driver.findElement(By.cssSelector("your_selector"));
WebElement b = driver.findElement(By.cssSelector("your_selector"));
int x = b.getLocation().x;
int y = b.getLocation().y;
Actions actions = new Actions(driver);
actions.moveToElement(a)
.pause(Duration.ofSeconds(1))
.clickAndHold(a)
.pause(Duration.ofSeconds(1))
.moveByOffset(x, y)
.moveToElement(b)
.moveByOffset(x,y)
.pause(Duration.ofSeconds(1))
.release().build().perform();
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论