【问题标题】:move cursor and click with Selenium WebDriver ActionChains移动光标并单击 Selenium WebDriver ActionChains
【发布时间】:2017-11-27 13:02:41
【问题描述】:

我一直在寻找要在 BrowserStack 中使用的 ActionChain 函数。 我想从 Facebook 中删除一个应用程序。在我处理它之后,我意识到无法通过 find_element_by_id 和 find_element_by_name 等普通 selenium 函数找到元素并单击并删除应用程序。所以,我决定使用一些图像处理。我在页面中寻找应用程序的图标,并希望将光标移动到它上面。之后出现 X 按钮。当它出现时,我想点击它。我找到了图标的位置并使用 ActionChain 将光标移动到它上面。但是,光标停留在图标上的时间很短。并且 ActionChain 的点击功能不起作用。我在下面添加了我的代码。如果您能帮助我了解 ActionChain 的这些移动光标和单击功能,我会很高兴的。

图书馆:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
import time
import numpy as np
from PIL import Image
from skimage import data
from skimage.feature import match_template
from selenium.webdriver import ActionChains

ActionChains(driver).move_by_offset(xa,ya).click().perform()

the page I am working in

代码:

driver.get("https://www.facebook.com/settings?tab=applications")
time.sleep(5)
driver.save_screenshot('/Users/***/Desktop/ssfordelete.png')
ssfordelete = Image.open("/Users/***/Desktop/ssfordelete.png")
ssfordeleteGray = ssfordelete.convert('L')
ssfordeleteGrayNp = np.asarray(ssfordeleteGray)

allow = Image.open("/Users/***/Desktop/browserStack/allow.png")
allowGray = allow.convert('L')
allowGrayNp = np.asarray(allowGray)
resultdelete1 = match_template(ssfordeleteGrayNp, allowGrayNp)
dij = np.unravel_index(np.argmax(resultdelete1), resultdelete1.shape)
sizeofallowx=allowGrayNp.shape[0]
sizeofallowy=allowGrayNp.shape[1]
xa, ya = dij[::-1]
xa = xa + (sizeofallowx/2)
ya = ya + (sizeofallowy/2)
print (xa, ya)
while cntcnt<times:
    temp1 = Image.open("/Users/evrozm/Desktop/browserStack/{}icon.png".format(games[cntcnt]))
    temp1Gray = temp1.convert('L')
    temp1GrayNp = np.asarray(temp1Gray)
    resultdelete1 = match_template(ssfordeleteGrayNp, temp1GrayNp)
    dij = np.unravel_index(np.argmax(resultdelete1), resultdelete1.shape)
    sizeoftemp1y=temp1GrayNp.shape[0]
    sizeoftemp1x=temp1GrayNp.shape[1]
    xd1, yd1 = dij[::-1]
    xd1 = xd1 + (sizeoftemp1x/2)
    yd1 = yd1 + (sizeoftemp1y/2)
    print (xd1, yd1)
    ActionChains(driver).move_by_offset(xd1,yd1).perform()

    temp2 = Image.open("/Users/evrozm/Desktop/browserStack/delete.png")
    temp2Gray = temp2.convert('L')
    temp2GrayNp = np.asarray(temp2Gray)
    resultdelete2 = match_template(ssfordeleteGrayNp, temp2GrayNp)
    dij = np.unravel_index(np.argmax(resultdelete2), resultdelete2.shape)
    xd2, yd2 = dij[::-1]
    sizeoftemp2y=temp1GrayNp.shape[0]
    sizeoftemp2x=temp1GrayNp.shape[1]
    xd2, yd2 = dij[::-1]
    xd2 = xd2 + (sizeoftemp2x/2)
    yd2 = yd2 + (sizeoftemp2y/2)
    print (xd2, yd2)

    ActionChains(driver).move_by_offset(xd2,yd2).click().perform()
    cntcnt = cntcnt+1

【问题讨论】:

    标签: python-3.x image-processing selenium-webdriver


    【解决方案1】:

    我不擅长 Python,但我希望以下文档的第 7.2 节(动作链)对 http://selenium-python.readthedocs.io/api.html

    有所帮助

    可能是这样的:move_to_element_with_offset(to_element, xoffset, yoffset) 然后点击

    【讨论】:

    • 我使用了该链接中的信息。而不是移动到具有偏移量的元素,我直接想将光标移动到偏移量。这就是我使用 move_by_offset 的原因。但是,我仍然感谢您愿意提供帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-02
    • 1970-01-01
    • 2018-07-24
    • 2021-03-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多