Home > Blockchain >  How to pick the element using given screen coordinates - selenium, java
How to pick the element using given screen coordinates - selenium, java

Time:12-17

I need to know what is the element loaded to the given screen coordinates.
There are dynamic elements loaded in my web page's specific location (top left of the screen).
So I need to know what is the element loaded in the given screen area. (please refer below pic) And log the time and element details.

enter image description here

I'm using selenium and java and can use 'JavascriptExecutor' if needed.
So any idea how to get these elements by screen coordinates?

CodePudding user response:

Code:

package selenium;

import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.util.ArrayList;
import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

public class CoordinateTest extends WebDriverSetup {

    public static void main(String[] args) {
        
         // wrapped driver init
        WebDriver driver = startChromeDriver();
        
        driver.get("https://stackoverflow.com/");
        
        // get screen resolution
        Dimension screenDimension = Toolkit.getDefaultToolkit().getScreenSize();
        int screenWidth = (int) screenDimension.getWidth();
        int screenHeight = (int) screenDimension.getHeight();

        // desired rectangle
        Rectangle r = new Rectangle(200, 0, screenWidth / 2, screenHeight / 10);
        
        List<WebElement> rElements = getElementsFromRectangle(driver, r);
        for (WebElement element: rElements) {
            System.out.println("tag: "   element.getTagName());
            System.out.println("location: "   element.getLocation());
            System.out.println("text: "   element.getText()   System.lineSeparator());
        }
        driver.quit();
    }
    
    public static List<WebElement> getElementsFromRectangle(WebDriver driver, Rectangle r) {
        List<WebElement> rElements = new ArrayList<WebElement>();
        List<WebElement> allElements = driver.findElements(By.xpath("//*"));
        for (WebElement element: allElements) {
            if (elementIsInsideRectangle(element, r)) {
                rElements.add(element);
            }
        }
        return rElements;
    }
    
    public static Boolean elementIsInsideRectangle(WebElement element, Rectangle r) {
        int x = element.getRect().getX();
        int y = element.getRect().getY();
        int w = element.getRect().getWidth();
        int h = element.getRect().getHeight();
        int rMinX = (int) r.getMinX();
        int rMaxX = (int) r.getMaxX();
        int rMinY = (int) r.getMinY();
        int rMaxY = (int) r.getMaxY();
        if (x >= rMinX && x <= rMaxX && y >= rMinY && y <= rMaxY && x   h <= rMinX   rMaxX && y   w <= rMinY   rMaxY) {
            return true;
        }
        else {
            return false;
        }
    }
}

Output:

Starting ChromeDriver 96.0.4664.45 (76e4c1bb2ab4671b8beba3444e61c0f17584b2fc-refs/branch-heads/4664@{#947}) on port 29403
Only local connections are allowed.
Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe.
ChromeDriver was started successfully.
Pro 16, 2021 2:50:59 ODP. org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C
tag: a
location: (319, 3)
text: 

tag: span
location: (332, 25)
text: 

tag: li
location: (531, 12)
text: About

tag: a
location: (531, 12)
text: About

tag: li
location: (602, 12)
text: Products

tag: a
location: (602, 12)
text: Products

tag: li
location: (689, 12)
text: For Teams

tag: a
location: (689, 12)
text: For Teams

tag: svg
location: (802, 17)
text: 

tag: path
location: (802, 17)
text: 
  • Related