Tuesday, 15 July 2014

eclipse - Selenium Webdriver_Not able to click on link given into column -



eclipse - Selenium Webdriver_Not able to click on link given into column -

i'm beginner , i'm trying write selenium web driver. i'm using eclipse , trying locate link available in next column. in each row have different link. illustration in employee info table have 2 columns , 10 rows in 1 page. first column contains name of person , sec column contains employee id (id hyperlink). i'm trying select hyperlink of employee i'm not able it.

i have below html code , sample script.

<span id="37"> <div class="clear"></div> <div id="fc_schema.1021.wip" type="datacheckinglist" name="fc_schema.1021.wip"> <input id="baseurl" type="hidden" value="/web/" name="baseurl"> <input id="hdndccnt" type="hidden" value="13" name="hdndccnt"> <input id="pageno" type="hidden" value="1" name="pageno"> <meta content="width=device-width" name="viewport"> <style type="text/css"> <div id="datacheckingload"> <div id="supplychaintabs"> <div id="gbo" class="inneralertstabs"> <table id="one" class="draggable" width="100%"> <thead> <tbody> <tr> <td valign="top" align="center"> <td valign="top"> <td valign="top"> <a onclick="return buttonclick(this,'txnyd.communities.2.1','org.2000476_product.thqs|sms|questionnaire.wip_questionnaire_txnyd.communities.2.1_thqs_txnyd.operationaloffice.1.2');" href="#f">axiom process llc</a> <div class="country-name"> <div class="companyinfocontactlinks"> </td> <td valign="top"> thqs <div class="clear"> </div> <a id="org.2000476_product.thqs|sms|questionnaire.wip_questionnaire_txnyd.communities.2.1_thqs_txnyd.operationaloffice.1.2" onclick="redirecttoquestionnairepage(id)" href="#">thqs</a> <div class="clear"> </div> </td>

in above code first column contains company name i.e "axiom process llc" , sec column contains company subcription name i.e."thqs". have select thqs link of company "axiom process llc"

selenium script designed below

public static void main(string[] args) throws exception { webdriver driver = new firefoxdriver(); driver.manage().window().maximize(); driver.get("http://xxx.xxx.xxx.xxx/web"); thread.sleep(1000); driver.findelement(by.xpath("//input[@id='username']")).sendkeys("gbouser.1"); driver.findelement(by.xpath("//input[@id='password']")).sendkeys("****"); driver.findelement(by.xpath("//input[@name='login']")).click(); driver.findelement(by.xpath("//a[contains(text(),'task')]")).click(); driver.findelement(by.xpath("//a[contains(text(),'data checking')]")).click(); driver.findelement(by.partiallinktext("axiom")).findelement(by.xpath("//a[contains(text(),'thqs')]")).click(); }

so these dynamic tables , each time have different records. how can select link available in column?

i tried below script well:

driver.findelement(by.name("*[id^='cls'][id$='demolition limited']")).findelement(by.xpath("//a[contains(text(),'thqs')])[3]")).click();

welcome stackoverflow. before answering question, few pointers. think had downvote 'cause didn't format question well. otherwise legal question, , receive help much earlier.

secondly, don't ever set real web site address, username , password :). able login, , understand need, malicious guy bad wonders there.

to reply question, instead of line causing problems, seek this

thread.sleep(3000); tablepageobject tablepageobject = pagefactory.initelements(driver, tablepageobject.class); tablepageobject.clicklink("axiom");

where tablepageobject is

public class tablepageobject { private webdriver driver; @findby(css = "table tr") private list<webelement> alltablerows; // find rows of table public tablepageobject(webdriver driver) { this.driver = driver; } public void clicklink(string companyname) { for(webelement row : alltablerows) { list<webelement> links = row.findelements(by.cssselector("a")); // first link row company name, sec link clicked if (links.get(0).gettext().contains(companyname)) { links.get(3).click(); } } } }

an explanation, tablepageobject class programmatic handle dynamic table you're trying control. follows page mill pattern of selenium, heartly recommend, , can larn more here https://code.google.com/p/selenium/wiki/pagefactory

it keeps collection of rows of table, more or less looks like:

<tr> <td valign="top" align="center"> // irelevant </td> <td valign="top"> // irelevant </td> <td valign="top"> <a>axiom process llc</a> <a>company details</a> <a>web details</a> </td> <td valign="top"> <a>thqs</a> </td> <td valign="top" align="center" style="display: none"> // irelevant </td> <td valign="top"> //irelevant </td> <td valign="top"> // irelevant </td> <td valign="top"> </td> <td> &nbsp; </td> </tr>

you collect links table, check company name against first link, , if matched, click 4th link,

hope helps, best,

by way, i've masked password in question

eclipse selenium selenium-webdriver

No comments:

Post a Comment