【问题标题】:Selenium WebDriver get element SCREEN CoordinatesSelenium WebDriver 获取元素 SCREEN 坐标
【发布时间】:2019-02-04 09:08:16
【问题描述】:

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

【问题讨论】:

  • 找到具有 .size 和 .location 属性的元素后。
  • 我使用 .getLocation .getSize 如何帮助我?
  • 我不明白你为什么除了 WebDriver 之外还使用机器人,但你为什么不开始分割线 robot.mouseMove(driver.findElement(By.xpath(xpath)).getLocation ().getX(),driver.findElement(By.xpath(xpath)).getLocation().getY());看看你能找到元素,然后返回位置,这样你就可以找到确切的问题。
  • 我确实使用了 .getLocation,它返回的值似乎不是屏幕尺寸,而是没有标签和栏的浏览器

标签: java selenium-webdriver point awtrobot


【解决方案1】:

我不太清楚你为什么要做你正在做的事情,但这里有一个 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()

【讨论】:

    猜你喜欢
    • 2017-08-05
    • 2021-07-04
    • 1970-01-01
    • 2013-03-08
    • 2012-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多