Why my test is throwing Exception-Unable to locate element in webdriver?

前端 未结 5 999
鱼传尺愫
鱼传尺愫 2021-01-24 06:41
package testproject;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.*;
          


        
相关标签:
5条回答
  • 2021-01-24 07:22

    I faced similar problem, issue resolved after setting timeout.

    Webdriver driver = new FirefoxDriver();
    driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);
    

    Not sure whats the role of timeout here though.

    0 讨论(0)
  • 2021-01-24 07:22

    remove the dot(.) and star(*) from the xpath and give proper tag name in place of star.

    for example if @id=gb is the id of div element, place div in place of star. Hope it will work.

    0 讨论(0)
  • 2021-01-24 07:38
    //launch browser 
    FirefoxDriver driver = new FirefoxDriver(options); 
    driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);
    //gmail login :
    driver.get("http://www.gmail.com");
    driver.findElement(By.id("identifierId")).sendKeys("****",Keys.ENTER);
    Thread.sleep(5000);
    driver.findElement(By.id("password")).sendKeys("***",Keys.ENTER);
    //logout:
    driver.findElement(By.xpath("//div[@id='gb']/div[1]/div[1]/div[2]/div[4]/div[1]/a/span")).click();
    Thread.sleep(5000);
    driver.findElement(By.linkText("Sign out")).click();
    
    0 讨论(0)
  • 2021-01-24 07:44

    Remove the dot at the beginning of your xpath expression. That way you have an xpath expression thaty could match everything. With the dot at the beginning you might retrict yourself depending on if the current node is the root node or not. Ther eis no way to know it. Just the fact the dot can only give you trouble. Unfortunately you cannot always trust what tools like firebug give you (it is still true in 99% of the case).

    Of course, ensure that the elemetns you are targeting are already on the screen as suggested by the previous answer.

    0 讨论(0)
  • 2021-01-24 07:45

    Are you certain your page is completely loaded after you sign in?

    Did you set a timeout for your webdriver? (how long it has to wait for elements). Probably it reads your html before it's completey loaded.

    Webdriver driver = new FirefoxDriver();
    driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);
    

    To find out quickly if this is the problem do Thread.sleep(8000) after you do wb2.click();

    0 讨论(0)
提交回复
热议问题