【发布时间】:2018-08-25 02:40:29
【问题描述】:
我的代码打开了 2 个子窗口。对每个执行操作后,我需要关闭窗口并切换回父窗口。
没有选项作为 driver.switchToParentWindow。 只有driver.switchToPreviousWindow。
例如:我关闭第二个子窗口 --> 然后 driver.switchToPreviousWindow 将控制切换回第一个子窗口,但是当我关闭此窗口并执行 driver.SwitchToPreviousWindow 时,它会搜索最近关闭的第二个子窗口,而我想要它将控制切换到父窗口。
我已尝试到处寻找解决方案,但似乎找不到使用 Selenium VBA 切换回父窗口的解决方案。
以下是我的代码:
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
【问题讨论】:
-
您使用的是哪个浏览器?你可以在另一个浏览器中重现吗?我知道这(曾经是?)至少对于 Firefox 来说是一个已知问题
-
我用的是谷歌浏览器,最新版本版本 --> 65.0.3325.162