WebDriver with Java - click to open a link using xpath -
i want open a link on webpage. link appears within unordered list resides within in tag. url web page selftechy dot com. tabs home, about, selenium.
i attempted open link using driver.findelement(by.linktext("selenium"));
page seems lost styling. tried xpath method, doesn't work either. please explain me why doesn't work , how should modify code make work properly. 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 open 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 page lost styling driver.findelement(by.linktext("selenium")); //use xpath method open 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(); } }
why search div , child element - there particular reason? don't see advantage , not getting a
element want click. in opinion simpler use
driver.findelement(by.xpath("//a[@title = 'selenium']")).click();
using approach have use
driver.findelement(by.xpath("//div[@id = 'page']/ul/li[3]/a")).click();
Comments
Post a Comment