Reputation: 485
I want to open a a link on a webpage. The link appears to be within a unordered list which resides within in a tag. The url to the web page is selftechy dot com. The tabs are home, about, selenium.
I attempted to open the link using driver.findElement(By.linkText("Selenium"));
but page seems like lost its styling. I also tried with xpath method, but it doesn't work either. Please explain to me why it doesn't work and how should I modify the code to make it work properly. Thanks for your help.
HTML code fragment:
<body class="custom">
<div id="container">
<div id="page">
<ul class="menu">
<li class="tab tab-home current"><a href="http://selftechy.com">Home</a></li>
<li class="tab tab-1"><a href="http://selftechy.com/about" title="About">About</a></li>
<li class="tab tab-2"><a href="http://selftechy.com/selenium-2" title="Selenium">Selenium</a></li>
</ul>
webdriver code to open the link
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.junit.*;
import org.junit.Before;
import org.junit.After;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
public class selftechyTestng
{
private WebDriver driver;
private String baseUrl;
@Before
public void setUp() throws Exception
{
driver = new FirefoxDriver();
baseUrl = "http://selftechy.com/";
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
}
@Test
public void searchElements() throws Exception{
driver.get(baseUrl);
//use By.linkText method the page lost its styling
driver.findElement(By.linkText("Selenium"));
//use xpath method to open the link doesn't work either
List<WebElement> elements = driver.findElements(By.xpath("//div[@id=page]/*[3]")).click();
driver.findElement(By.xpath("//div[@id=page]/*[3]")).click();
}
}
Upvotes: 2
Views: 35512
Reputation: 756
Below code will open the link in new Tab.
String selectLinkOpeninNewTab = Keys.chord(Keys.CONTROL,Keys.RETURN); driver.findElement(By.linkText("urlLink")).sendKeys(selectLinkOpeninNewTab);
Upvotes: 0
Reputation: 756
Below code will open the link in new window and prints the title and url of the newly opened window.
String defaultwindow = "";
@Test(description="Main Page")
public void UserOnMainPage()
{
driver.get("http://yoururl.com");
defaultwindow = driver.getWindowHandle();
String selectAll = Keys.chord(Keys.SHIFT,Keys.RETURN);
driver.findElement(By.linkText("linkname")).sendKeys(selectAll);
printTitleandUrlofNewlyOpenedwindow();
}
private void printTitleandUrlofNewlyOpenedwindow()
{
Set<String> windowHandles1 = driver.getWindowHandles();
int size = windowHandles1.size();
System.out.println(size);
for (String string : windowHandles1)
{
driver.switchTo().window(string);
if(string.equals(defaultwindow))
{
System.out.println("On Main Window");
Reporter.log("On Main Window");
}
else
{
String title=driver.getTitle();
System.out.println(title);
Reporter.log(title);
String recipeUrl = driver.getCurrentUrl();
System.out.println(recipeUrl);
Reporter.log(recipeUrl);
}
}
driver.switchTo().window(defaultwindow);
}
Upvotes: 0
Reputation: 1482
You can also use this xpath:
"//a[text()='Selenium']"
This will find the link with text = Selenium
Upvotes: 0
Reputation: 6218
Why do you search for the div and then the child element - Is there any particular reason? I don't see any advantage and certainly you are then not getting the a
element which you actually want to click. In my opinion it is much simpler to use
driver.findElement(By.xpath("//a[@title = 'Selenium']")).click();
Using your approach you have to use
driver.findElement(By.xpath("//div[@id = 'page']/ul/li[3]/a")).click();
Upvotes: 7