jeudi 14 novembre 2019

Unable to locate element in LinkedIn using Selenium

I'm trying to write my first program with Selenium that logs in into my profile and then search for specific people or companies. Everything work fine until I want to enter profile from search results. My code is here

 public static void main(String args[]){

   ChromeDriverManager.getInstance(DriverManagerType.CHROME).setup();

   WebDriver driver = new ChromeDriver();

   final String username = "username";
   final String password = "password";

   driver.get("https://www.linkedin.com/");
   driver.manage().window().maximize();

   driver.findElement(By.xpath("/html/body/nav/section[2]/form/div[1]/div[1]/input"))
           .sendKeys(username);

   driver.findElement(By.xpath("/html/body/nav/section[2]/form/div[1]/div[2]/input"))
           .sendKeys(password);

   driver.findElement(By.xpath("/html/body/nav/section[2]/form/div[2]/button"))
           .click();

   driver.findElement(By.xpath("//*[@id=\"ember31\"]/input"))
           .sendKeys("Bill Gates", Keys.ENTER);

   WebElement element = driver.findElement(By.xpath("//*[@id=\"ember503\"]/span/span[1]/span[1]"));
   element.click();
}

It gives me error Exception in thread "main" org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="ember503"]/span/span[1]/span[1]"}.

So, first I tried different xpathsand none of them worked. Then I tried cssSelector still nothing. Then I tried this

  WebDriverWait wait = new WebDriverWait(driver, 10);
  wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//[@id=\"ember503\"]/span/span[1]/span[1)));

And got another error Exception in thread "main" org.openqa.selenium.TimeoutException: Expected condition failed: waiting for element to be clickable: By.xpath: //*[@id="ember503"]/span/span[1]/span[1] (tried for 10 second(s) with 500 milliseconds interval).

So I discovered that selenium can't find every element that somehow takes time to load on the page. Can you help me understand where I made a mistake ?

Aucun commentaire:

Enregistrer un commentaire