【问题标题】:NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"html/body/form/input[1]"}NoSuchElementException:没有这样的元素:无法找到元素:{“method”:“xpath”,“selector”:“html/body/form/input[1]”}
【发布时间】:2017-04-23 14:16:52
【问题描述】:

我收到错误消息:

NoSuchElementException:没有这样的元素:无法找到元素: {"method":"xpath","selector":"html/body/form/input[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);

    }
}

【问题讨论】:

    标签: java xpath selenium-webdriver


    【解决方案1】:

    当您尝试在 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);
    
    
    
        }
    
    }
    

    【讨论】:

      【解决方案2】:

      如果要处理两个复选框之一,则需要先切换到iframe,然后再搜索该元素。

      driver.get("https://www.w3schools.com/Html/tryit.asp?filename=tryhtml_checkbox");
      driver.switchTo().frame("iframeResult");
      WebElement ele =driver.findElement(By.xpath("//input[@value='Bike']"));
      

      如果之后你想处理iframe之外的元素,你可能需要切换回来

      driver.switchTo().defaultContent();
      

      【讨论】:

        猜你喜欢
        • 2021-12-14
        • 1970-01-01
        • 1970-01-01
        • 2021-12-20
        • 2022-06-23
        • 2018-04-09
        • 1970-01-01
        • 1970-01-01
        • 2020-03-18
        相关资源
        最近更新 更多