我的代码打开了2个子窗口。一旦我对每个操作执行操作,我需要关闭窗口并切换回父窗口。
没有选项作为driver.switchToParentWindow。 只有driver.switchToPreviousWindow。
例如:我关闭了第二个子窗口 - >然后driver.switchToPreviousWindow将控制权切换回第一个子窗口,但当我关闭此窗口并执行driver.SwitchToPreviousWindow时,它会搜索最近关闭的第二个子窗口,而我希望它将控制权切换到父窗口。
我曾尝试到处寻找解决方案,但我似乎找不到使用Selenium VBA切换回Parent窗口。
以下是我的代码:
For a = 9 To LastRow
If Wb.Sheets(DestName).Cells(a, 3).Text = "Report Name" Then
'Checking if cell has 'Report Name'
StoreFile = Wb.Sheets(DestName).Cells(a, 4).Text
Debug.Print StoreFile
'Click on Report
Set myelement = driver.FindElementByLinkText(StoreFile) 'Click on report by name
Debug.Print myelement.Text
If myelement Is Nothing Then
GoTo endTry
ElseIf StoreFile = "CBD_Yoplait" Then
StoreFile = "CBD_Yoplait" & ".Category Buyer Dynamic"
Debug.Print StoreFile
Set myelement = driver.FindElementByLinkText(StoreFile)
myelement.Click
Else
myelement.Click
End If
'1st child window opens
driver.SwitchToNextWindow
Application.Wait (Now + TimeValue("0:0:07"))
'Click on 'Report Home'
Set myelement = driver.FindElementByXPath("//*
[@id=""ribbonToolbarTabsListContainer""]/div[1]/table/tbody/tr/td[3]")
If myelement Is Nothing Then
MsgBox ("no element found")
Else
myelement.Click
End If
'Click on 'Export'
Set myelement = driver.FindElementByXPath("//*
[@id=""RptHomeMenu_""]/tbody/tr/td/div/div[16]/a/div[2]")
If myelement Is Nothing Then
MsgBox ("no element found")
Else
myelement.Click
End If
'Click on 'Excel with Formatting'
Set myelement = driver.FindElementByXPath("//*
[@id=""RptHomeExportMenu_WEB-
INFxmllayoutsblocksHomeExportMenuLayoutxml""]
/tbody/tr/td/div/div[8]/a/div[2]")
If myelement Is Nothing Then
MsgBox ("no element found")
Else
myelement.Click
End If
'Opend 2nd child window
driver.SwitchToNextWindow
Application.Wait (Now + TimeValue("0:0:05"))
'Click on 'Export filter details'
Set myelement = driver.FindElementById("exportFilterDetails")
If myelement Is Nothing Then
MsgBox ("no element found")
Else
myelement.Click
End If
'Click on Export button
Set myelement = driver.FindElementById("3131")
If myelement Is Nothing Then
MsgBox ("no element found")
Else
myelement.Click
End If
Application.Wait (Now + TimeValue("0:0:08"))
FileSpec = StoreFile & ".xls*"
Debug.Print FileSpec
FileName = Dir(MyDir & FileSpec)
Debug.Print FileName
If FileName <> "" Then
MostRecentFile = FileName
MostRecentDate = FileDateTime(MyDir & FileName)
Do While FileName <> ""
If FileDateTime(MyDir & FileName) > MostRecentDate Then
MostRecentFile = FileName
MostRecentDate = FileDateTime(MyDir & FileName)
End If
FileName = Dir
Loop
End If
MyFile = MostRecentFile
Debug.Print MyFile
ChDir MyDir
Set SrcWb = Workbooks.Open(MyDir + MyFile, UpdateLinks:=0)
'Saving as xls workbook
SrcWb.SaveAs DestFolder & MyFile, XlFileFormat.xlExcel8
Application.Wait (Now + TimeValue("0:0:04"))
Application.DisplayAlerts = True
SrcWb.Close
driver.Close
driver.SwitchToPreviousWindow
driver.Close
driver.SwitchToPreviousWindow ( Want to switch back to parent window)
Application.Wait (Now + TimeValue("0:0:08"))
endTry:
End If
Next a
答案 0 :(得分:1)
关闭第一个子窗口以将控件切换到父窗口而不是driver.SwitchToPreviousWindow
之后,更好的解决方案是使用以下任一方法:< / p>
SwitchToWindowByName():
/// <summary>
/// Switch focus to the specified window by name.
/// </summary>
/// <param name="name">The name of the window to activate</param>
/// <param name="timeout">Optional timeout in milliseconds</param>
/// <param name="raise">Optional - Raise an exception after the timeout when true</param>
/// <returns>Current web driver</returns>
public Window SwitchToWindowByName(string name, int timeout = -1, bool raise = true) {
try {
return session.windows.SwitchToWindowByName(name, timeout);
} catch (Errors.NoSuchWindowError) {
if (raise)
throw new Errors.NoSuchWindowError(name);
return null;
}
}
SwitchToWindowByTitle():
/// <summary>
/// Switch focus to the specified window by title.
/// </summary>
/// <param name="title">The title of the window to activate</param>
/// <param name="timeout">Optional timeout in milliseconds</param>
/// <param name="raise">Optional - Raise an exception after the timeout when true</param>
/// <returns>Current web driver</returns>
public Window SwitchToWindowByTitle(string title, int timeout = -1, bool raise = true) {
try {
return session.windows.SwitchToWindowByTitle(title, timeout);
} catch (Errors.NoSuchWindowError) {
if (raise)
throw new Errors.NoSuchWindowError(title);
return null;
}
}
答案 1 :(得分:0)
我从@ DebanjanB的解决方案中获取了这个想法,它只是一个简单的单行更改'driver.SwitchToWindowByTitle(“WindowTitle”)'并且它有效。