Java Selenium通过xpath查找元素
我正在尝试使用selenium登录Java Selenium通过xpath查找元素,java,html,selenium,xpath,inspect-element,Java,Html,Selenium,Xpath,Inspect Element,我正在尝试使用selenium登录 <form id="main-login-form" class="login-form form-container"> <input name="username" id="username-input" placeholder="Username" class="form-input" autofocus=""> 导入org.openqa.selenium.*; 导入org.openqa.selenium.
<form id="main-login-form" class="login-form form-container">
<input name="username" id="username-input" placeholder="Username" class="form-input" autofocus="">
导入org.openqa.selenium.*;
导入org.openqa.selenium.htmlunit.HtmlUnitDriver
公共类表
{
我一直在犯这个错误
Exception in thread "main" org.openqa.selenium.NoSuchElementException:
Unable to locate a node using //input[@id='username-input']
For documentation on this error, please visit:
http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '2.48.2', revision: '41bccdd', time: '2015-10-09 19:59:12'
你做得对!
点击URL后,请等待它加载所有元素,以便您的web元素显示在页面上。
驱动程序.get(“”)之后的任何等待条件;工作正常。是
中的元素吗?不,它是一个“”和一个“”。例如,您可以将驱动程序更改为FirefoxDriver,看看问题是否仍然存在吗?相同的事情@drets。仍然会出现相同的错误。请注意!它会将FireFox打开到默认页面。然后“无法连接到端口7055上的主机127.0.0.1”我想我需要tch selenium上的更多youtube。它不喜欢第17行。driver=webdriver.Remote(DesiredCapabilities.chrome());
说webdriver
无法解决。您是在Java绑定上编写的,我的答案是用python编写的。我刚刚向您展示了使用xpath查找元素没有问题。
public static void main(String[] args) {
// Create a new instance of the html unit driver
// Notice that the remainder of the code relies on the interface,
// not the implementation.
WebDriver driver = new HtmlUnitDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
// And now use this to visit Google
driver.get("https://focus.nassau.k12.fl.us/focus/");
// Find the text input element by its name
WebElement element = driver.findElement(By.xpath("//input[@id='username-input']"));
element.sendKeys("mcdonaldje");
System.out.println(driver.getCurrentUrl());
// Now submit the form. WebDriver will find the form for us from the element
driver.quit();
}
Exception in thread "main" org.openqa.selenium.NoSuchElementException:
Unable to locate a node using //input[@id='username-input']
For documentation on this error, please visit:
http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '2.48.2', revision: '41bccdd', time: '2015-10-09 19:59:12'
In [16]: from selenium import webdriver
In [17]: driver = webdriver.Remote(desired_capabilities=webdriver.DesiredCapabilities.HTMLUNIT)
In [18]: driver.get("https://focus.nassau.k12.fl.us/focus/");
In [19]: el = driver.find_element_by_xpath("//input[@id='username-input']")
In [20]: el.send_keys("mcdonaldje")
In [21]: driver.current_url
Out[21]: u'https://focus.nassau.k12.fl.us/focus/'