簡體   English   中英

線程“main”中的異常org.openqa.selenium.NoSuchElementException:無法找到element:// * [@ id ='login-email']

[英]Exception in thread “main” org.openqa.selenium.NoSuchElementException: Unable to locate element: //*[@id='login-email']

我不得不重新測試xpath ,以前它工作正常,但現在它給了我一個錯誤。

我嘗試過不同的定位器,比如idname 但仍然得到相同的錯誤。

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("bandanakeela@yopmail.com");
        driver.findElement(By.xpath("//*[@id='login-password']")).sendKeys("keela");
        driver.findElement(By.xpath("//*[@id='login-form']/div[3]/div/button")).click();       
 }
}

當您訪問URL https://staging.keela.co/login會有一個阻止UI的Ajax加載器,因此我們必須等待Ajax加載器完成加載所有WebElements並且emailpassword字段變為可見。 為了實現這一點,我們將引入ExplicitWaitWebDriverWait其中ExpectedConditions設置為email字段的elementToBeClickable 。這是工作代碼塊:

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("bandanakeela@yopmail.com");
driver.findElement(By.xpath("//input[@id='login-password']")).sendKeys("keela");
driver.findElement(By.xpath("//button[@class='btn btn-sm btn-block btn-primary']")).click(); 

請嘗試以下代碼。

注意:如果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("bandanakeela@yopmail.com");
driver.findElement(By.id("login-password")).sendKeys("keela");
driver.findElement(By.xpath("//button[@type='submit'][text()='Log in']")).click();

該頁面使用js來構造元素。 所以我建議你使用phantomjs驅動程序。

然后你必須等到元素存在。 頁面加載時,您會看到齒輪圖標。 等到元素加載。 而且你也可以使用id而不是xpath,因為你知道你的元素id。

您可以選擇要使用的等待類型。 顯式等待或隱式等待。

是selenium文檔。

以及wait的示例代碼:

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"));

您正在打開URL,並在下一刻輸入email-id。 在輸入email-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("bandanakeela@yopmail.com");
driver.findElement(By.xpath("//*[@id='login-password']")).sendKeys("keela");
driver.findElement(By.xpath("//*[@id='login-form']/div[3]/div/button")).click();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM