my coding driver.findElement(By.linkText("Logout")).click(); is not executed in the test script

27,908

Try using driver.findElement(By.partialLinkText("Logout")); instead, because there could be other/whitespace characters that you aren't seeing. partialLinkText() only checks that the link contains your text, but linkText() checks if the link's whole text is equal to your text.

Also, be sure that the element containing the text "Logout" is an <a></a> - because linkText() and partialLinkText() won't check any other kind of element.

EDIT:

If the problem is that the element exists but isn't visible, then you'll need to wait for it to become visible:

// find the element
WebElement anchor = driver.findElement(By.partialLinkText("Logout"));

// create a wait with 10-second timeout
WebDriverWait wait = new WebDriverWait(driver, 10);

// wait for the element to become visible
anchor = wait.until(ExpectedConditions.visibilityOf(anchor));

// proceed with test...

Or, if the element isn't going to eventually become visible on its own, you can force it to be visible:

// find the element
WebElement anchor = driver.findElement(By.partialLinkText("Logout"));

// execute JavaScript to change the element's visibility
((JavascriptExecutor)driver).executeScript("arguments[0].style.visibility = 'visible'; arguments[0].style.display = 'block';", anchor);

// proceed with test...
Share:
27,908
sasa_samsudin
Author by

sasa_samsudin

Updated on June 24, 2020

Comments

  • sasa_samsudin
    sasa_samsudin almost 4 years

    I am a new tester, I got two question regarding my test script, I export my test script(export to Junit4 webdriver) from selenium ide and then paste it into my eclipse indigo. But when I run my test script, It will only run until
    driver.findElement(By.name("j_idt61")).click();

    After that line of code actually got one more line which is to click log out link driver.findElement(By.linkText("Logout")).click();

    but selenium seems can't find linkText "Logout"..

    this is my code

    package admin ;
    
    import java.util.regex.Pattern;
    import java.util.concurrent.TimeUnit;
    import org.junit.*;
    import static org.junit.Assert.*;
    import static org.hamcrest.CoreMatchers.*;
    import org.openqa.selenium.*;
    import org.openqa.selenium.firefox.FirefoxDriver;
    import org.openqa.selenium.support.ui.Select;
    
    public class login {
      private WebDriver driver;
      private String baseUrl;
      private boolean acceptNextAlert = true;
      private StringBuffer verificationErrors = new StringBuffer();
    
      @Before
      public void setUp() throws Exception {
        driver = new FirefoxDriver();
        baseUrl = " here I put my website URL";
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
      }
    
      @Test
      public void testLogin() throws Exception {
        driver.get(baseUrl + "/admin/pages/login.xhtml");
    
        driver.findElement(By.id("j_username")).click();
        driver.findElement(By.id("j_username")).clear();
        driver.findElement(By.id("j_username")).sendKeys("sasa");
        driver.findElement(By.id("j_password")).click();
        driver.findElement(By.id("j_password")).clear();
        driver.findElement(By.id("j_password")).sendKeys("sasasamsudin");
        driver.findElement(By.name("j_idt61")).click();
        driver.findElement(By.linkText("Logout")).click();
      }
    
      @After
      public void tearDown() throws Exception {
        driver.quit();
        String verificationErrorString = verificationErrors.toString();
        if (!"".equals(verificationErrorString)) {
          fail(verificationErrorString);
        }
      }
    
     public boolean isElementPresent(By id) {
        try {
          driver.findElements(By.id("J_idt16:J_idt30"));
          return true;
        } catch (org.openqa.selenium.NoSuchElementException e) {
          return false;
        }
      }
    
     public boolean isElementVisible(By id)
     {
    
         return driver.findElement(By.id("J_idt16:J_idt30")).isDisplayed();
    
     }
    
      private String closeAlertAndGetItsText() {
        try {
          Alert alert = driver.switchTo().alert();
          if (acceptNextAlert) {
            alert.accept();
          } else {`enter code here`
            alert.dismiss();
          }
          return alert.getText();
        } finally {
          acceptNextAlert = true;
        }
      }
    }
    

    Please anyone help me .I just start my work and really need this automation testing to be working properly ..THANKS :)

  • sasa_samsudin
    sasa_samsudin almost 11 years
    hai @Pat Meeker I changed my coding to paritalLinkText but still got error below : org.openqa.selenium.ElementNotVisibleException: Element is not currently visible and so may not be interacted with I checked the ""logout" is an <a></a> .. <a onclick="mojarra.jsfcljs(document.getElementById('j_idt16'),‌​{'j_idt16:j_idt30':'‌​j_idt16:j_idt30'},''‌​);return false" href="#">Logout</a>