在Selenium中显示当前光标位置

Dmi*_*lov 8 python testing flash 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()
Run Code Online (Sandbox Code Playgroud)

kim*_*noa 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悬停(默认为元素的中心)和第二个ActionChains的组合move_by_offset

  3. 现在您处于适当位置,重新请求位置敏感元素.就我而言,这是一个工具提示对象.这selenium.webdriver.remote.element将有你需要的成员.location['x'].location['y']理智检查你的位置.

perform'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 ; 文档有点模糊,但这正是函数建议的内容:窗口而不是光标位置.


bag*_*nyi 5

如果您想确认的是 Selenium 是否单击了正确的元素,而不是click()在元素上使用 -ing:

actions.move_to_element(my_element).click().perform()
Run Code Online (Sandbox Code Playgroud)

context_click()在上面:

actions.move_to_element(my_element).context_click().perform()
Run Code Online (Sandbox Code Playgroud)

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


Pur*_*rus 0

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

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

WebDriver driver = new ChromeDriver();

if (driver instanceof JavascriptExecutor) {
    ((JavascriptExecutor) driver).executeScript(YOUR_JAVA_SCRIPT_STRING);
}
Run Code Online (Sandbox Code Playgroud)

我注意到您提到您不想使用任何本机 JWPlayer API。我认为这是唯一可能的解决方案。