我遇到了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();
}
}
答案 0 :(得分:0)
在与他们交互之前,你需要明确地等待元素变得可见,例如&#34; Transcripts&#34;链接:
WebDriverWait wait = new WebDriverWait(webDriver, 5);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//a[@class='hideToggle2']")));
此外,在尝试查找夏季注册链接时,它似乎失败了。我通过部分链接文本找到,而不是潜入CSS选择器:
driver.findElement(By.partialLinkText("Summer")).click();