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?

5
  • What error are you getting?
    – FDM
    Oct 31, 2015 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, 2015 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, 2015 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, 2015 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, 2016 at 12:53

2 Answers 2

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);
3
  • I tried with perform() already but couldn't help me out! See my last comment under the question.
    – Disha
    Nov 2, 2015 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 ? Nov 3, 2015 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, 2015 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

2
  • How does sendKeys has anything to do with visibility?
    – FDM
    Oct 13, 2016 at 8:36
  • Probably it forces a scroll and on-focus Mar 26, 2017 at 22:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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