Here I'm using LinkedIn for the learning purpose & want to perform the action on Edit profile=> Work experience=> Add position. Here "Add position" button comes under work experience which becomes visible on hovering to that specific area. Look at my code,
driver.findElement(By.id("login-email")).clear();
driver.findElement(By.id("login email")).sendKeys("email id");
driver.findElement(By.id("login-password")).clear();
driver.findElement(By.id("login-password")).sendKeys("password");
driver.findElement(By.name("submit")).click();
driver.findElement(By.linkText("Home")).click();
driver.findElement(By.linkText("Profile")).click();
Thread.sleep(3000);
/* To scroll the page down */
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("window.scrollBy(0,250)", "");
/* To hover mouse on required option*/
Actions hover=new Actions(driver);
hover.moveToElement(driver.findElement(By.xpath("id('background-experience')/div[1]/div/button[1]")));
driver.findElement(By.xpath("id('background-experience')/div[1]/div/button[1]")).click();
Thread.sleep(5000);
/* To Edit company name */
driver.findElement(By.xpath("id('companyName-positionCompany-position-editPositionForm')")).clear();
driver.findElement(By.xpath("id('companyName-positionCompany-position-editPositionForm')")).sendKeys("testing company");
This is continued to edit work details but when I execute, it stops the page at work experience & not able to click on add position button to edit work. It gives error on "edit company" code line. Can anyone help me perform the same?
Boolean Performed = false; while(Performed == false){ try{ //whatever you want it to do Performed = true; }catch(NoSuchElementException e){ se.executeScript("window.scrollBy(0,50)", "");} //or whatever value you want }
This would try to perform the specified action and scroll down by 50 pixels if it failed, so after 10 iteration in your case it would go through.