簡體   English   中英

org.openqa.selenium.NoSuchElementException:無法找到元素錯誤

[英]org.openqa.selenium.NoSuchElementException: Unable to locate element error

我已經使用Internet Explorer和Firefox使用Selenium Web驅動程序在JAVA中編寫了以下代碼。 每當我遇到相同的錯誤時。 嘗試同時使用“ id”和“ xpath”方法,但仍然失敗。 嘗試添加一些延遲也仍然無法正常工作。

我的Firefox JAVA代碼:

package ieconnector;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.RemoteWebDriver;

public class FireFoxConnector {

public static void main(String[] args) {
    try{
    GetBrowserProperty gbp = new GetBrowserProperty();
    System.setProperty("webdriver.ie.driver",gbp.getIeConnection());
    System.setProperty("webdriver.gecko.driver","D:\\softwares\\Selenium\\geckodriver-v0.21.0-win64\\geckodriver.exe");
    WebDriver wb = new FirefoxDriver();
    Capabilities caps = ((RemoteWebDriver) wb).getCapabilities();
    System.out.println("Caps is "+caps);
    wb.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
    //wb.navigate().to("https://somewebsite.com:22222/SSO/ui/SSOLogin.jsp");
    wb.get("https://somewebsite.com:22222/SSO/ui/SSOLogin.jsp");
    wb.manage().deleteAllCookies();
    wb.manage().window().maximize();
    //wb.findElement(By.id("usertxt")).sendKeys(("user").toUpperCase());
    //wb.findElement(By.className("passtxt")).sendKeys("password");
    //WebDriverWait wait = new WebDriverWait(wb,10);
    //WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("usertxt")));
    wb.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS);
    //wb.findElement(By.id("usertxt")).sendKeys("USER");
    wb.findElement(By.xpath("//*[@id='usertxt']")).sendKeys("USER");
    System.out.println("Testing is successful");
} catch (Exception e) {
    e.printStackTrace();
}

}

}

以下是我在IE / Firefox中的開發人員工具中的HTML代碼的屏幕截圖。

HTML

根據您共享的HTML來找到“ 用戶ID”字段,您可以使用以下解決方案:

  • cssSelector

     wb.findElement(By.cssSelector("input.txtbox#usertxt")).sendKeys("USER"); 
  • xpath

     wb.findElement(By.xpath("//input[@class='txtbox' and @id='usertxt']")).sendKeys("USER"); 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM