Selenium WebDriver获取元素SCREEN坐标

问题描述 投票:0回答:1

我想获取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 bar等。这真的是我的问题吗?如果是这样我该如何解决?提前致谢!

java selenium-webdriver point awtrobot
1个回答
0
投票

我不清楚为什么你正在做你正在做的事情但是这里有一个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()
© www.soinside.com 2019 - 2024. All rights reserved.