Selenium Webdriver用ID搜索时找不到按钮元素

时间:2019-08-16 14:35:07

标签: selenium selenium-webdriver xpath css-selectors webdriverwait

URL http://letskodeit.teachable.com/pages/practice中有一个名为打开窗口的按钮元素。

我可以检查名为“ openwindow”的元素按钮。我尝试为此元素使用“ id”和“ xpath”。但错误显示“找不到元素”。我有隐性的等待。 我可以检查名为“ openwindow”的元素按钮。我尝试为此元素使用“ id”和“ xpath”。但错误显示“找不到元素”。我有隐式的等待。

import java.util.Set;
import java.util.concurrent.TimeUnit;   

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;     

public class WindowHandles {

       private WebDriver achromeDriver;
       private String baseUrl;   

       @Before
       public void setUp() throws Exception {  

              baseUrl = "http://letskodeit.teachable.com/pages/practice";           
              System.setProperty("webdriver.chrome.driver", "C:\\Selenium\\ChromeDirver\\chromedriver.exe");             

              achromeDriver = new ChromeDriver();               

              achromeDriver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);             

              achromeDriver.manage().window().maximize();   

              System.out.println("setup completed");
       }    

       @Test

       public void test() throws InterruptedException {                       // Get the handle

              String parentHandle = achromeDriver.getWindowHandle();
              System.out.println("Parent Handle: " + parentHandle);                

              // Find Open Window button                
             System.out.println("before finding the element");                

              WebElement openWindow = achromeDriver.findElement(By.xpath("//button[@name='openwindow']"));                

              openWindow.click();    

              // Get all handles

              Set<String> handles = achromeDriver.getWindowHandles();    

              // Switching between handles

              for (String handle: handles) {
                     System.out.println(handle);

              }
              // Switch back to the parent window

       }



       @After

       public void tearDown() throws Exception {

       }

}

2 个答案:

答案 0 :(得分:0)

我添加了显式等待和achromeDriver.get(baseUrl);方法来启动URL。下面的代码对我有用。

import java.util.Set;
import java.util.concurrent.TimeUnit;   

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;     

public class WindowHandles {

   private WebDriver achromeDriver;
   private String baseUrl;   

   @Before
   public void setUp() throws Exception {  

          baseUrl = "http://letskodeit.teachable.com/pages/practice";           
          System.setProperty("webdriver.chrome.driver", 
"C:\\Selenium\\ChromeDirver\\chromedriver.exe");             

          achromeDriver = new ChromeDriver();               
          achromeDriver.get(baseUrl);


          achromeDriver.manage().window().maximize();   

          System.out.println("setup completed");
   }    

   @Test

   public void test() throws InterruptedException {   // Get the handle

          String parentHandle = achromeDriver.getWindowHandle();
          System.out.println("Parent Handle: " + parentHandle);                
          WebDriverWait w= new WebDriverWait(achromeDriver, 10);
          WebElement openWindow = 
 achromeDriver.findElement(By.xpath("//button[@id='openwindow']")); 
          w.until(ExpectedConditions.elementToBeClickable(openWindow));
          // Find Open Window button                
         System.out.println("before finding the element");                              

          openWindow.click();    

          // Get all handles

          Set<String> handles = achromeDriver.getWindowHandles();    

          // Switching between handles

          for (String handle: handles) {
                 System.out.println(handle);

          }
          // Switch back to the parent window

   }



   @After

   public void tearDown() throws Exception {

   }

}

答案 1 :(得分:-1)

要在文本为打开窗口的元素上click(),您必须为elementToBeClickable()引入 WebDriverWait ,并且您可以使用跟随Locator Strategies

  • cssSelector

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("button#openwindow"))).click();
    
  • xpath

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[@id='openwindow']"))).click();