可以使用 Java 和 Selenium 来通过像素坐标(CSS 位置)来定位元素。

huangapple go评论64阅读模式
英文:

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)`

3146214是我想要将第一个重新排序图标拖动到的位置
英文:
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:

  1. In CSS selector "dynamic" means those generated dynamically so that I don't want to use it.
  2. But to perform the act.dragAndDropBy I need a WebElement parameter:

dragAndDrop(source, target)

dragAndDropBy(source, xOffset, yOffset)

  1. 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();

huangapple
  • 本文由 发表于 2020年8月10日 14:56:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/63335463.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定