在Selenium中显示当前光标位置

时间:2014-04-04 09:38:09

标签: python flash testing selenium selenium-webdriver

有没有办法显示当前光标位置或类似的东西?我有动作链,应该点击某个对象的确切点,但我想我已经选择了错误的坐标。我使用Firefox webdriver。 这是脚本的样子:

from selenium import webdriver
import time
from selenium.webdriver.common.action_chains import ActionChains

driver = webdriver.Firefox()
driver.get("http://www.mysite/")
elem = driver.find_element_by_xpath('//div[@id="player"][1]/object[@type="application/x-shockwave-flash"]')
action_chains = ActionChains(driver)
action_chains.move_to_element_with_offset(elem, 660, 420).perform()
action_chains.click().perform()
time.sleep(10)
driver.close()

5 个答案:

答案 0 :(得分:6)

我有一个不透明的HTML5画布,我必须在不同的偏移处检查工具提示的值,以及有什么问题!经过大量的敲击,我学到了几件重要的事情:

  1. move_to_element_by_offset在2.42之前有问题。另请参阅https://code.google.com/p/selenium/issues/detail?id=4215

  2. 即使在更新后,move_to_element_by_offset仍然显得不稳定。我切换到ActionChains move_to_element悬停(默认为元素中心)和第二个ActionChainsmove_by_offset

  3. 的组合
  4. 现在您处于适当位置,重新请求位置敏感元素。就我而言,那是一个工具提示对象。这个selenium.webdriver.remote.element会有.location['x'].location['y']个成员,您需要仔细检查自己的位置。

  5. perform' d ActionChain悬停之后抓住光标位置不起作用的事情,以节省每个人的时间:

      传递给.location['x']的元素上的
    • .location['y']move_to_element。这些值未由ActionChains更新。
    • selenium.webdriver.remote.webdriver switch_to_active_element;我不清楚哪些元素符合"活跃",但它不会将我们的工具提示确认为活动元素,并且给了我[0,0]坐标。
    • selenium.webdriver.remote.webdriver get_window_position;文档有点模糊,但这正是函数建议的内容:窗口而不是光标位置。

答案 1 :(得分:3)

是的,这个问题令人沮丧。

这个方法可以让你通过在鼠标上画一个红点来查看它的位置: enter image description here 首先将以下 CSS 临时添加到您的网页:

// TODO: Remove me after you've figured out where the mouse is moving to.
.dot {
  background: red;
  position: absolute;
  width: 2px;
  height: 2px;
  z-index: 10000;
}

然后,在您的代码中,就在您调用 *.moveTo() 方法之前,添加一个暂停以允许您将代码注入浏览器的控制台。此代码使用 JavaScript (JS / webdriver.io),但可以轻松添加到任何语言中:

// Other test stuff...

console.log("This gives you 10 seconds to inject the code..."); // <-- Add this and the next line
browser.pause(10000);

browser.moveTo(...gawdKnowsWhere);

现在,当您看到测试命令行控制台等待 10 秒钟时,打开 Chrome/Firefox 的开发人员工具 (F12) 并将此代码段粘贴到浏览器的“控制台”选项卡中:

  (function() {
    "use strict";

    document.onmousemove = handleMouseMove;
    function handleMouseMove(event) {
      var dot, eventDoc, doc, body, pageX, pageY;
      
      event = event || window.event; // IE-ism
      
      // If pageX/Y aren't available and clientX/Y
      // are, calculate pageX/Y - logic taken from jQuery
            // Calculate pageX/Y if missing and clientX/Y available
      if (event.pageX == null && event.clientX != null) {
        eventDoc = (event.target && event.target.ownerDocument) || document;
        doc = eventDoc.documentElement;
        body = eventDoc.body;

        event.pageX = event.clientX +
          (doc && doc.scrollLeft || body && body.scrollLeft || 0) -
          (doc && doc.clientLeft || body && body.clientLeft || 0);
        event.pageY = event.clientY +
          (doc && doc.scrollTop  || body && body.scrollTop  || 0) -
          (doc && doc.clientTop  || body && body.clientTop  || 0 );
      }

      // Add a dot to follow the cursor
      dot = document.createElement('div');
      dot.className = "dot";
      dot.style.left = event.pageX + "px";
      dot.style.top = event.pageY + "px";
      document.body.appendChild(dot);
    }
  })();

现在,去寻找一个红点。无论鼠标移到哪里,它将无法点击那里,因为现在有一个红色方块挡住了路。但至少你可以看到它试图去哪里。

看不到点?

一些提示:

  • 通过修改宽度和高度使 CSS 中的点变大。
  • 在屏幕上移动鼠标并观察所有红点。当您移动鼠标时,它们是否出现?
  • 确保您可以在您期望的位置绘制一个红点。 z 顺序可能不够高,等等。

答案 2 :(得分:0)

我没有看到任何可能直接更改分辨率的方法,因为我们无法使用任何定位器。

对于您的JWPlayer示例,我确信可能会有一些javascript代码段允许更改分辨率。使用Selenium JavascriptExecutor,您可以运行该javacript代码来模拟分辨率更改..但不是通过单击光标。

WebDriver driver = new ChromeDriver();

if (driver instanceof JavascriptExecutor) {
    ((JavascriptExecutor) driver).executeScript(YOUR_JAVA_SCRIPT_STRING);
}

我注意到您提到您不想使用任何原生JWPlayer API。我认为这是唯一可行的解​​决方案。

答案 3 :(得分:0)

对于我来说,作为一个硒的新手有点难以理解,它只是无法在flash对象上产生点击。我在这里看到的唯一可行解决方案是使用sikuli-script

我在GitHub上找到了这个项目:https://github.com/kevlened/sikuli_cpython。它允许在纯python中使用sikuli脚本(用jython编写),所以我想我可以编写一些sikuli脚本并将它们与selenium测试集成。

答案 4 :(得分:0)

如果您只想确认 Selenium 是否点击了正确的元素,而不是对元素进行 click()-ing:

actions.move_to_element(my_element).click().perform()

context_click() 就可以了:

actions.move_to_element(my_element).context_click().perform()

Selenium 将右键单击您的元素,这将导致上下文菜单显示在光标的右下角。一旦你看到上下文菜单的位置,你就会知道 Selenium 是否点击了正确的元素。

相关问题