做雖然這個答案是不相關的硒,我認爲它可以幫助別人尋找一個答案這個問題。
我一直在這種情況之前,雖然不可能使用Selenium來「點擊」一個閃光按鈕,但是有一些東西比點擊那個按鈕還好,並繼續與你需要做的事情保持一致。
網頁上的任何Flash影片都像其他任何窗口一樣,只是不像其他任何元素那麼容易訪問,但是如果您足夠「低」,則可以枚舉屬於該瀏覽器實例的窗口並查找Flash窗口。
這樣的想法如下:
找到活動的瀏覽器實例(IE,FF,鉻等,在這種情況下IE)
var ps = Process.GetProcessesByName("iexplore").Select(p => p.Id);
foreach (var handle in ps.SelectMany(EnumerateProcessWindowHandles))
{
GetChildWindows(handle);
if (_macromediaFound != false){break;}
}
列出所有它的子窗口(EnumerateWindowsEx使用Windows API)
public static List<IntPtr> GetChildWindows(IntPtr parent)
{
List<IntPtr> result = new List<IntPtr>();
GCHandle listHandle = GCHandle.Alloc(result);
try
{
EnumWindowProc childProc = new EnumWindowProc(EnumWindow);
EnumChildWindows(parent, childProc, GCHandle.ToIntPtr(listHandle));
}
finally
{
if (listHandle.IsAllocated)
listHandle.Free();
}
return result;
}
找到具有窗口W的窗口第i個被稱爲MacromediaFlashPlayerActiveX類名(這是在IE)
前面的代碼將調用EnumWindow每次它找到一個子窗口上的每個進程清單當然對步驟的功能(方法)1
private static bool EnumWindow(IntPtr handle, IntPtr pointer)
{
StringBuilder message = new StringBuilder(1000);
SendMessage(handle, WmGettext, message.Capacity, message);
StringBuilder classN = new StringBuilder(1000);
GetClassName(handle, classN, classN.Capacity);
if (classN.ToString().Contains("MacromediaFlashPlayerActiveX"))
{ // continues below....
有了這些信息,您將擁有窗口的XY位置,您需要找到正確的XY來單擊屏幕上的按鈕。
Thread.Sleep(2000); // this will allow any needed time to actually draw the flash on screen
Rectangle rect = new Rectangle();
GetWindowRect(handle, ref rect);
var oldPos = Cursor.Position;
Point clientPoint = new Point(rect.X + 20, rect.Y + 10); // In this particular Flash Movie the Upload File button is 20 points from x and 10 points from Y)
if (rect.X != 0 && rect.Y != 0)
{
Cursor.Position = new Point(clientPoint.X, clientPoint.Y);
}
發送一個單擊事件到這些座標
mouse_event(MouseEventfLeftdown, 0, 0, 0, UIntPtr.Zero);
mouse_event(MouseEventfLeftup, 0, 0, 0, UIntPtr.Zero);
Thread.Sleep(2000);
// Options but "clean" return the mouse to it's original position
Cursor.Position = oldPos;
一旦點擊已發送尋的類名#32770(至少IE)
hwndTmp = (IntPtr)FindWindow("#32770", "Text of the upload window");
窗口
從這裏開始,繼續這個過程應該非常簡單。
我知道這不是最乾淨的解決方案(您需要一個登錄用戶,也有一個屏幕來獲取座標),但我知道有一種情況是有沒有替代這種情況和你需要爲它找到解決方案。
希望這會有所幫助。
謝謝Arran可以提供任何示例代碼或鏈接爲AutoIT單擊Flash對象... – selva