Reputation: 33
I want to get the on screen coordinates of a WebElement and use the robot class to click on it.
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);
}
It think the problem is that the coordinates passed in the mousePress method do not contain the firefox tabs,url bar etc. Is that really my problem? and if so how do I solve it? Thanks in advance!
Upvotes: 3
Views: 10076
Reputation: 601
It is not really clear to me why are you are doing what you are doing but here is a python script that gives some examples that might help
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()
Upvotes: 1