当前位置: 首页 > 知识库问答 >
问题:

单击子窗口中的按钮时收到“org.openqa.selenium.nosuchelementException:没有这样的元素”

薛征
2023-03-14

尝试单击子窗口中的按钮时出现以下错误。

public class FELogin {
private WebDriver driver;
private String url, myuserName, myPassword, LoginUsernamelink, LoginPasswordlink, LoginOklink, OpenFEModulelink;    

@BeforeTest
  public void setUp() throws Exception {        
    //get values from OR.Properties file
     FileInputStream conf=new FileInputStream("C:\\eclipse new selenium\\Proj\\Config\\OR.Properties");
     Properties p=new Properties();
     p.load(conf);   
     Thread.sleep(1000);
     url=p.getProperty("testSiteName");
     Thread.sleep(1000);         
     myuserName=p.getProperty("myusername_link");
     myPassword=p.getProperty("mypassword_link");
     LoginUsernamelink=p.getProperty("LoginUsername_link");
     LoginPasswordlink=p.getProperty("LoginPassword_link");
     LoginOklink=p.getProperty("LoginOk_link");
     OpenFEModulelink=p.getProperty("OpenFEModule_link");

     //Chrome Driver Configuration
     System.setProperty("webdriver.chrome.driver", "C:\\Selenium JARs\\chromedriver.exe");
     ChromeOptions options = new ChromeOptions();
     options.addArguments("--disable-extensions");
     driver = new ChromeDriver(options);         
      driver.get(url);
      driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

      //maximize chrome window
      driver.manage().window().maximize();
      Thread.sleep(1000);        
  }
@Test
public void Login_Main() throws InterruptedException {   

  //Enter username
  driver.findElement(ById.xpath(LoginUsernamelink)).clear();
  driver.findElement(ById.xpath(LoginUsernamelink)).sendKeys("user");
  Thread.sleep(1000);
  //Enter password
  driver.findElement(ById.xpath(LoginPasswordlink)).clear();
  driver.findElement(ById.xpath(LoginPasswordlink)).sendKeys("New@123");
  Thread.sleep(1000);
  //login to website
  driver.findElement(ById.xpath(LoginOklink)).click();
  Thread.sleep(6000); 
  driver.switchTo().frame(0);
  Thread.sleep(1000);

  driver.findElement(ById.cssSelector(OpenFEModulelink)).click();
  Thread.sleep(3000);     

  String ParentWindow = driver.getWindowHandle();
  Thread.sleep(200);
    for(String childHandle: driver.getWindowHandles()){
        Thread.sleep(100);
        driver.switchTo().window(childHandle);
    Thread.sleep(100);              
    }

//the below code for login to child window

  Thread.sleep(1000);
  //enter user name
  driver.findElement(By.id("username")).sendKeys("user");
  Thread.sleep(1000);
  //enter password
  driver.findElement(By.id("password")).sendKeys("New@123");
  Thread.sleep(1000);
  //login to website
  driver.findElement(By.name("submit")).click();
  Thread.sleep(3000);    
  driver.manage().window().maximize();
  Thread.sleep(5000);        

//------upto here working fine-------------

  //click on search button in child window 

  //----The below code is not working for me---getting exception(no such element)
  driver.findElement(ById.xpath(".//*[@id='zT7Tg3']")).click();
  Thread.sleep(1000);

}

共有1个答案

公羊俊
2023-03-14

HTML源可能会帮助我更好地分析。但是,您也可以在切换到特定的框架或窗口之后使用getpagesource()函数来调试代码,以检查它是否正确地切换到页面,并查看元素在您要切换到的页面中是否可用。

 类似资料: