Clicking at coordinates without identifying element

前端 未结 14 1897
醉酒成梦
醉酒成梦 2020-11-28 07:49

As part of my Selenium test for a login function, I would like to click a button by identifying its coordinates and instructing Selenium to click at those coordinates. This

相关标签:
14条回答
  • 2020-11-28 08:05

    I used AutoIt to do it.

    using AutoIt;
    AutoItX.MouseClick("LEFT",150,150,1,0);//1: click once, 0: Move instantaneous
    
    1. Pro:
      • simple
      • regardless of mouse movement
    2. Con:
      • since coordinate is screen-based, there should be some caution if the app scales.
      • the drive won't know when the app finish with clicking consequence actions. There should be a waiting period.
    0 讨论(0)
  • 2020-11-28 08:06

    I first used the JavaScript code, it worked amazingly until a website did not click.

    So I've found this solution:

    First, import ActionChains for Python & active it:

    from selenium.webdriver.common.action_chains import ActionChains
    actions = ActionChains(driver)
    

    To click on a specific point in your sessions use this:

    actions.move_by_offset(X coordinates, Y coordinates).click().perform()
    

    NOTE: The code above will only work if the mouse has not been touched, to reset the mouse coordinates use this:

    actions.move_to_element_with_offset(driver.find_element_by_tag_name('body'), 0,0))
    

    In Full:

    actions.move_to_element_with_offset(driver.find_element_by_tag_name('body'), 0,0)
    actions.move_by_offset(X coordinates, Y coordinates).click().perform()
    
    0 讨论(0)
  • 2020-11-28 08:06

    This can be done using Actions class in java

    Use following code -

    new Actions(driver).moveByOffset(x coordinate, y coordinate).click().build().perform(); 
    

    Note: Selenium 3 doesn't support Actions class for geckodriver

    Also, note that x and y co-ordinates are relative values from current mouse position. Assuming mouse co-ordinates are at (0,0) to start with, if you want to use absolute values, you can perform the below action immediately after you clicked on it using the above code.

    new Actions(driver).moveByOffset(-x coordinate, -y coordinate).perform();

    0 讨论(0)
  • 2020-11-28 08:10

    I used the Actions Class like many listed above, but what I found helpful was if I need find a relative position from the element I used Firefox Add-On Measurit to get the relative coordinates. For example:

            IWebDriver driver = new FirefoxDriver();
            driver.Url = @"https://scm.commerceinterface.com/accounts/login/?next=/remittance_center/";
            var target = driver.FindElement(By.Id("loginAsEU"));
            Actions builder = new Actions(driver);            
            builder.MoveToElement(target , -375  , -436).Click().Build().Perform();
    

    I got the -375, -436 from clicking on an element and then dragging backwards until I reached the point I needed to click. The coordinates that MeasureIT said I just subtracted. In my example above, the only element I had on the page that was clickable was the "loginAsEu" link. So I started from there.

    0 讨论(0)
  • 2020-11-28 08:11

    Action chains can be a little finicky. You could also achieve this by executing javascript.

    self.driver.execute_script('el = document.elementFromPoint(440, 120); el.click();')
    
    0 讨论(0)
  • 2020-11-28 08:15

    In Selenium Java, you can try it using Javascript:

    WebDriver driver = new ChromeDriver();
    
    if (driver instanceof JavascriptExecutor) {
    	((JavascriptExecutor) driver).executeScript("el = document.elementFromPoint(x-cordinate, y-cordinate); el.click();");
    }

    0 讨论(0)
提交回复
热议问题