java - Not able to click on an element even by using JavascriptExecutor scrolling -
below test script. not able click on element though scrolling window. tried using explicit wait. getting webdriverexception saying element not clickable @ point (588, 1611)
driver.manage().timeouts().implicitlywait(20, timeunit.seconds); int success = 0; driver.get("http://www.gcrit.com/build3/admin"); driver.findelement(by.name("username")).sendkeys("admin"); driver.findelement(by.name("password")).sendkeys("admin@123"); driver.findelement(by.id("tdb1")).click(); driver.findelement(by.xpath("//a[text()='categories/products']")).click(); javascriptexecutor jse = (javascriptexecutor)driver; jse.executescript("window.scrollto(0,3004)",""); string str = "mockingjay"; list <webelement> lt1 = driver.findelements(by.tagname("td")); iterator <webelement> it1 = lt1.iterator(); while(it1.hasnext()) { webelement el = it1.next(); if(el.gettext().contains(str)) { webdriverwait wait = new webdriverwait(driver, 20); wait.until(expectedconditions.elementtobeclickable(el)); el.click(); break; } } driver.findelement(by.xpath("//span[text()='edit']")).click(); if(driver.findelement(by.xpath("//td[text()='products status:']/following-sibling::td/input[1]")).isselected()) { system.out.println("changing product status 'out of stock'"); }
and source code. tried taking td tags
in list, iterate among them find element
, clicking on it.
<tr class="datatablerow" onmouseover="rowovereffect(this)" onmouseout="rowouteffect(this)" onclick="document.location.href='http://www.gcrit.com/build3/admin/categories.php?cpath=&pid=1391'"> <td class="datatablecontent"><a href="http://www.gcrit.com/build3/admin/categories.php?cpath=&pid=1391&action=new_product_preview"><img src="images/icons/preview.gif" border="0" alt="preview" title=" preview " /></a> led tv/52"</td> <td class="datatablecontent" align="center"> <img src="images/icon_status_green.gif" border="0" alt="active" title=" active " width="10" height="10" /> <a href="http://www.gcrit.com/build3/admin/categories.php?action=setflag&flag=0&pid=1391&cpath="><img src="images/icon_status_red_light.gif" border="0" alt="set inactive" title=" set inactive " width="10" height="10" /></a></td> <td class="datatablecontent" align="right"><a href="http://www.gcrit.com/build3/admin/categories.php?cpath=&pid=1391"><img src="images/icon_info.gif" border="0" alt="info" title=" info " /></a> </td> </tr> <tr class="datatablerow" onmouseover="rowovereffect(this)" onmouseout="rowouteffect(this)" onclick="document.location.href='http://www.gcrit.com/build3/admin/categories.php?cpath=&pid=1494'"> <td class="datatablecontent"><a href="http://www.gcrit.com/build3/admin/categories.php?cpath=&pid=1494&action=new_product_preview"><img src="images/icons/preview.gif" border="0" alt="preview" title=" preview " /></a> mock</td> <td class="datatablecontent" align="center"> <img src="images/icon_status_green.gif" border="0" alt="active" title=" active " width="10" height="10" /> <a href="http://www.gcrit.com/build3/admin/categories.php?action=setflag&flag=0&pid=1494&cpath="><img src="images/icon_status_red_light.gif" border="0" alt="set inactive" title=" set inactive " width="10" height="10" /></a></td> <td class="datatablecontent" align="right"><a href="http://www.gcrit.com/build3/admin/categories.php?cpath=&pid=1494"><img src="images/icon_info.gif" border="0" alt="info" title=" info " /></a> </td> </tr> <tr class="datatablerow" onmouseover="rowovereffect(this)" onmouseout="rowouteffect(this)" onclick="document.location.href='http://www.gcrit.com/build3/admin/categories.php?cpath=&pid=1507'"> <td class="datatablecontent"><a href="http://www.gcrit.com/build3/admin/categories.php?cpath=&pid=1507&action=new_product_preview"><img src="images/icons/preview.gif" border="0" alt="preview" title=" preview " /></a> mockingjay</td> <td class="datatablecontent" align="center"> <img src="images/icon_status_green.gif" border="0" alt="active" title=" active " width="10" height="10" /> <a href="http://www.gcrit.com/build3/admin/categories.php?action=setflag&flag=0&pid=1507&cpath="><img src="images/icon_status_red_light.gif" border="0" alt="set inactive" title=" set inactive " width="10" height="10" /></a></td> <td class="datatablecontent" align="right"><a href="http://www.gcrit.com/build3/admin/categories.php?cpath=&pid=1507"><img src="images/icon_info.gif" border="0" alt="info" title=" info " /></a> </td> </tr>
when write code this, try make things simple possible. put code going reused functions easier call , easier maintain.
in case, looping through tds looking element contains text matches product looking for. rather doing this, can search specific element using xpath.
//td[@class='datatablecontent'][contains(.,'mockingjay')]
you've got code scrolls, waits elements clickable, etc. none of necessary. in java/selenium, when attempt click on element, if it's not visible page scroll automatically.
i've looked @ code , broken distinct actions , written function each. wrote code used functions attempting do. working code , i'm not seeing element not clickable
error.
here functions
public static void deleteselectedcategory() { driver.findelement(by.cssselector("#tdb4 > span.ui-button-text")).click(); } public static void editselectedcategory() { driver.findelement(by.cssselector("#tdb3 > span.ui-button-text")).click(); } public static boolean isproductinstock(string productname) { return !driver.findelements(by.xpath("//tr[./td[@class='datatablecontent'][contains(.,'" + productname + "')]]//img[@src='images/icon_status_green.gif']")).isempty(); } public static void login(string username, string password) { driver.findelement(by.name("username")).sendkeys(username); driver.findelement(by.name("password")).sendkeys(password); driver.findelement(by.id("tdb1")).click(); } public static void selectcategory(string categoryname) { driver.findelement(by.xpath("//a[.='categories/products']")).click(); driver.findelement(by.xpath("//td[@class='datatablecontent'][contains(.,'" + categoryname + "')]")).click(); }
here code drives scenario
string productname = "mockingjay"; // in stock // string productname = "baloon"; // out of stock driver.get("http://www.gcrit.com/build3/admin"); login("admin", "admin@123"); selectcategory(productname); boolean instock = isproductinstock(productname); system.out.println("product in stock: " + instock); if (instock) { editselectedcategory(); system.out.println("changing product status 'out of stock'"); // set product out of stock }
btw, don't have edit product see if it's out of stock. red light/green light indicate whether it's out of stock. wrote code checks see if green light indicated on page , if is, edit product , added placeholder can add code change product out of stock, etc.
Comments
Post a Comment