admin管理员组

文章数量:1317359

I'm new to test automation in Java with Selenium and the Cucumber plugin. I need to click on an object on a website without identifying xpath, but simply declaring its position on the horizontal and vertical axis of the screen. How can I do it?

How do I identify it by analyzing the page and what is the listing in Selenium with Cucumber?

I'm new to test automation in Java with Selenium and the Cucumber plugin. I need to click on an object on a website without identifying xpath, but simply declaring its position on the horizontal and vertical axis of the screen. How can I do it?

How do I identify it by analyzing the page and what is the listing in Selenium with Cucumber?

Share Improve this question edited Jan 29 at 9:05 Mark Rotteveel 109k229 gold badges156 silver badges220 bronze badges asked Jan 29 at 8:59 nowaySyncnowaySync 1152 silver badges13 bronze badges 2
  • 2 Clicking by X and Y is a very bad idea, because that is generally resolution and rendering engine dependent. If anything changes in the resolution or the rendering engine, your coordinates will be off. – Mark Rotteveel Commented Jan 29 at 9:06
  • Why do you feel like you have to click on coordinates instead of using a locator? – JeffC Commented Jan 29 at 14:50
Add a comment  | 

1 Answer 1

Reset to default 0

If you need to click on a specific position on a webpage without using XPath, you can do it using Selenium’s Actions class in Java. Simply move to the desired x, y coordinates and perform a click. Here's a basic example.

Actions actions = new Actions(driver);
actions.moveByOffset(500, 300).click().perform();

If you are using Cucumber framework, you can use below code,

@Then("I click on coordinates {int} and {int}")
public void iClickOnCoordinates(int x, int y) {
    Actions actions = new Actions(driver);
    actions.moveByOffset(x, y).click().perform();
}

本文标签: javaHow to click on an element based on the x and y axisStack Overflow