Xpath是正确的,仍然没有得到这样的元素:无法找到元素 [英] Xpath is correct still get no such element: Unable to locate element

查看:689
本文介绍了Xpath是正确的,仍然没有得到这样的元素:无法找到元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Xpath是正确的&没有iFrame,我可以在Chrome控制台中找到元素,但是我的程序仍然失败.我也使用了显式等待.

My Xpath is correct & no iFrame and I can locate element in Chrome console but my program still fails. I have used explicit wait also.

网站 http://newtours.demoaut.com/ 我试图找到登录页面并发送登录ID.

website http://newtours.demoaut.com/ I am trying to locate login page and send login id.

错误消息:

通过:openURL 失败:loginToTours ** org.openqa.selenium.NoSuchElementException **:**没有这样的元素:无法找到元素:{"method":"xpath","selector":"//input [@ name ='userName']"} ** ***元素信息:{Using = xpath,value =//input [@ name ='userName']}
PASSED: openURL FAILED: loginToTours **org.openqa.selenium.NoSuchElementException**: **no such element: Unable to locate element: {"method":"xpath","selector":"//input[@name='userName']"}** *** Element info: {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

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>

推荐答案

问题出在您的测试代码中.

The problem lies in your test code.

说明:测试类中有两个@Test方法.其中一个打开URL,等待元素出现,另一种方法只是搜索元素.如果TestNG最终首先运行您的loginToTours()测试方法,那就是您将看到此错误消息的时间.默认情况下,TestNG按字母顺序执行方法.由于loginToTours()没有指定findElement()将位于哪个网页中,因此Selenium最终在空白页上执行它,因此您会看到错误消息.修复方法之一是使loginToTours()方法依赖于openURL(),以便在搜索发生之前在浏览器中打开有效的网页. [这也正是我在清理代码中所做的].

Explanation : You have two @Test methods in your test class. One of them opens up a URL and waits for an element to be present and the other method which just searches for the element. If TestNG ends up running your loginToTours() test method first, that is when you will see this error message. By default TestNG executes methods in an alphabetical order. Since loginToTours() doesn't specify in which web page the findElement() is to be located, Selenium ends up executing it on a blank page and thus you see the error message. One of the fixes would be to have loginToTours() method depend on openURL() so that way there is already a valid web page opened up in the browser before the search happens. [ That is exactly what I have done in my cleaned up code as well ].

WebDriver在这里没有问题:)

WebDriver is at no fault here :)

这是测试代码的清理版本,可以正常工作

Here's a cleaned up version of your test code which will work fine

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();
    }

}

这篇关于Xpath是正确的,仍然没有得到这样的元素:无法找到元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆