0

我的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> 
+0

我在正確的頁面。一旦硒加載頁面,我測試了我的xpath。加載URL的方法是openURL()... driver.get(prop.getProperty(「URL」)); – Tokci

+0

生成此消息的XML是什麼? –

+0

在最後添加了xml – Tokci

回答

1

問題在於你的測試代碼。

說明:在測試類中有兩個@Test方法。其中一個打開一個URL並等待一個元素出現,另一個只搜索該元素。如果TestNG首先運行您的loginToTours()測試方法,那麼您將看到此錯誤消息。默認情況下,TestNG按字母順序執行方法。由於loginToTours()沒有指定findElement()位於哪個網頁,因此Selenium最終在空白頁面上執行它,因此您會看到錯誤消息。其中一個修復方法是讓loginToTours()方法取決於openURL(),這樣在搜索前就已經在瀏覽器中打開了一個有效的網頁。 [這正是我在清理過的代碼中完成的工作]。

webdriver的是無過錯這裏:)

這裏是你的測試代碼清理版本,做工精細

public class CallUrl { 
    WebDriver driver; 

    @BeforeClass 
    public void openBrowser() throws IOException { 
     ChromeOptions options = new ChromeOptions(); 
     options.addArguments("chrome.switches", "--disable-extensions"); 
     driver = new ChromeDriver(options); 
    } 

    @AfterClass 
    public void cleanup() { 
     if (driver != null) { 
      driver.quit(); 
     } 
    } 

    @Test 
    public void openURL() throws IOException { 
     driver.get("http://newtours.demoaut.com/"); 
     WebDriverWait myDynamicElement = new WebDriverWait(driver, 30); 
     myDynamicElement.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//input[@name='userName']"))); 
    } 

    @Test (dependsOnMethods = "openURL") 
    public void loginToTours() throws InterruptedException { 
     driver.findElement(By.xpath("//input[@name='userName']")).click(); 
    } 

} 
+0

謝謝,爲了完美的回答:-) – Tokci

+0

如果有幫助,你能接受我的回答嗎? –

0

你的XPath正在搜索input標籤,這是不存在在你的XML。

+0

參考輸入標記 – Tokci

+0

右鍵,您需要指定您正在解析的**確切XML **。你說你正在解析的XML沒有輸入標籤,而你隨機放入註釋的輸入標籤不是你問題中XML的一部分。 –

+0

您是否在尋找TestNG使用的XML或網站的HTML/CSS代碼?我粘貼的是HTML使用的網站和XML粘貼問題是TestNG XML ..我也給鏈接到網站 – Tokci

相關問題