2017-09-13 50 views
1

我不得不重新测试xpath,以前它工作正常,但现在它给了我一个错误。线程“主”异常org.openqa.selenium.NoSuchElementException:无法找到元素:// * [@ id ='login-email']

我试过不同的定位器,像id,name。但仍然得到相同的错误。

package staging; 

import org.openqa.selenium.By; 
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.firefox.FirefoxDriver; 

public class login { 

    public static void main (String[]args){ 
     System.setProperty("webdriver.gecko.driver","C:\\Program Files\\geckodriver.exe"); 
     WebDriver driver = new FirefoxDriver(); 

     //opening the browser 
     driver.get("https://staging.keela.co/login"); 

     //logging 
     driver.findElement(By.xpath("//*[@id='login-email']")).sendKeys("[email protected]"); 
     driver.findElement(By.xpath("//*[@id='login-password']")).sendKeys("keela"); 
     driver.findElement(By.xpath("//*[@id='login-form']/div[3]/div/button")).click();  
} 
} 
+0

你确定的ID是'登录,email'?可能是一个错字..你有测试页面的源代码截图吗? – jaredgilmore

+0

好的我已经发布了它可以在左侧看到它(顶部)@jaredgilmore –

+0

对不起,我的意思是你正在测试的网页的源代码,而不是测试。 – jaredgilmore

回答

1

当你访问的网址https://staging.keela.co/login还有阿贾克斯装载机,其阻断UI,所以我们必须等待阿贾克斯装载机完成加载所有WebElements和emailpassword现场变得可见。为了实现这一目标,我们将介绍ExplicitWaitWebDriverWaitExpectedConditions设置为elementToBeClickableemail field.Here是工作代码块:

System.setProperty("webdriver.gecko.driver","C:\\Utility\\BrowserDrivers\\geckodriver.exe"); 
WebDriver driver = new FirefoxDriver(); 
driver.get("https://staging.keela.co/login"); 
WebDriverWait wait = new WebDriverWait (driver, 15); 
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@id='login-email']"))); 
element.sendKeys("[email protected]"); 
driver.findElement(By.xpath("//input[@id='login-password']")).sendKeys("keela"); 
driver.findElement(By.xpath("//button[@class='btn btn-sm btn-block btn-primary']")).click(); 
+0

你能解释一下,你为什么在这里使用'xpath'定位器。如果存在'id'属性。这对我来说没有任何意义.. !!!并且对于您的实物信息,“id”属性是查找“webelement”最快的定位器之一。 –

0

该页面使用js来构造元素。所以我建议你使用phantomjs驱动。

然后你必须等到元素存在。加载页面时会看到齿轮图标。等到元素加载。并且你可以使用id而不是xpath,因为你知道你的元素ID。

您可以选择要使用的等待类型。显式等待或隐式等待。

Here是硒文档。对于等待

和示例代码:

WebElement myDynamicElement = (new WebDriverWait(driver, 10)) 
    .until(ExpectedConditions.presenceOfElementLocated(By.id("login-email"))); 

,或者你可以等到页面加载:

new WebDriverWait(firefoxDriver, pageLoadTimeout).until(
      webDriver -> ((JavascriptExecutor) webDriver).executeScript("return document.readyState").equals("complete")); 
+0

感谢它的工作,但只有部分的sendkeys..data提供,但登录按钮没有点击 –

+0

我刚刚添加了等待的例子,这就是为什么:)你无法看到的元素和你的问题的答案等待,直到元素是存在。 – 2017-09-14 19:28:58

0

您正在打开的网址,并在第二天进入那一刻电子邮件ID。在输入电子邮件ID之前,您需要检查页面是否已完全加载。在这种情况下,明确的等待将帮助你 -

//opening the browser 
driver.get("https://staging.keela.co/login"); 

//Explicit wait 

WebDriverWait wait = new WebDriverWait(WebDriverRefrence,20); 
WebElement email; 
email = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("login-email"))); 

//logging 
driver.findElement(By.xpath("//*[@id='login-email']")).sendKeys("[email protected]"); 
driver.findElement(By.xpath("//*[@id='login-password']")).sendKeys("keela"); 
driver.findElement(By.xpath("//*[@id='login-form']/div[3]/div/button")).click(); 
1

试试下面的代码。

注:如果id属性是可用的,那么你应该使用idxpath尝试使用相对xpath

我已经使用了明确的等待方法,因此您的驱动程序可能会在页面完全加载后找到下一个webelement

driver.get("https://staging.keela.co/login"); 
driver.manage().window().maximize(); 

//Explicit wait for 60 seconds, to find the webelement. You can increase or decrease the time as per your specification.   
new WebDriverWait(driver, 60).until(ExpectedConditions.elementToBeClickable(driver.findElement(By.id("login-email")))); 
driver.findElement(By.id("login-email")).sendKeys("[email protected]"); 
driver.findElement(By.id("login-password")).sendKeys("keela"); 
driver.findElement(By.xpath("//button[@type='submit'][text()='Log in']")).click(); 
+0

它不工作..same错误,如:无法找到元素:#login \ -email –

相关问题