【发布时间】: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()
代码:
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