2

I want to open a a link on a webpage. The link appears to be within a unordered list which resides within in a tag. The url to the web page is selftechy dot com. The tabs are home, about, selenium.

I attempted to open the link using driver.findElement(By.linkText("Selenium")); but page seems like lost its styling. I also tried with xpath method, but it doesn't work either. Please explain to me why it doesn't work and how should I modify the code to make it work properly. Thanks for your help.

HTML code fragment:

<body class="custom">
<div id="container">
<div id="page">
<ul class="menu">
<li class="tab tab-home current"><a href="http://selftechy.com">Home</a></li>
<li class="tab tab-1"><a href="http://selftechy.com/about" title="About">About</a></li>
<li class="tab tab-2"><a href="http://selftechy.com/selenium-2" title="Selenium">Selenium</a></li>
</ul>

webdriver code to open the link

import java.util.List;
import java.util.concurrent.TimeUnit;
import org.junit.*;

import org.junit.Before;
import org.junit.After;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;

public class selftechyTestng 
{
    private WebDriver driver;
    private String baseUrl;

    @Before
    public void setUp() throws Exception
    {
        driver = new FirefoxDriver();
        baseUrl = "http://selftechy.com/";
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    }
    @Test
    public void searchElements() throws Exception{
        driver.get(baseUrl);

            //use By.linkText method the page lost its styling
            driver.findElement(By.linkText("Selenium"));

        //use xpath method to open the link doesn't work either 
        List<WebElement> elements = driver.findElements(By.xpath("//div[@id=page]/*[3]")).click(); 
        driver.findElement(By.xpath("//div[@id=page]/*[3]")).click(); 
    }

}
1
  • You might have a problem with your xpaths. Before trying to debug your webdriver script, try using firepath to confirm that your xpath definitely works at identifying your screen element. Commented May 20, 2013 at 14:42

4 Answers 4

7

Why do you search for the div and then the child element - Is there any particular reason? I don't see any advantage and certainly you are then not getting the a element which you actually want to click. In my opinion it is much simpler to use

driver.findElement(By.xpath("//a[@title = 'Selenium']")).click();

Using your approach you have to use

driver.findElement(By.xpath("//div[@id = 'page']/ul/li[3]/a")).click(); 
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the reply. Both of your solutions worked. I see that @ is used for selecting the attribute within the HTML tag.
@dirkk: how would you have identified the same if title was not available as an attribute in html.
@KrishnaChandraTiwari If you have a new question, please ask an actual new question. It is too difficult to solve it here in the comments, as it totally depends how your HTML looks like - You might not have a title attribute, but you sure have something else to identify the element.
0

You can also use this xpath:

"//a[text()='Selenium']"

This will find the link with text = Selenium

Comments

0

Below code will open the link in new window and prints the title and url of the newly opened window.

    String defaultwindow = "";
@Test(description="Main Page")
public void UserOnMainPage()
{
        driver.get("http://yoururl.com");
        defaultwindow = driver.getWindowHandle();
        String selectAll = Keys.chord(Keys.SHIFT,Keys.RETURN);
        driver.findElement(By.linkText("linkname")).sendKeys(selectAll);
        printTitleandUrlofNewlyOpenedwindow();
}

private void printTitleandUrlofNewlyOpenedwindow() 
{
        Set<String> windowHandles1 = driver.getWindowHandles();
        int size = windowHandles1.size();
        System.out.println(size); 
        for (String string : windowHandles1) 
        {
           driver.switchTo().window(string);

           if(string.equals(defaultwindow))
           {
               System.out.println("On Main Window");
               Reporter.log("On Main Window");
           }
           else
           {
               String title=driver.getTitle();
               System.out.println(title);
               Reporter.log(title);  
               String recipeUrl = driver.getCurrentUrl();
               System.out.println(recipeUrl);     
               Reporter.log(recipeUrl);

           }
       }
       driver.switchTo().window(defaultwindow);
}

Comments

0

Below code will open the link in new Tab.

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

Comments

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.