Java – selenium 2 webdriver cannot find the link

I saw other problems about similar / same problems, but they didn't help me solve the problem: (. I logged in to the production website. Said( http://www.site.com/log )After that, I want to click on the link, but selenium can't find the link The relevant HTML sections are:

<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 as follows:

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 exception of "element not found"

thank you.

Solution

Try the following

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

I hope it works

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>