【问题标题】:Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element: //input[@name='firstName'] [duplicate]线程“主”org.openqa.selenium.NoSuchElementException 中的异常:无法找到元素://input[@name='firstName'] [重复]
【发布时间】:2019-05-11 01:10:04
【问题描述】:
WebDriver driver=new FirefoxDriver(); 
Thread.sleep(3000); 
driver.get("https://www.google.com/gmail/about/"); 
driver.manage().timeouts().pageLoadTimeout(40,TimeUnit.SECONDS);     
//Clicking on Create account link
driver.findElement(By.xpath("//a[@href='https://accounts.google.com/SignUp?service=mail&continue=http%3A%2F%2Fmail.google.com%2Fmail%2F%3Fpc%3Dcarousel-about-en']")).click(); 
driver.manage().timeouts().pageLoadTimeout(40,TimeUnit.SECONDS); 
Assert.assertTrue(driver.findElement(By.xpath("//input[@name='firstName']")).isDisplayed()); 

线程“主”org.openqa.selenium.NoSuchElementException 中的异常: 无法定位元素://input[@name='firstName']

如何解决?

【问题讨论】:

  • 请添加相关HTML代码

标签: java selenium selenium-webdriver


【解决方案1】:

这是时间问题,表单需要一两秒才能加载。您可以添加显式等待来等待它

WebDriverWait wait = new WebDriverWait(WebDriverRefrence, 10);
WebElement firstName = wait.until(ExpectedConditions.presenceOfElementLocated(By.name("firstName")));
Assert.assertTrue(firstName.isDisplayed()); 

【讨论】:

  • 我试过使用explicitwait ..Still throw TimeoutException Exception in thread "main" org.openqa.selenium.TimeoutException: Expected condition failed: waiting for element located by: By.xpath: // *[id='firstName'](以 500 MILLISECONDS 间隔尝试 15 秒)在 org.openqa.selenium.support.ui.WebDriverWait.timeoutException(WebDriverWait.java:80) 在 org.openqa.selenium.support .ui.FluentWait.until(FluentWait.java:232) 在 srcpkg.Registration.main(Registration.java:35)
  • @Pradeep //*[id='firstName'] 无效 xpath。使用//*[@id='firstName'] 或By.id("firstName")
【解决方案2】:

看起来你的 xpath 可能是错误的。而不是:

//input[@name='firstName'] 

尝试使用:

//*[@id='firstName'] 

作为 xpath。

  • 您可以在 Chrome 上找到 xpath,方法是转到 Inspect Element,右键单击该元素并从“复制”子菜单中选择 Copy XPath 选项。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-02-22
    • 1970-01-01
    • 2019-08-18
    • 1970-01-01
    • 2019-09-20
    • 2017-05-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多