【发布时间】:2017-01-26 10:27:47
【问题描述】:
我的 Xpath 正确且没有 iFrame,我可以在 Chrome 控制台中找到元素,但我的程序仍然失败。我也使用了显式等待。
网站http://newtours.demoaut.com/ 我正在尝试查找登录页面并发送登录 ID。
错误信息:
通过:openURL 失败:loginToTours **org.openqa.selenium.NoSuchElementException**:**没有这样的元素:无法找到元素:{"method":"xpath","selector":"//input[@name='userName']"} ** *** 元素信息:{Using=xpath, value=//input[@name='userName']}
package SeleniumPracticePackage;
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.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait ;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;
import java.util.Properties;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
public class CallUrl {
WebDriver driver;
Properties prop;
@BeforeTest
public void openBrowser() throws IOException
{
// driver = new ChromeDriver();
ChromeOptions options = new ChromeOptions();
options.addArguments("chrome.switches","--disable-extensions");
System.setProperty("webdriver.chrome.driver","C:\\Users\\Ashish\\Documents\\Selenium\\drivers\\chromedriver_win32\\chromedriver.exe");
//System.setProperty("webdriver.chrome.driver",(System.getProperty("user.dir") + "//src//test//resources//chromedriver_new.exe"));
driver = new ChromeDriver(options);
}
@Test
public void openURL() throws IOException
{
//call URL from properties file
prop = new Properties();
FileInputStream urlFile = new FileInputStream("C:\\Users\\Ashish\\Documents\\Selenium\\SeleniumPracticeSite\\src\\URL.properties");
prop.load(urlFile);
driver.get(prop.getProperty("URL"));
WebDriverWait myDynamicElement = new WebDriverWait(driver,30);
myDynamicElement.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//input[@name='userName']")));
}
@Test
public void loginToTours () throws InterruptedException
{
driver.findElement(By.xpath("//input[@name='userName']")).click();
//sendKeys(prop.getProperty("login"));
}
}
TestNG XML
<?xml version="1.0" encoding="UTF-8"?>
<suite name = "Automation Practice Test">
<test name ="smoke test">
<groups>
<run>
<include name="Priority2" />
</run>
</groups>
<classes>
<class name ="SeleniumPracticePackage.CallUrl" />
</classes>
</test>
</suite>
网站 HTML 源代码
<tr>
<td align="right"><font face="Arial, Helvetica, sans-serif" size="2">User
Name: </font></td>
<td width="112">
<input type="text" name="userName" size="10">
</td>
</tr>
【问题讨论】:
-
我在正确的页面。 selenium 加载页面后,我测试了我的 xpath。加载 URL 的方法是 openURL() ... driver.get(prop.getProperty("URL"));
-
产生此消息的 XML 是什么?
-
最后添加了xml
-
您的 XML 中的
input标记在哪里?
标签: java selenium xpath selenium-webdriver selenium-chromedriver