如何使用给定的屏幕坐标选择元素 - selenium,java
Posted
技术标签:
【中文标题】如何使用给定的屏幕坐标选择元素 - selenium,java【英文标题】:How to pick the element using given screen coordinates - selenium, java 【发布时间】:2022-01-19 13:09:12 【问题描述】:我需要知道加载到给定屏幕坐标的元素是什么。 我的网页的特定位置(屏幕左上角)加载了动态元素。 所以我需要知道给定屏幕区域中加载的元素是什么。 (请参考下图) 并记录时间和元素的详细信息。
我正在使用 selenium 和 java,如果需要可以使用 'javascriptExecutor'。 那么知道如何通过屏幕坐标获取这些元素吗?
【问题讨论】:
【参考方案1】:代码:
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://***.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;
输出:
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:
【讨论】:
以上是关于如何使用给定的屏幕坐标选择元素 - selenium,java的主要内容,如果未能解决你的问题,请参考以下文章