user1315920
user1315920

Reputation: 23

Unable to find a link while running Selenium Test case

I am using Selenium webdriver. I can log in to the application, but while logging out it gets stuck, reason it cannot find logout link. I tried to find it byLink and byId. I have also tried using thread.sleep() but nothing seems to be working.

Logout link is present in all the pages.

HTML code:

<li>#{loginView.loggedInUser}>
    <ul><li><h:link value="Administration" outcome="Administration.xhtml" /></li>
    <li><h:commandLink value="Logout" actionListener="#{loginView.logout}">
      <f:param id="userName" value="#{loginView.username}" />
    </h:commandLink></li>
    </ul></li>

SELENIUM code:

Thread.sleep(5000); 

WebElement logOut = findElementByLinkText("Logout");

logOut.click();

assertEquals("Please sign in: ", findElementBySelector("h3.loginTitle.centerAlign").getText());

Upvotes: 1

Views: 546

Answers (2)

Khan
Khan

Reputation: 139

Use this code check how many links are present on page if it contains your logout link then you can click on it by using locator "linktext".

public void Link(){

        driver.get(baseUrl);
        HtmlTagFinder links = LinkFinder.links();
        List<WebElement> allLinks = (List<WebElement>) links.findFrom(driver);
        System.out.println(allLinks.size());
        int i = 1;
        for(WebElement link : allLinks){
            System.out.println(i);
            System.out.println(link.getText());
            i++;
        }

        driver.close();
        driver.quit();
    }

Upvotes: 2

Anand Somani
Anand Somani

Reputation: 801

driver.findElement(By.xpath("@value='Logout'")).click();

Upvotes: 0

Related Questions