【问题标题】:Switching back to the parent window in chrome using Selenium VBA使用 Selenium VBA 切换回 chrome 中的父窗口
【发布时间】: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

标签: vba selenium


【解决方案1】:

在关闭第一个子窗口以将控制切换到父窗口而不是driver.SwitchToPreviousWindow之后,更好的解决方案是使用以下任一方法:

  • 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;
        }
    }
    

【讨论】:

  • 感谢@DebanjanB,我从您的解决方案中得到了这个想法,它只是简单的一行更改'driver.SwitchToWindowByTitle(“WindowTitle”)'并且它起作用了。
【解决方案2】:

我从@DebanjanB 的解决方案中获得了这个想法,它只是一个简单的一行更改'driver.SwitchToWindowByTitle(“WindowTitle”)'并且它起作用了。

【讨论】:

  • 由于他的回答导致您的问题得到解决,因此赞成和/或将他的回答标记为对您问题的回答将是犹太教。
猜你喜欢
  • 1970-01-01
  • 2012-11-28
  • 1970-01-01
  • 1970-01-01
  • 2012-12-31
  • 2018-01-03
  • 2016-04-25
  • 2019-02-23
  • 2012-07-06
相关资源
最近更新 更多