-1

I have already check and search for same question and there are lot of solution but no one is working for me so posing question here.

I am doing practice of selenium web driver. I am using this form for practice : http://www.toolsqa.com/automation-practice-form/

Now , I have 3 issues in that.

1 - There are first 2 links called "Partial link test" & "List test" which I am able to click on, using "findelement", but I want to open both link in NEW TAB in same browser.

2 - I am not able to upload file. My code is not working for that element.

3 - How can I select particular value from dropdown of "Continents"??

My code is given below :

    WebDriver driver = new FirefoxDriver();


    driver.get("http://www.toolsqa.com/automation-practice-form/");

    driver.manage().window().maximize();


    **driver.findElement(By.linkText("Partial Link Test")).click();
    driver.findElement(By.linkText("Link Test")).click();**


    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);


    driver.findElement(By.name("firstname")).sendKeys("Tester");
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

    driver.findElement(By.name("lastname")).sendKeys("Tester");
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);


    driver.findElement(By.id("sex-0")).click();
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

    driver.findElement(By.id("exp-2")).click();
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);



    driver.findElement(By.id("datepicker")).sendKeys("01/01/1985");
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);


    driver.findElement(By.id("profession-1")).click();
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);



   **driver.findElement(By.id("photo")).sendKeys("C:/Users/Public/Pictures/Sample Pictures/Desert.jpeg");**
   driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);


   driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
   Thread.sleep(600);


   driver.findElement(By.id("tool-0")).click();
   driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
   **driver.findElement(By.id("continents")).click();**
   driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

Please help to correct my code.

9
  • Could you please check that the new window is not in a different iframe Commented Nov 28, 2014 at 9:06
  • @SakshiSingla - Appreciate your time , there is not iframe. links open in window. Commented Nov 28, 2014 at 9:10
  • Yes first issue solved , but can you explain me that this line mean? Keys.chord(Keys.CONTROL,Keys.RETURN); Commented Nov 28, 2014 at 9:19
  • If you Ctrl+click a link in the browser, it will open the link in new tab in the same browser. Keys.chord - Simulate pressing many keys at once in a simultanewos manner(selenium.googlecode.com/git/docs/api/java/org/openqa/selenium/…) Commented Nov 28, 2014 at 9:22
  • Upload file: WebElement upload = driver.findElement(By.id("myfile")); upload.sendKeys("/Users/sso/the/local/path/to/darkbulb.jpg"); Commented Nov 28, 2014 at 9:25

2 Answers 2

3

I have added the answers inline to each of your questions, below. Also, an advice, is to use Implicit wait only once at the top while creating a browser instance, as its scope is the whole class itself. So, once declared, then selenium will wait a maximum of that time, for detecting an element. It can be rather overridden by using Explicit waits for certain elements, if necessary Please see this link for better understanding Implicit and Explicit waits:

1 - There are first 2 links called "Partial link test" & "List test" which I am able to click on, using "findelement", but I want to open both link in NEW TAB in same browser.

    //Clicking and opening Partial Link Text in new tab
    WebElement element = driver.findElement(By.linkText("Partial Link Test"));
    Actions act = new Actions(driver);
    act.contextClick(element).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ENTER).build().perform();

    //Clicking and opening Link Text in new tab
    element = driver.findElement(By.linkText("Link Test"));
    act.contextClick(element).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ENTER).build().perform();

2 - I am not able to upload file. My code is not working for that element.
The path of the file must be like this:

driver.findElement(By.id("photo")).sendKeys("C:\\Users\\Public\\Pictures\\Sample Pictures\\Desert.jpg");

3 - How can I select particular value from dropdown of "Continents"??

You can use Select class for that like below. It will select the option "Australia".

Select sel = new Select(driver.findElement(By.id("continents")));
sel.selectByVisibleText("Australia");
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much subh. All 3 issues you have solved for me. Thanks again.
2

Open link in new Tab:

String selectLinkOpeninNewTab = Keys.chord(Keys.CONTROL,Keys.RETURN); driver.findElement(By.linkText("urlLink")).sendKeys(selectLinkOpeninNewTab);

2 Comments

Yes its working now but m confusion is : here it is pressing CTRL + R key right? and if we press that manually it reloads page. Then how in code it works like Ctrl + T??
We are sending these keys on the link(Ctrl+R). Which means: Ctrl+Click the link. If you notice, we are not sending click to the element. So, Ctrl+R is doing the trick

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.