selenium常见问题——定位子窗口(frame)内元素失败

当页面存在子窗口(frame,框架)时,需要先切换到子窗口,然后再定位元素,否则定位会失败

 案例:

使用账号:zhangsan/123456 登陆网站 后,直接定位“用户管理”就存在该问题

如下图所示,

 直接执行   driver.findElement(By.linkText("商品管理")).click();  会查找元素失败。

解决办法:

先切换到元素所在的frame,然后再进行定位

 driver.switchTo().frame("leftFrame");
 driver.findElement(By.linkText("用户管理")).click();   

还有一点需要提醒,从一个框架切换到另一个框架时,先切换回默认框架,然后再谢欢到另一个框架

  //从leftFrame切换到子框架mainframe上
  driver.switchTo().defaultContent(); //先切换回主框架
  driver.switchTo().frame("mainFrame");//然后切换到另一个框架

完整代码如下:

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class Example {

    // <!> Check if selenium-standalone.jar is added to build path.
    public static void test(WebDriver driver) {
        // TODO Test script
    	 driver.get("http://114.215.176.95:60503/goodmanage/index.jsp");
    	   
    	   // 登陆
    	   driver.findElement(By.id("userName")).click();
    	   driver.findElement(By.id("userName")).sendKeys("zhangsan");
    	   driver.findElement(By.id("password")).click();
    	   driver.findElement(By.id("password")).sendKeys("123456");
    	   driver.findElement(By.id("btnLogin")).click();

           //打开主界面后,先切换到“leftFrame” frame,点击“商品管理”下的“商品添加”
 	       driver.switchTo().frame("leftFrame");
     	   driver.findElement(By.linkText("商品管理")).click();   	   
     	   driver.findElement(By.linkText("商品添加")).click();
    	   try {
			Thread.currentThread();
			Thread.sleep(2000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	   
    	   //切换到子框架mainframe上
    	   driver.switchTo().defaultContent(); //先切换回主框架
 	       driver.switchTo().frame("mainFrame");//然后切换到另一个框架

 	       //录入商品信息或点击提交按钮
    	   driver.findElement(By.id("productName")).click();
    	   driver.findElement(By.id("productName")).sendKeys("productname");
    	   driver.findElement(By.id("productPrice")).click();
    	   driver.findElement(By.id("productPrice")).sendKeys("100"); 
    	   driver.findElement(By.id("description")).click();
    	   driver.findElement(By.id("description")).sendKeys("description"); 
    	   driver.findElement(By.id("productNumber")).click();
    	   driver.findElement(By.id("productNumber")).sendKeys("5"); 
 	      WebElement dropdown = driver.findElement(By.name("goodsType"));
 	      dropdown.findElement(By.xpath("//option[. = '笔记本']")).click();
 	     dropdown.findElement(By.xpath("//input[@value='提交']")).click();

    }

    public static void main(String[] args) {
        // Run main function to test your script.
    	//设置chrome驱动程序的路径
    			System.setProperty("webdriver.chrome.driver","e:\\chromedriver.exe");   
    			System.out.println(System.getProperty("webdriver.chrome.driver"));//打印驱动器路径
    			
    			//初始化一个chrome驱动实例,保存到driver中
    			WebDriver driver = new ChromeDriver();   
    	 
    			//最大化窗口
    			driver.manage().window().maximize();  

        try { test(driver); } 
        catch(Exception e) { e.printStackTrace(); }
        finally { driver.quit(); }
    }

}

Supongo que te gusta

Origin blog.csdn.net/caohongxing/article/details/119088424
Recomendado
Clasificación