3

I saw the other questions regarding similar/same issues but they did not help me solve the problem :(. I log into the production site . say ( http://www.site.com/log) . I want to click on a link after that but Selenium is not able to find the link. The relevant HTML part is :

<div style="display:none" id="managers">
             <a class="projectManager" style="color:black"> Project Manager</a>

             <a class="transportManager"> Transport Manager</a>
         </div>

The java code is below:

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.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.ui.Select;

public class test {
    private WebDriver driver;
    private String baseUrl="";
    private StringBuffer verificationErrors = new StringBuffer();
    @Before
    public void setUp() throws Exception {
        //driver = new FirefoxDriver();
        //driver.manage().timeouts().implicitlyWait(45, TimeUnit.SECONDS);
        DesiredCapabilities chromeCapabilities = DesiredCapabilities.chrome();

        String chromeBinary = System.getProperty(" ");
        if (chromeBinary == null || chromeBinary.equals("")) {
            String os = System.getProperty("os.name").toLowerCase().substring(0, 3);
            chromeBinary = "lib/chromedriver-" + os + (os.equals("win") ? ".exe" : "");
            System.setProperty("webdriver.chrome.driver", chromeBinary);
        }
        driver=new ChromeDriver(chromeCapabilities);
        driver.manage().timeouts().implicitlyWait(70,TimeUnit.SECONDS);
    }

    @Test
    public void testEmployee() throws Exception {
        driver.get("http://www.site.com/log");
        driver.findElement(By.name("j_username")).clear();
        driver.findElement(By.name("j_username")).sendKeys("username");
        driver.findElement(By.name("j_password")).clear();
        driver.findElement(By.name("j_password")).sendKeys("password");
        driver.findElement(By.cssSelector("input[name=\"login\"]")).click();
        Thread.sleep(10000);

        driver.findElement(By.linkText(" Project Manager")).click();
        driver.findElement(By.linkText("Sign Out")).click();
        System.out.println("Test done");
        }
    @After
    public void tearDown() throws Exception {
        driver.quit();
        String verificationErrorString = verificationErrors.toString();
        if (!"".equals(verificationErrorString)) {
            fail(verificationErrorString);
        }
    }

    private boolean isElementPresent(By by) {
        try {
            driver.findElement(by);
            return true;
        } catch (NoSuchElementException e) {
            return false;
        }
    }
}

Question: What is the error ? It gives an "element not found" exception

Thanks.

crazyaboutliv
  • 3,029
  • 9
  • 33
  • 50

3 Answers3

6

Try using the following..

driver.findElement(By.partialLinkText(" Project Manager")).click();
driver.findElement(By.partialLinkText("Sign Out")).click();

Hope this works.

vidit
  • 6,293
  • 3
  • 32
  • 50
Amit Horakeri
  • 747
  • 5
  • 18
  • Hey , this is what I did. But, I am wondering why did this work and what not the rest :( ? – crazyaboutliv Oct 10 '11 at 14:09
  • Okay... Listen, this is how it is.. When u specify By.linkText("somelement"); here "somelement" should be as it is specified in the code, I mean to say, In your code there is a link called Project Manager, which may contain some &nbsp(space) characters. So the selenium driver matches for the exact search and hence its not finding that element. So in such cases you should try to partially match the element, which is done using By.partialLinkText("SomeElement"). Great to hear that its working well.. – Amit Horakeri Oct 11 '11 at 04:48
  • 6 years later, and in VBA, this did the trick! I'd never noticed the "PartialLink" as an option but it was exactly what I was looking for. Thanks, @AmitHorakeri! – FreeMan May 22 '17 at 12:13
1

My Understanding: I think here you are looking for the link "Project Manager".

Step 1: Link Text Won't Support Always.So Go with CSS Selector

CSS Selector for Project Manager:css=.projectManager

Step 2:

Perform the Click

driver.findElement(By.cssSelector(".projectManager")).click();

AlterNative Methods to click

driver.findElement(By.className("projectManager")).click();
driver.findElement(By.partialLinkText("Project Manager")).click();

If you have a CSS class you should prefere this solution to finding an element by the link text as it is far more stable and can also be used on multi language UIs

Thanks!

Stefan
  • 135
  • 4
user3487861
  • 340
  • 2
  • 2
0

What about removing the blank in the link text string? You have

driver.findElement(By.linkText(" Project Manager")).click();

try

driver.findElement(By.linkText("Project Manager")).click();

HTML doesn't count spaces.

Nick.T
  • 587
  • 4
  • 20