How to click on a specific position of a web element in selenium?

I am working on automating a video player. I'm having difficulty pressing the progress bar of the video player at the end.
Consider the youtube video example. using xpath, I was able to recognize the element of the video progress indicator. Now I wanted to click at the end of the video.

I tried using moveToElement (ele, xOffset, yOffset) . But I can’t have a static offset here, since the size of the video player depends on the size of the browser window. I tried to get the size of the web element and use it as xoffset, but this does not seem to work. moveToElement (ele, ele.getRect (). getWidth () - 10, yOffset) . Any suggestions here on how to do this?

+4
source share
3 answers

Try this code. I tried using Chrome 54 with webdriver 2.53 on windows 8. Do not touch the mouse during the test, or even better, move the cursor out of the screen range when starting the test.

   WebElement elem = driver.findElement(By.className("ytp-progress-bar"));

    int width = elem.getSize().getWidth();

    Actions act = new Actions(driver);
    act.moveToElement(elem).moveByOffset((width/2)-2, 0).click().perform();

You can find the offset from many attributes in the div 'ytp-progress-bar'. You do not need to look for width, etc.

First you must go to the progress bar, because it disappears after 2-3 seconds due to inactivity on the video screen.

+6
source

Sometimes Selenium is not enough, and you need to work with Javascript and JQuery. I'm not a Java developer, but the code in C # will look like this:

IJavaScriptExecutor js = (IJavaScriptExecutor)driver;
js.ExecuteScript("Your Script");

With jQuery there will be more help. I hope I could help you.

0

To get the exact location to click on the page, there is a free Firefox add-on to help you achieve this accuracy. It is called "MeasureIt". In doing so, you can measure the exact vertical and horizontal distances that you want to click

0
source

Source: https://habr.com/ru/post/1660482/


All Articles