我的Xpath是正確的&沒有iFrame和我可以在Chrome控制檯中定位元素,但我的程序仍然失敗。我也用過明確的等待。Xpath是正確的仍然沒有這樣的元素:無法定位元素
網站http://newtours.demoaut.com/ 我想找到登錄頁面併發送登錄ID。
錯誤消息:
通過:的OpenURL FAILED:loginToTours ** org.openqa.selenium.NoSuchElementException **:**沒有這樣的元件:無法找到元素:{ 「方法」:」 xpath「,」selector「:」//輸入[@ 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>
我在正確的頁面。一旦硒加載頁面,我測試了我的xpath。加載URL的方法是openURL()... driver.get(prop.getProperty(「URL」)); – Tokci
生成此消息的XML是什麼? –
在最後添加了xml – Tokci