gpt4 book ai didi

java - Selenium WebDriver 获取元素屏幕坐标

转载 作者:行者123 更新时间:2023-12-01 06:03:15 25 4
gpt4 key购买 nike

我想获取 WebElement 的屏幕坐标并使用机器人类来单击它。

SeleniumMethods sl= new SeleniumMethods();
WebDriver driver = new FirefoxDriver();
public void example () throws Exception{
driver.get("http://www.example.com/");
driver.manage().window().maximize();
//Xpath to more Info Link
String xpath = "/html/body/div/p[2]/a";
Robot robot = new Robot();
//Pass in the X and Y Coordinates of the Element (Integer)
robot.mouseMove(driver.findElement(By.xpath(xpath)).getLocation().getX(),driver.findElement(By.xpath(xpath)).getLocation().getY());
robot.mousePress(InputEvent.BUTTON1_MASK);
Thread.sleep(50);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
}

我认为问题在于 mousePress 方法中传递的坐标不包含 firefox 选项卡、url 栏等。这真的是我的问题吗?如果是这样我该如何解决?提前致谢!

最佳答案

我不太清楚你为什么要做你正在做的事情,但这里有一个 python 脚本,它提供了一些可能有帮助的示例

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains


driver = webdriver.Chrome()
url = "https://learn.letskodeit.com/p/practice"
driver.get(url)

el = driver.find_element_by_id("openwindow")
#in devtools you can see the elements x,y and compare to:
print("location:", el.location)
print("size", el.size)

#you can just now say el.click() but if you must move:
action = ActionChains(driver)
action.move_to_element(el)
action.click()
action.perform()

关于java - Selenium WebDriver 获取元素屏幕坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52079782/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com