2017-04-23 461 views
0

我收到錯誤消息:NoSuchElementException:no such element:Unable to locate element:{「method」:「xpath」,「selector」:「html/body/form/input [1]」}

NoSuchElementException異常:沒有這樣的元件:無法找到元素: { 「方法」: 「的xpath」, 「選擇器」: 「HTML /體/形式/輸入[1]」}

當我嘗試運行下面的代碼。 而xpath是正確的,我已經雙重檢查

package Package; 

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.firefox.FirefoxDriver; 

public class Selenium1 { 

    public static void main(String[] args) throws InterruptedException { 
    System.setProperty("webdriver.chrome.driver","C:/chromedriver.exe"); 
    WebDriver driver = new ChromeDriver(); 
    driver.get("https://www.w3schools.com/Html/tryit.asp?filename=tryhtml_checkbox"); 
    WebElement ele =driver.findElement(By.xpath("html/body/form/input[1]")); 
    boolean displayedstatus = ele.isDisplayed(); 
    System.out.println("The display status :"+displayedstatus); 

    boolean enablestatus = ele.isEnabled(); 
    System.out.println("The enable status :"+enablestatus); 

    boolean selectedstatus = ele.isSelected(); 
    System.out.println("The selected status :"+selectedstatus); 

    ele.click(); 
    selectedstatus = ele.isSelected(); 
    System.out.println("The selected status :"+selectedstatus); 

    } 
} 

回答

0

如果要處理兩個中的一個複選框,你需要切換到iframe,然後再搜索元素。

​​

如果以後要處理外iframe元素,你可能需要與

driver.switchTo().defaultContent(); 
0

切換回當你嘗試搜索的iframe內的元素,你必須有切換焦點到您正在處理的iframe

搜索的iframe中的元素之前,試試這個:

driver.switchTo().frame(driver.findElement(By.name("iframeTitle"))); 

在這種情況下,iframe的標題是:iframeResult

下面是代碼:

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.firefox.FirefoxDriver; 

public class Selenium1 { 

    public static void main(String[] args) throws InterruptedException { 
    System.setProperty("webdriver.chrome.driver","C:/chromedriver.exe"); 
    WebDriver driver = new ChromeDriver(); 
    driver.get("https://www.w3schools.com/Html/tryit.asp?filename=tryhtml_checkbox"); 

    //switching focus to iframe 
    driver.switchTo().frame(driver.findElement(By.name("iframeResult"))); 

    WebElement ele =driver.findElement(By.xpath("html/body/form/input[1]")); 
    boolean displayedstatus = ele.isDisplayed(); 
    System.out.println("The display status :"+displayedstatus); 

    boolean enablestatus = ele.isEnabled(); 
    System.out.println("The enable status :"+enablestatus); 

    boolean selectedstatus = ele.isSelected(); 
    System.out.println("The selected status :"+selectedstatus); 

    ele.click(); 
    selectedstatus = ele.isSelected(); 
    System.out.println("The selected status :"+selectedstatus); 



    } 

} 
+0

謝謝很多! :) –

相關問題