Reputation: 86905
How can I select a link with selenium webdriver?
Selenium before would be done by:
selenium.click("link=Users");
But how can I do the same with webdriver?
I thought about
driver.findElement(By.partialLinkText("Users")).click();
but this does not work. No link is clicked!
<html>
<body>
<div id="mainpage" class="mainpage">
<div id="pageid" class="pageid">
<div id="body">
<table>
<tbody>
<tr>
<td>
<table>
<tbody>
<tr>
<td>
<div id="id_menu" class="mymenu">
<ul>
<li class="li_class ">
<a href="/user.xhtml">Users</a>
stacktrace:
org.openqa.selenium.NoSuchElementException: Unable to locate element:
{"method":"partial link text","selector":"Users"} Command duration or timeout: 11.36
seconds For documentation on this error, please visit: http://seleniumhq.org/exceptions
/no_such_element.html Build info: version: '2.24.1', revision: '17205', time: '2012-06-19
17:28:14' System info: os.name: 'Windows XP', os.arch: 'x86', os.version: '5.1',
java.version: '1.7.0_02' Driver info: driver.version: RemoteWebDriver Session ID:
178449d1-4ff3-44f3-b35c-6a158db7c430 error at line: 34
Upvotes: 7
Views: 57158
Reputation: 504
Using a CSS selector:
a[href*=user.xhtml]
Here are some tips for writing cssSelector
= --> Equals string
^= --> Starts with string
$= --> Ends with string
*= --> Contains
~= --> Contains in a list
Upvotes: 3
Reputation: 1
I had similar problem using PHP Webdriver. LinkText or partialLinkText weren't working, but text provided for search was equal to this in SOURCE code. (lets say it was link text: "Users")
I was scratching my head why it is not working, when everywhere else it was. Then I saw the difference. In fact in source code link text was Users, but on display it was modified by css text-transform to lower case, so it was displayed as "users". When I changed my search criteria from Users to users it worked like harm!
So remember - webdriver actually works on data it see. Didn't have idea it is case sensitive at all! But it actually worked and solved my problem. Cheers!
Upvotes: 0
Reputation: 108
XPath is one of the most exact ways to point the element.
Try this:
driver.findElement(By.XPath("//li[@class='li_class']/a")).Click();
Upvotes: 7
Reputation: 1
I think this will work:
driver.findElement(By.xpath("//a[@href='/user.xhtml']")).click();
Upvotes: 0
Reputation: 1
Try this out:
package mypack;
import java.util.List;
import org.openqa.selenium.By;
import mypackage.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
@SuppressWarnings("unused")
public class classnew {
private static FirefoxDriver driver;
public static void main(String[] args) {
//String baseUrl = "http://newtours.demoaut.com/";
FirefoxDriver Firefoxdriver = new FirefoxDriver();
driver = null;
driver.get("http://newtours.demoaut.com");
String linkText1 = driver.findElement(By.partialLinkText("egis")).getText();
System.out.println(linkText1);
String linkText2 = driver.findElement(By.partialLinkText("EGIS")).getText();
System.out.println(linkText1);
driver.quit();
}
}
Upvotes: -2
Reputation: 2709
In my case chromedriver not allowed to click link, due to form obtain click. I was able to fix this by using:
if(driver.toString().contains("chrome")) {
WebElement form=driver.findElement(By.id("form_id"));
((JavascriptExecutor)driver)
.executeScript("arguments[0].setAttribute('style', 'display: block;')", form); //here I change visibility of element
}
Upvotes: 0
Reputation: 1
I also had the issue that LinkText and LinkPartialText didn´t work. This was because I uset then HTMLUnit Driver. Using FireFox both methods works fine.
Upvotes: 0
Reputation: 3414
I agree with Christoph - Link text should work. But I follow a different approach which works for me all the time.
All the element that I need to locate or select I give them an id (without CSS there won't be any difference in view). This helps in readability of my test cases, writing functions for general stuff and improves maintainability of the code. Only for the dynamic generated code or places where I can't use id I go for different approach.
Upvotes: 1
Reputation: 9345
This should work:
driver.findElement(By.LinkText("Users")).click();
By LinkText is possible
Upvotes: 8