IE中的Selenium Web驱动程序 - 控件从父窗口传递到子窗口但无法找到元素

时间:2017-05-05 08:06:40

标签: selenium selenium-webdriver

enter link description here我对selenium web驱动程序很新。我正在自动化一个应用程序,在点击链接后,弹出窗口打开,其余操作需要在弹出窗口中执行。我面临的问题是,我能够使用WindowHandle导航到弹出窗口,并且弹出窗口的页面标题也正确地被捕获。但是我无法在弹出窗口中找到任何输入的web元素

上面链接的代码:

Private Sub Search_Click()
 Dim sheet1 As Excel.Worksheet
 Dim Sheet2 As Excel.Worksheet
 Dim Sheet3 As Excel.Worksheet
 Dim Sheet4 As Excel.Worksheet
 Dim Sheet5 As Excel.Worksheet

 Dim length As Integer
 Dim i As Integer
 Dim text1 As String
 Dim text2 As String
 Dim text3 As String
 Dim text4 As String
 Dim text5 As String
 Dim text6 As String
 Dim input1 As String
 Dim input2 As String
 Dim input3 As String
 Dim output4 As String
 Dim output5 As String
 Dim output6 As String





 Set Sheet5 = Worksheets(5)
 text1 = Sheet5.Cells(4, 2).Value
 text2 = Sheet5.Cells(4, 6).Value
 text3 = Sheet5.Cells(4, 8).Value
 input1 = Sheet5.Cells(10, 2).Value
 input2 = Sheet5.Cells(10, 6).Value
 input3 = Sheet5.Cells(10, 8).Value

  Sheet5.Cells(4, 2).Value = ""
  Sheet5.Cells(4, 6).Value = ""
  Sheet5.Cells(4, 8).Value = ""
  Sheet5.Cells(10, 2).Value = ""
  Sheet5.Cells(10, 6).Value = ""
  Sheet5.Cells(10, 8).Value = ""


  Sheet5.Cells(7, 3).Value = ""
  Sheet5.Cells(7, 6).Value = ""
  Sheet5.Cells(7, 9).Value = ""
  Sheet5.Cells(13, 3).Value = ""
  Sheet5.Cells(13, 6).Value = ""
  Sheet5.Cells(13, 9).Value = ""


 Sheet5.Cells(4, 2).Interior.ColorIndex = 28
 Sheet5.Cells(4, 6).Interior.ColorIndex = 28
 Sheet5.Cells(4, 8).Interior.ColorIndex = 28
 Sheet5.Cells(10, 2).Interior.ColorIndex = 28
 Sheet5.Cells(10, 6).Interior.ColorIndex = 28
 Sheet5.Cells(10, 8).Interior.ColorIndex = 28

 Sheet5.Cells(7, 3).Interior.ColorIndex = 6
 Sheet5.Cells(7, 6).Interior.ColorIndex = 6
 Sheet5.Cells(7, 6).Interior.ColorIndex = 6
 Sheet5.Cells(13, 3).Interior.ColorIndex = 6
 Sheet5.Cells(13, 6).Interior.ColorIndex = 6
 Sheet5.Cells(13, 9).Interior.ColorIndex = 6




 Set Sheet3 = Worksheets(3)
 length = WorksheetFunction.CountA(Sheet3.Columns(1))

 For i = 1 To length
  text4 = Sheet3.Cells(i, 1).Value
  text5 = Sheet3.Cells(i, 2).Value
  'text6 = Sheet3.Cells(i, 3).Value

  If text1 = text4 And text2 = text5 And text3 Like "?[NB,NS,NF,PE]" Then
    Sheet5.Cells(7, 3).Value = Sheet3.Cells(i, 5).Value And Sheet5.Cells(7, 6).Value = Sheet3.Cells(i, 6) And Sheet5.Cells(7, 9).Value = Sheet3.Cells(i, 4)

  End If
 Next

 For i = 1 To length
  output4 = Sheet3.Cells(i, 5).Value
  output5 = Sheet3.Cells(i, 2).Value
  'output6 = Sheet3.Cells(i, 3).Value

  If input1 = output4 And input2 = output5 And input3 Like "?[NB,NS,NF,PE]" Then
    Sheet5.Cells(13, 3).Value = Sheet3.Cells(i, 1).Value And Sheet5.Cells(13, 6).Value = Sheet3.Cells(i, 6) And Sheet5.Cells(13, 9).Value = Sheet3.Cells(i, 4)

  End If
 Next



 'For i = 1 To length
  'text4 = Sheet3.Cells(i, 1).Value
  'text5 = Sheet3.Cells(i, 2).Value
  'text6 = Sheet3.Cells(i, 3).Value
  'If text1 = text2 Then
    'Sheet3.Cells(7, 2).Value = Sheet2.Cells(i, 2).Value
  'End If
 'Next

 If Sheet5.Cells(7, 3).Value = "" Then
  Sheet5.Cells(7, 3).Value = "No result found"
  Sheet5.Cells(7, 3).Interior.ColorIndex = 3
 Else
  Sheet5.Cells(7, 3).Interior.ColorIndex = 43
 End If

If text1 = "" Then
 Sheet5.Cells(7, 3).Interior.ColorIndex = 6
 Sheet5.Cells(7, 3).Value = "provide some value in search text"
End If

If Sheet5.Cells(7, 6).Value = "" Then
  Sheet5.Cells(7, 6).Value = "No result found"
  Sheet5.Cells(7, 6).Interior.ColorIndex = 3
 Else
  Sheet5.Cells(7, 6).Interior.ColorIndex = 43
 End If

If text1 = "" Then
 Sheet5.Cells(7, 6).Interior.ColorIndex = 6
 Sheet5.Cells(7, 6).Value = "provide some value in search text"
End If

If Sheet5.Cells(7, 9).Value = "" Then
  Sheet5.Cells(7, 9).Value = "No result found"
  Sheet5.Cells(7, 9).Interior.ColorIndex = 3
 Else
  Sheet5.Cells(7, 9).Interior.ColorIndex = 43
 End If

If text1 = "" Then
 Sheet5.Cells(7, 9).Interior.ColorIndex = 6
 Sheet5.Cells(7, 9).Value = "provide some value in search text"
End If

If Sheet5.Cells(13, 3).Value = "" Then
  Sheet5.Cells(13, 3).Value = "No result found"
  Sheet5.Cells(13, 3).Interior.ColorIndex = 3
 Else
  Sheet5.Cells(13, 3).Interior.ColorIndex = 43
 End If

If input1 = "" Then
 Sheet5.Cells(13, 3).Interior.ColorIndex = 6
 Sheet5.Cells(13, 3).Value = "provide some value in search text"
End If

If Sheet5.Cells(13, 6).Value = "" Then
  Sheet5.Cells(13, 6).Value = "No result found"
  Sheet5.Cells(13, 6).Interior.ColorIndex = 3
 Else
  Sheet5.Cells(13, 6).Interior.ColorIndex = 43
 End If

If input1 = "" Then
 Sheet5.Cells(13, 6).Interior.ColorIndex = 6
 Sheet5.Cells(13, 6).Value = "provide some value in search text"

End If
If Sheet5.Cells(13, 9).Value = "" Then
  Sheet5.Cells(13, 9).Value = "No result found"
  Sheet5.Cells(13, 9).Interior.ColorIndex = 3
 Else
  Sheet5.Cells(13, 9).Interior.ColorIndex = 43
 End If

If input1 = "" Then
 Sheet5.Cells(13, 9).Interior.ColorIndex = 6
 Sheet5.Cells(13, 9).Value = "provide some value in search text"
End If

End Sub



Private Sub Search_Click()
 Dim sheet1 As Excel.Worksheet
 Dim Sheet2 As Excel.Worksheet
 Dim Sheet3 As Excel.Worksheet
 Dim Sheet4 As Excel.Worksheet
 Dim Sheet5 As Excel.Worksheet

 Dim length As Integer
 Dim i As Integer
 Dim text1 As String
 Dim text2 As String
 Dim text3 As String
 Dim text4 As String
 Dim text5 As String
 Dim text6 As String
 Dim input1 As String
 Dim input2 As String
 Dim input3 As String
 Dim output4 As String
 Dim output5 As String
 Dim output6 As String





 Set Sheet5 = Worksheets(5)
 text1 = Sheet5.Cells(4, 2).Value
 text2 = Sheet5.Cells(4, 6).Value
 text3 = Sheet5.Cells(4, 8).Value
 input1 = Sheet5.Cells(10, 2).Value
 input2 = Sheet5.Cells(10, 6).Value
 input3 = Sheet5.Cells(10, 8).Value

  Sheet5.Cells(4, 2).Value = ""
  Sheet5.Cells(4, 6).Value = ""
  Sheet5.Cells(4, 8).Value = ""
  Sheet5.Cells(10, 2).Value = ""
  Sheet5.Cells(10, 6).Value = ""
  Sheet5.Cells(10, 8).Value = ""


  Sheet5.Cells(7, 3).Value = ""
  Sheet5.Cells(7, 6).Value = ""
  Sheet5.Cells(7, 9).Value = ""
  Sheet5.Cells(13, 3).Value = ""
  Sheet5.Cells(13, 6).Value = ""
  Sheet5.Cells(13, 9).Value = ""


 Sheet5.Cells(4, 2).Interior.ColorIndex = 28
 Sheet5.Cells(4, 6).Interior.ColorIndex = 28
 Sheet5.Cells(4, 8).Interior.ColorIndex = 28
 Sheet5.Cells(10, 2).Interior.ColorIndex = 28
 Sheet5.Cells(10, 6).Interior.ColorIndex = 28
 Sheet5.Cells(10, 8).Interior.ColorIndex = 28

 Sheet5.Cells(7, 3).Interior.ColorIndex = 6
 Sheet5.Cells(7, 6).Interior.ColorIndex = 6
 Sheet5.Cells(7, 6).Interior.ColorIndex = 6
 Sheet5.Cells(13, 3).Interior.ColorIndex = 6
 Sheet5.Cells(13, 6).Interior.ColorIndex = 6
 Sheet5.Cells(13, 9).Interior.ColorIndex = 6




 Set Sheet3 = Worksheets(3)
 length = WorksheetFunction.CountA(Sheet3.Columns(1))

 For i = 1 To length
  text4 = Sheet3.Cells(i, 1).Value
  text5 = Sheet3.Cells(i, 2).Value
  'text6 = Sheet3.Cells(i, 3).Value

  If text1 = text4 And text2 = text5 And text3 Like "?[NB,NS,NF,PE]" Then
    Sheet5.Cells(7, 3).Value = Sheet3.Cells(i, 5).Value And Sheet5.Cells(7, 6).Value = Sheet3.Cells(i, 6) And Sheet5.Cells(7, 9).Value = Sheet3.Cells(i, 4)

  End If
 Next

 For i = 1 To length
  output4 = Sheet3.Cells(i, 5).Value
  output5 = Sheet3.Cells(i, 2).Value
  'output6 = Sheet3.Cells(i, 3).Value

  If input1 = output4 And input2 = output5 And input3 Like "?[NB,NS,NF,PE]" Then
    Sheet5.Cells(13, 3).Value = Sheet3.Cells(i, 1).Value And Sheet5.Cells(13, 6).Value = Sheet3.Cells(i, 6) And Sheet5.Cells(13, 9).Value = Sheet3.Cells(i, 4)

  End If
 Next



 'For i = 1 To length
  'text4 = Sheet3.Cells(i, 1).Value
  'text5 = Sheet3.Cells(i, 2).Value
  'text6 = Sheet3.Cells(i, 3).Value
  'If text1 = text2 Then
    'Sheet3.Cells(7, 2).Value = Sheet2.Cells(i, 2).Value
  'End If
 'Next

 If Sheet5.Cells(7, 3).Value = "" Then
  Sheet5.Cells(7, 3).Value = "No result found"
  Sheet5.Cells(7, 3).Interior.ColorIndex = 3
 Else
  Sheet5.Cells(7, 3).Interior.ColorIndex = 43
 End If

If text1 = "" Then
 Sheet5.Cells(7, 3).Interior.ColorIndex = 6
 Sheet5.Cells(7, 3).Value = "provide some value in search text"
End If

If Sheet5.Cells(7, 6).Value = "" Then
  Sheet5.Cells(7, 6).Value = "No result found"
  Sheet5.Cells(7, 6).Interior.ColorIndex = 3
 Else
  Sheet5.Cells(7, 6).Interior.ColorIndex = 43
 End If

If text1 = "" Then
 Sheet5.Cells(7, 6).Interior.ColorIndex = 6
 Sheet5.Cells(7, 6).Value = "provide some value in search text"
End If

If Sheet5.Cells(7, 9).Value = "" Then
  Sheet5.Cells(7, 9).Value = "No result found"
  Sheet5.Cells(7, 9).Interior.ColorIndex = 3
 Else
  Sheet5.Cells(7, 9).Interior.ColorIndex = 43
 End If

If text1 = "" Then
 Sheet5.Cells(7, 9).Interior.ColorIndex = 6
 Sheet5.Cells(7, 9).Value = "provide some value in search text"
End If

If Sheet5.Cells(13, 3).Value = "" Then
  Sheet5.Cells(13, 3).Value = "No result found"
  Sheet5.Cells(13, 3).Interior.ColorIndex = 3
 Else
  Sheet5.Cells(13, 3).Interior.ColorIndex = 43
 End If

If input1 = "" Then
 Sheet5.Cells(13, 3).Interior.ColorIndex = 6
 Sheet5.Cells(13, 3).Value = "provide some value in search text"
End If

If Sheet5.Cells(13, 6).Value = "" Then
  Sheet5.Cells(13, 6).Value = "No result found"
  Sheet5.Cells(13, 6).Interior.ColorIndex = 3
 Else
  Sheet5.Cells(13, 6).Interior.ColorIndex = 43
 End If

If input1 = "" Then
 Sheet5.Cells(13, 6).Interior.ColorIndex = 6
 Sheet5.Cells(13, 6).Value = "provide some value in search text"

End If
If Sheet5.Cells(13, 9).Value = "" Then
  Sheet5.Cells(13, 9).Value = "No result found"
  Sheet5.Cells(13, 9).Interior.ColorIndex = 3
 Else
  Sheet5.Cells(13, 9).Interior.ColorIndex = 43
 End If

If input1 = "" Then
 Sheet5.Cells(13, 9).Interior.ColorIndex = 6
 Sheet5.Cells(13, 9).Value = "provide some value in search text"
End If

End Sub

1 个答案:

答案 0 :(得分:1)

关于解决方案的几句话:

  1. 保持imports最小化。
  2. 避免使用不需要的导入import com.thoughtworks.selenium.webdriven.commands.Click;
  3. 查看您的代码&删除不需要的代码driver.findElement(By.id("overridelink")).click();
  4. 避免使用Thread.sleep(6000);
  5. 在整个执行过程中诱导implicitlyWait有效。
  6. 如果您的xpath看起来容易受到攻击,请使用css By.xpath("//Image[@title = 'SEARCH HTTPS']")
  7. 以下是您自己的工作代码,其中包含一些简单的调整,最后在控制台上打印text IM sent

    System.setProperty("webdriver.ie.driver", "C:\\your_directory\\IEDriverServer.exe");
    WebDriver driver = new InternetExplorerDriver();
    driver.manage().window().maximize();
    driver.get("https://194.74.171.122/e-ServiceDesk/frmLogin.aspx?ReturnUrl=/e-ServiceDesk/default.aspx");
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    String window_title1 = driver.getTitle();
    if (driver.getTitle().equalsIgnoreCase(window_title1))
    {
        driver.findElement(By.id("txtUserName")).clear();
        driver.findElement(By.id("txtUserName")).sendKeys("RSen");
        driver.findElement(By.id("btnLogin")).click();
        Thread.sleep(6000);
    }
    String window_title2 = driver.getTitle();
    if (driver.getTitle().equalsIgnoreCase(window_title2))
    {
        Boolean a = driver.findElement(By.xpath("//Image[@title = 'SEARCH HTTPS']")).isDisplayed() ;
        System.out.println(a);
        String MainWindow = driver.getWindowHandle();
        driver.findElement(By.xpath("//Image[@title = 'SEARCH HTTPS']")).click();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        Set<String> s1=driver.getWindowHandles();
        Iterator<String> i1=s1.iterator();
        while(i1.hasNext())         
            {       
                String ChildWindow=i1.next();
                if(!MainWindow.equalsIgnoreCase(ChildWindow))           
                {           
                        driver.switchTo().window(ChildWindow);        
                        driver.manage().window().maximize();
                        Thread.sleep(10000);
                        String c = driver.getTitle();
                        System.out.println(c);  
                        Thread.sleep(5000);
                        driver.switchTo().frame("eFormContents");
                        Thread.sleep(3000);
                        driver.findElement(By.id("existCallRef")).click();
                        driver.findElement(By.id("existCallRef")).sendKeys("IM");
                        System.out.println("text IM sent");
                }
            }   
    }
    
  8. 如果这有助于您,请告诉我。