1

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?

  • What error are you getting? – FDM Oct 31 '15 at 8:31
  • I got such, org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"xpath","selector":"id('companyName-positionCompany-position-editPositionForm')"} – Disha Oct 31 '15 at 9:18
  • After trials, i found that it's working by changing pixel value from, jse.executeScript("window.scrollBy(0,250)", ""); to jse.executeScript("window.scrollBy(0,500)", ""); . So now i'm worried how can i define this pixel value?? Is there any way? – Disha Oct 31 '15 at 11:17
  • Try pausing for few seconds after you move the mouse cursor over the element and before you click it. – Nitesh Nov 2 '15 at 8:40
  • To avoid hard coding the pixel values you could create a for while to scroll piece by piece and put your test cases into a try catch block. 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. – Daniel Jun 30 '16 at 12:53
1

I would suggest to use perform() function on the main menu link to hold the menu list till the time Selenium identifies the required sub-menu link and then click on it. I have modified your code (just added a perform() function on line 2).Hope this helps.

Actions hover=new Actions(driver);
hover.moveToElement(driver.findElement(By.xpath("id('background-experience')/div[1]/div/button[1]"))).perform();
driver.findElement(By.xpath("id('background-experience')/div[1]/div/button[1]")).click();
Thread.sleep(5000);
  • I tried with perform() already but couldn't help me out! See my last comment under the question. – Disha Nov 2 '15 at 6:49
  • Since you are clicking the same element after moving to it. I have added click and perform method on the same line. Actions hover=new Actions(driver); hover.moveToElement(driver.findElement(By.xpath("id('background-experience')/div[1]/div/button[1]"))).click().perform(); Thread.sleep(5000); Do you mind trying this ? – JustARandomGuy Nov 3 '15 at 22:59
  • I tried with your given code.Thanks for your response, but unfortunately I got the same error. I thought its unable to scroll & locate the element so I've modified pixel value from jse.executeScript("window.scrollBy(0,250)", ""); to jse.executeScript("window.scrollBy(0,600)", ""); And surprisingly its working! – Disha Nov 5 '15 at 7:46
0

Simple way & working solution, When you want to take action on Hidden element

  1. First of all we know that element should be visible

    hidden_element.send_keys ""
    
  2. Once element get visible using selenium we can easily take any action

    hidden_element.click()
    

I hope code snippet will help you! Try it

  • How does sendKeys has anything to do with visibility? – FDM Oct 13 '16 at 8:36
  • Probably it forces a scroll and on-focus – Michael Durrant Mar 26 '17 at 22:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.