我收到错误消息:
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 :(得分:0)
如果您要处理两个复选框之一,则需要先切换到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();
答案 1 :(得分: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);
}
}