Java 试图查找元素时出现硒元素异常

Java 试图查找元素时出现硒元素异常,java,selenium,selenium-webdriver,Java,Selenium,Selenium Webdriver,我对Selenium的用户名和密码文本框有问题。我的主要目标是让我的程序打开Chrome浏览器,加载isis.ufl.edu,继续注册,然后用用户的用户名和密码登录。问题是,当我给出一个xpath或css路径来指向用户名和密码文本框时,Eclipse会给我一个错误,说明它找不到元素。有人说这只发生在Chrome上,但有人知道解决这个问题的方法吗 Exception in thread "main" org.openqa.selenium.NoSuchElementException: no su

我对Selenium的用户名和密码文本框有问题。我的主要目标是让我的程序打开Chrome浏览器,加载isis.ufl.edu,继续注册,然后用用户的用户名和密码登录。问题是,当我给出一个xpath或css路径来指向用户名和密码文本框时,Eclipse会给我一个错误,说明它找不到元素。有人说这只发生在Chrome上,但有人知道解决这个问题的方法吗

Exception in thread "main" org.openqa.selenium.NoSuchElementException: no such       element
  (Session info: chrome=40.0.2214.115)
  (Driver info: chromedriver=2.14.313457    (3d645c400edf2e2c500566c9aa096063e707c9cf),platform=Mac OS X 10.10.2 x86_64)  (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 23 milliseconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '2.44.0', revision: '76d78cf', time: '2014-10-23 20:03:00'
System info: host: 'Dailens-MacBook-Pro.local', ip: '192.168.1.102', os.name: 'Mac OS X', os.arch: 'x86_64', os.version: '10.10.2', java.version: '1.7.0_67'
Session ID: c8b9c346dcf7bcdc28c1cc078638d872
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities [{platform=MAC, acceptSslCerts=true, javascriptEnabled=true, browserName=chrome, chrome={userDataDir=/var/folders/81/v5g4vbfj05x16pgjg9h7_kf40000gn/T/.org.chromium.Chromium.ML2rMj}, rotatable=false, locationContextEnabled=true, mobileEmulationEnabled=false, version=40.0.2214.115, takesHeapSnapshot=true, cssSelectorsEnabled=true, databaseEnabled=false, handlesAlerts=true, browserConnectionEnabled=false, webStorageEnabled=true, nativeEvents=true, applicationCacheEnabled=false, takesScreenshot=true}]
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:204)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:156)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:599)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:352)
at org.openqa.selenium.remote.RemoteWebDriver.findElementByCssSelector(RemoteWebDriver.java:441)
at org.openqa.selenium.By$ByCssSelector.findElement(By.java:426)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:344)
at TestClass.main(TestClass.java:51)
这是我的密码:

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.WebDriverWait;
import java.util.Scanner;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;

public class TestClass  {
public static void main(String[] args) {
    //Fetching isis and schedule information
    Scanner user_input = new Scanner(System.in);
    System.out.println("User enter Isis username:");
    System.out.println("User enter Isis password:");
    System.out.println("User enter current semester:");
    String username = user_input.next();
    String password = user_input.next();
    String semester = user_input.next();

    // Create a new instance of the chrome driver
    // Notice that the remainder of the code relies on the interface, 
    // not the implementation.
    System.setProperty("webdriver.chrome.driver", "//users/dailenspencer/downloads/chromedriver");
    WebDriver driver = new ChromeDriver();


    // And now use this to visit isis
    driver.get("http://www.isis.ufl.edu/");
    // Alternatively the same thing can be done like this
    // driver.navigate().to("http://www.isis.ufl.edu/");

    //Clicking on registration tab
    driver.findElement(By.xpath("//a[@class='hideToggle2']")).click();


    //If user input fall for semester, click on fall registration
   if(semester.charAt(0) == 'f' || semester.charAt(0) == 'F'){
       driver.findElement(By.cssSelector("#reg > ul:nth-child(3) > li:nth-child(3) > a")).click();

   }

   //If user input summer, click on summer registration tab
   if(semester.charAt(1) == 'u'){
       driver.findElement(By.cssSelector("#reg > ul:nth-child(3) > li:nth-child(2) > a")).click();
   }

   //else, click spring registration tab
   else{
       driver.findElement(By.cssSelector("#reg > ul:nth-child(3) > li:nth-child(1) > a")).click();
   }

   long end = System.currentTimeMillis() + 5000;
   while (System.currentTimeMillis() < end) {
       WebElement resultsDiv = driver.findElement(By.className("wrap"));

       // If results have been returned, the results are displayed in a drop down.
       if (resultsDiv.isDisplayed()) {
         break;
       }
   }



   //Find the textbox for username, and send username to text box
   WebElement elementu = driver.findElement(By.cssSelector("#username"));
   elementu.sendKeys(username);

   WebElement elementp = driver.findElement(By.cssSelector("#password"));
   elementp.sendKeys(password);







    // Now submit the form. WebDriver will find the form for us from the element
    driver.findElement(By.cssSelector("body > div.content > div > form > p:nth-child(3) > input")).click();



    // Check the title of the page
    System.out.println("Page title is: " + driver.getTitle());

    // Google's search is rendered dynamically with JavaScript.
    // Wait for the page to load, timeout after 10 seconds
    (new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() {
        public Boolean apply(WebDriver d) {
            return d.getTitle().toLowerCase().startsWith("isis");
        }
    });



    //Close the browser
    driver.quit();
}
}
import org.openqa.selenium.By;
导入org.openqa.selenium.WebDriver;
导入org.openqa.selenium.WebElement;
导入org.openqa.selenium.chrome.ChromeDriver;
导入org.openqa.selenium.support.ui.ExpectedCondition;
导入org.openqa.selenium.support.ui.WebDriverWait;
导入java.util.Scanner;
导入org.openqa.selenium.chrome.ChromeDriver;
导入org.openqa.selenium.interactions.Actions;
公共类TestClass{
公共静态void main(字符串[]args){
//获取isis和计划信息
扫描仪用户输入=新扫描仪(System.in);
System.out.println(“用户输入Isis用户名:”);
System.out.println(“用户输入Isis密码:”);
System.out.println(“用户输入当前学期:”);
字符串username=user_input.next();
字符串密码=user_input.next();
字符串学期=用户输入。下一步();
//创建chrome驱动程序的新实例
//请注意,代码的其余部分依赖于接口,
//而不是执行。
System.setProperty(“webdriver.chrome.driver”,“//users/dailenspencer/downloads/chromedriver”);
WebDriver驱动程序=新的ChromeDriver();
//现在用这个来访问isis
驱动程序。获取(“http://www.isis.ufl.edu/");
//或者也可以这样做
//驱动程序。导航()。到(“http://www.isis.ufl.edu/");
//单击注册选项卡
findElement(By.xpath(“//a[@class='hideToggle2']”)。单击();
//如果用户输入秋季学期,请单击秋季注册
如果(学期字符(0)='f'| |学期字符(0)='f'){
driver.findElement(By.cssSelector(“#reg>ul:nth child(3)>li:nth child(3)>a”)。单击();
}
//如果用户输入summer,请单击summer注册选项卡
如果(学期字符(1)='u'){
driver.findElement(By.cssSelector(“#reg>ul:nth child(3)>li:nth child(2)>a”)。单击();
}
//否则,单击spring注册选项卡
否则{
driver.findElement(By.cssSelector(“#reg>ul:nth child(3)>li:nth child(1)>a”)。单击();
}
长端=System.currentTimeMillis()+5000;
while(System.currentTimeMillis()div.content>div>form>p:nth child(3)>input”))。单击();
//检查页面的标题
System.out.println(“页面标题为:“+driver.getTitle());
//谷歌的搜索是用JavaScript动态呈现的。
//等待页面加载,10秒后超时
(新的WebDriverWait(驱动程序,10))。直到(新的预期条件(){
公共布尔应用(WebDriver d){
返回d.getTitle().toLowerCase().startsWith(“isis”);
}
});
//关闭浏览器
driver.quit();
}
}
在与元素交互之前,您需要明确等待元素变为可见,例如“转录本”链接:

而且,它在试图寻找夏季注册链接时似乎失败了。我会通过部分链接文本而不是深入CSS选择器来找到它:

driver.findElement(By.partialLinkText("Summer")).click();
driver.findElement(By.partialLinkText("Summer")).click();