IE-Control中的Selenium Web驱动程序正在从父窗口传递到childwindow,但无法找到元素

IE-Control中的Selenium Web驱动程序正在从父窗口传递到childwindow,但无法找到元素,selenium,selenium-webdriver,Selenium,Selenium Webdriver,我对selenium web驱动程序非常陌生。我正在自动化一个应用程序,在点击链接后,弹出窗口打开,其余操作需要在弹出窗口中执行。我面临的问题是,我能够使用WindowHandle导航到弹出窗口,并且弹出窗口的页面标题也被正确捕获。但我无法在弹出窗口中找到任何输入web元素 上面链接的代码: Private Sub Search_Click() Dim sheet1 As Excel.Worksheet Dim Sheet2 As Excel.Worksheet Dim Sheet3 As

我对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

关于解决方案的几句话:

  • 尽量减少导入的
  • 避免使用不需要的导入
    import com.thoughtworks.selenium.webdriven.commands.Click
  • 查看您的代码并删除不需要的代码
    driver.findElement(By.id(“overridelink”))。单击()
  • 避免使用
    线程睡眠(6000)
  • 一旦你诱导
    隐式等待
    它在整个执行过程中都是有效的
  • 如果您的xpath在.xpath(//Image[@title='SEARCH HTTPS'])下看起来易受攻击,请使用css
  • 下面是您自己的工作代码,其中有一些简单的调整,可以在末尾的控制台上打印
    文本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");
                }
            }   
    }
    
    System.setProperty(“webdriver.ie.driver”,“C:\\your\u directory\\IEDriverServer.exe”);
    WebDriver驱动程序=新的InternetExplorerDriver();
    driver.manage().window().maximize();
    驱动程序。获取(“https://194.74.171.122/e-ServiceDesk/frmLogin.aspx?ReturnUrl=/e-ServiceDesk/default.aspx”);
    driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
    字符串窗口_title1=driver.getTitle();
    if(driver.getTitle().equalsIgnoreCase(窗口标题1))
    {
    driver.findElement(By.id(“txtUserName”)).clear();
    driver.findElement(By.id(“txtUserName”)).sendKeys(“RSen”);
    driver.findElement(By.id(“btnLogin”))。单击();
    睡眠(6000);
    }
    字符串窗口_title2=driver.getTitle();
    if(driver.getTitle().equalsIgnoreCase(窗口标题2))
    {
    布尔值a=driver.findElement(By.xpath(“//Image[@title='SEARCH HTTPS']”);
    系统输出打印项次(a);
    字符串MainWindow=driver.getWindowHandle();
    findElement(By.xpath(“//Image[@title='SEARCH HTTPS']))。单击();
    driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
    Set s1=driver.getWindowHandles();
    迭代器i1=s1.Iterator();
    while(i1.hasNext())
    {       
    字符串ChildWindow=i1.next();
    如果(!MainWindow.equalsIgnoreCase(ChildWindow))
    {           
    driver.switchTo().window(ChildWindow);
    driver.manage().window().maximize();
    睡眠(10000);
    字符串c=driver.getTitle();
    系统输出打印ln(c);
    睡眠(5000);
    driver.switchTo().frame(“eFormContents”);
    睡眠(3000);
    driver.findElement(By.id(“existCallRef”))。单击();
    driver.findElement(By.id(“existCallRef”)).sendKeys(“IM”);
    System.out.println(“文本IM发送”);
    }
    }   
    }
    

  • 如果这对您有帮助,请告诉我。

    您可能想发布不起作用的代码。我无法在此处粘贴完整代码。请使用编辑问题。在主菜单中添加了代码链接question@Rajit您在哪一行看到错误?Thankstory@Dev,我没有拒绝。我投票赞成这个答案。不确定是否发生了一些未命中的点击。我已经这样做了。但它又一次递减为0