【问题标题】:Xpath is correct still get no such element: Unable to locate elementXpath是正确的仍然没有这样的元素:无法定位元素
【发布时间】: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 标记在哪里?
  • 公开网站newtours.demoaut.com

标签: java selenium xpath selenium-webdriver selenium-chromedriver


【解决方案1】:

您的 XPath 正在搜索一个 input 标记,该标记不存在于您的 XML 中。

【讨论】:

  • 引用输入标签
  • 对,您需要在此处指定要解析的确切 XML。你说你正在解析的XML没有输入标签,而你刚才随机放在评论中的输入标签不是你问题中XML的一部分。
  • 您正在寻找 TestNG 使用的 xml 或网站的 HTML/CSS 代码?我粘贴的是使用网站的 HTML,粘贴的 xml 是 TestNG xml .. 我还给出了网站的链接
【解决方案2】:

问题在于您的测试代码。

解释:您的测试类中有两个@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();
    }

}

【讨论】:

  • 谢谢,完美的答案:-)
【解决方案3】:

我遇到了类似的问题,即使 XPath 有效(当进入检查模式时它能够找到元素),我也没有获得值。我的代码看起来有点像下面 -

driver.get("https://www.theurlgoeshere")
elements = driver.find_elements_by_xpath("//*[@class='classname']")
for element in elements:
    element.send_keys("Test value")
driver.close()

对于上面的代码,我遇到了找不到元素的错误。为了避免这个错误,我导入了“time”模块,并在get语句之后添加了一个休眠时间,通过这个小修改代码就可以工作了。

driver.get("https://www.theurlgoeshere")
time.sleep(3)
elements = driver.find_elements_by_xpath("//*[@class='classname']")
for element in elements:
element.send_keys("Test value")
driver.close()

当它继续进行并行处理时,驱动程序甚至在它出现在动态页面上之前就试图找到它。在搜索其中的 XPath 之前,该页面必须完全加载。

【讨论】:

    猜你喜欢
    • 2022-11-14
    • 1970-01-01
    • 2021-10-21
    • 2021-04-11
    • 2023-01-03
    • 2019-02-03
    • 1970-01-01
    • 2018-10-23
    • 1970-01-01
    相关资源
    最近更新 更多