Choosing a link with Selenium Webdriver?

How can I select a link with selenium webdriver?

Selenium used to run:

selenium.click("link=Users"); 

But how can I do the same with webdriver?

I thought about

  driver.findElement(By.partialLinkText("Users")).click(); 

but it does not work. Link not 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 
+7
source share
9 answers

XPath is one of the most accurate ways to specify an element.

Try the following:

 driver.findElement(By.XPath("//li[@class='li_class']/a")).Click(); 
+7
source

This should work:

 driver.findElement(By.LinkText("Users")).click(); 

By LinkText is possible

+4
source

Using 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 
+3
source

I agree with Christophe - the link text should work. But I take a different approach, which works for me all the time.

All the element I need to find or select, I give them an identifier (without CSS there will be no difference in viewing). This helps in the readability of my test cases, writing functions for general material, and improves maintainability of the code. Only for dynamically generated code or places where I cannot use id, I use a different approach.

+1
source

I think this will work:

 driver.findElement(By.xpath("//a[@href='/user.xhtml']")).click(); 
0
source

I also had a problem that LinkText and LinkPartialText did not work. This was due to the fact that I am using the HTMLUnit driver. Using FireFox, both methods work fine.

0
source

In my case, the chronograph is not allowed to click on the link, because of the form, click. I was able to fix this 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 } 
0
source

I had a similar problem using PHP Webdriver. LinkText or partialLinkText did not work, but the text provided for the search was equal to this in the SOURCE code. (let's say it was the text of the link: "Users")

I scratched my head why it wasn’t working when it was everywhere. Then I saw the difference. In fact, there were users in the text of the link to the source code, but on the display it was changed css text-transform to lower case, so it was displayed as "users". When I changed the search criteria from users to users, it worked like harm!

So remember - webdriver really works with the data that it sees. I had no idea that it is case sensitive at all! But it really worked and solved my problem. Hooray!

0
source

Try the following:

 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(); } } 
-2
source

All Articles