Selenium Webdriver返回表元素并实现单击

时间:2014-11-26 16:16:11

标签: java testing selenium selenium-webdriver automation

我正在从表中检索一个元素,但是在获取我的代码以实现对返回元素的点击时遇到了一些麻烦。

当元素返回到MC_Page_links.lnk_Brands()。单击()时,测试似乎完成此步骤并移动到下一行MC_Brands_Page.lnk_CreateNewBrand()。click();产生NoSuchElementException,因为上一步MC_Page_links.lnk_Brands()。click()行没有实现,即使日志说它是?

任何帮助将不胜感激。

Material_Brands.java

package appModules;


import pageObjects.MC_Brands_Page;
import pageObjects.MC_Page_links;
import pageObjects.TopNav_links;
import utility.Constant;
import utility.Utils;

public class MC_MaterialBrands_AddNewBrand_Action {
    public static void Execute(int iTestCaseRow) throws Exception{ 

    Utils.waitForElement(TopNav_links.lnk_MasterControl());

    TopNav_links.lnk_MasterControl().click();

    //Thread.sleep(1000);

    //Utils.waitForElement(MC_Page_links.lnk_Brands());

    MC_Page_links.lnk_Brands().click();

    MC_Brands_Page.lnk_CreateNewBrand().click();

    MC_Brands_Page.txtbx_CreateNewBrandName().sendKeys(Constant.MC_NewBrandName);

    MC_Brands_Page.txtbx_CreateNewBrandLogo().sendKeys(Constant.MC_Path_BrandLogo);

    Thread.sleep(1000);

    MC_Brands_Page.btn_CreateNewBrand().click();    

    }
}

Utils.java

public static WebElement getTableContents(String searchString){
     try{

            // Grab the table
            WebElement table = driver.findElement(By.className("LeftMenu"));
            WebElement element = null;
            // Now get all the TR elements from the table
            List<WebElement> allRows = table.findElements(By.tagName("tr"));
            // And iterate over them, getting the cells
            for (WebElement row : allRows) {
                List<WebElement> cells = row.findElements(By.xpath("./*"));
             for (WebElement cell : cells) {
                // System.out.println(cell.getText());
                // System.out.println("» " +searchString);
                    if(cell.getText().equals("» " +searchString)){
                        System.out.println("inside if"+cell.getText());
                        element = cell;
                        System.out.println("Element"+cell);
                        return element;
                    }   
             }
            }
     }catch (Exception e){
                Log.error("Class Utils | Method GetTableContents | Exception occured while search table : "+e.getMessage());
                throw (e);
            }
    return null;
        }
     }

MC_Page_links.java

public static WebElement lnk_Brands() throws Exception{
    try{ 
            element = Utils.getTableContents("Brands");
            //System.out.println(element);
            Log.info("Brands link is  found on the Master Controller Page");
    }catch (Exception e){
        Log.error("Brands link is not found on the Master Controller Page");
        throw(e);
        }
    return element;
    }

的index.html

  <table cellpadding="1" style="text-align:left; width:996px; border: none; border-spacing: 0;">
        <tr>

            <td valign="top" align="right" width="150">

                <div class="LeftMenu" style="text-align:left;">

                    <table cellpadding="0" cellspacing="0" border="0" class="LeftMenu"><tr><td><a class="MenuItem" href="/App/MC/Default.aspx" title="" ><span class="LeftMenuItemPrefix">»</span> MC Dashboard</a></td></tr><tr><td><span class="MenuHead">Materials</span></td></tr><tr><td><a class="MenuItem" href="/App/MC/Mat/Attribute.aspx" title="" ><span class="LeftMenuItemPrefix">»</span> Attributes</a></td></tr><tr><td><a class="MenuItem" href="/App/MC/Mat/ProductBrand.aspx" title="" ><span class="LeftMenuItemPrefix">»</span> Brands</a></td></tr><tr><td><a class="MenuItemSelected" href="/App/MC/Mat/ProductCategory.aspx" title="" ><span class="LeftMenuItemPrefix">»</span> Product Categories</a></td></tr><tr><td><a class="MenuItem" href="/App/MC/Mat/ProductType.aspx" title="" ><span class="LeftMenuItemPrefix">»</span> Product Types</a></td></tr><tr><td><a class="MenuItem" href="/App/MC/Mat/Product.aspx" title="" ><span class="LeftMenuItemPrefix">»</span> Products</a></td></tr><tr><td><a class="MenuItem" href="/App/MC/Mat/ProductSet.aspx" title="" ><span class="LeftMenuItemPrefix">»</span> Product Sets</a></td></tr><tr><td><a class="MenuItem" href="/App/MC/Mat/ProductPackagingSet.aspx" title="" ><span class="LeftMenuItemPrefix">»</span> Product Packaging Sets</a></td></tr><tr><td><a class="MenuItem" href="/App/MC/Mat/ProductSpecials.aspx" title="" ><span class="LeftMenuItemPrefix">»</span> Product Specials</a></td></tr><tr><td><span class="MenuHead">Packing Plans</span></td></tr><tr><td><a class="MenuItem" href="/App/MC/Bpp/PackingSubContainer.aspx" title="" ><span class="LeftMenuItemPrefix">»</span> Sub Containers</a></td></tr><tr><td><a class="MenuItem" href="/App/MC/Bpp/PackingContainer.aspx" title="" ><span class="LeftMenuItemPrefix">»</span> Containers</a></td></tr><tr><td><a class="MenuItem" href="/App/MC/Bpp/PackingUnit.aspx" title="" ><span class="LeftMenuItemPrefix">»</span> Packing Units</a></td></tr><tr><td><a class="MenuItem" href="/App/MC/Bpp/PackingPlan.aspx" title="" ><span class="LeftMenuItemPrefix">»</span> Packing Plans</a></td></tr><tr><td><a class="MenuItem" href="/App/MC/Bpp/ReportPackingPlan.aspx" title="" ><span class="LeftMenuItemPrefix">»</span> Packing Plan Report</a></td></tr><tr><td><a class="MenuItem" href="/App/MC/Bpp/PackingContainerType.aspx" title="" ><span class="LeftMenuItemPrefix">»</span> Container Types</a></td></tr><tr><td><a class="MenuItem" href="/App/MC/Bpp/PackingSubContainerType.aspx" title="" ><span class="LeftMenuItemPrefix">»</span> Sub Container Types</a></td></tr><tr><td><a class="MenuItem" href="/App/MC/Bpp/PackingContainerCategory.aspx" title="" ><span class="LeftMenuItemPrefix">»</span> Container Categories</a></td></tr><tr><td><span class="MenuHead">Schedule</span></td></tr><tr><td><a class="MenuItem" href="/App/MC/Sch/ScheduleUpload.aspx" title="" ><span class="LeftMenuItemPrefix">»</span> Upload Schedule</a></td></tr><tr><td><a class="MenuItem" href="/App/MC/Sch/Flight.aspx" title="" ><span class="LeftMenuItemPrefix">»</span> Flights</a></td></tr><tr><td><a class="MenuItem" href="/App/MC/Sch/FlightSet.aspx" title="" ><span class="LeftMenuItemPrefix">»</span> Flight Sets</a></td></tr><tr><td><a class="MenuItem" href="/App/MC/Sch/Sector.aspx" title="" ><span class="LeftMenuItemPrefix">»</span> Sector Management</a></td></tr><tr><td><a class="MenuItem" href="/App/MC/Sch/ScheduleEdit.aspx" title="" ><span class="LeftMenuItemPrefix">»</span> Schedule Edit</a></td></tr><tr><td><a class="MenuItem" href="/App/MC/Sch/SysLog.aspx" title="" ><span class="LeftMenuItemPrefix">»</span> System Log</a></td></tr><tr><td><a class="MenuItem" href="/App/MC/Sch/LinePlanRules.aspx" title="" ><span class="LeftMenuItemPrefix">»</span> Line Packing Rules</a></td></tr><tr><td><a class="MenuItem" href="/App/MC/Sch/SectorTypeRules.aspx" title="" ><span class="LeftMenuItemPrefix">»</span> Sector Type Rules</a></td></tr><tr><td><span class="MenuHead">Locations</span></td></tr><tr><td><a class="MenuItem" href="/App/MC/Loc/Airport.aspx" title="" ><span class="LeftMenuItemPrefix">»</span> Airports</a></td></tr><tr><td><a class="MenuItem" href="/App/MC/Loc/AirportGroup.aspx" title="" ><span class="LeftMenuItemPrefix">»</span> Airport Sets</a></td></tr><tr><td><a class="MenuItem" href="/App/MC/Loc/Country.aspx" title="" ><span class="LeftMenuItemPrefix">»</span> Countries</a></td></tr><tr><td><a class="MenuItem" href="/App/MC/Loc/CountrySet.aspx" title="" ><span class="LeftMenuItemPrefix">»</span> Country Sets</a></td></tr><tr><td><a class="MenuItem" href="/App/MC/Loc/Warehouse.aspx" title="" ><span class="LeftMenuItemPrefix">»</span> Warehouses</a></td></tr><tr><td><a class="MenuItem" href="/App/MC/Loc/Catering.aspx" title="" ><span class="LeftMenuItemPrefix">»</span> Catering</a></td></tr><tr><td><span class="MenuHead">Inventory</span></td></tr><tr><td><a class="MenuItem" href="/App/MC/Inv/Devices.aspx" title="" ><span class="LeftMenuItemPrefix">»</span> Devices</a></td></tr><tr><td><a class="MenuItem" href="/App/MC/Inv/PhysicalContainerCode.aspx" title="" ><span class="LeftMenuItemPrefix">»</span> Containers</a></td></tr><tr><td><a class="MenuItem" href="/App/MC/Inv/BarsetNumber.aspx" title="" ><span class="LeftMenuItemPrefix">»</span> Barsets</a></td></tr><tr><td><a class="MenuItem" href="/App/MC/Inv/Translations.aspx" title="" ><span class="LeftMenuItemPrefix">»</span> Translations</a></td></tr><tr><td><span class="MenuHead">Config</span></td></tr><tr><td><a class="MenuItem" href="/App/MC/Conf/Config.aspx" title="" ><span class="LeftMenuItemPrefix">»</span> Sys Configs</a></td></tr><tr><td><a class="MenuItem" href="/App/MC/Conf/Setting.aspx" title="" ><span class="LeftMenuItemPrefix">»</span> Settings</a></td></tr><tr><td><a class="MenuItem" href="/App/MC/Conf/EDISettings.aspx" title="" ><span class="LeftMenuItemPrefix">»</span> EDI Settings</a></td></tr><tr><td><a class="MenuItem" href="/App/MC/Conf/EDIConfig.aspx" title="" ><span class="LeftMenuItemPrefix">»</span> EDI Config</a></td></tr><tr><td><a class="MenuItem" href="/App/MC/Conf/EmailAccount.aspx" title="" ><span class="LeftMenuItemPrefix">»</span> Email Accounts</a></td></tr><tr><td><span class="MenuHead">Fleet</span></td></tr><tr><td><a class="MenuItem" href="/App/MC/Flt/Aircraft.aspx" title="" ><span class="LeftMenuItemPrefix">»</span> Aircrafts</a></td></tr><tr><td><a class="MenuItem" href="/App/MC/Flt/AircraftType.aspx" title="" ><span class="LeftMenuItemPrefix">»</span> Aircraft Types</a></td></tr><tr><td><span class="MenuHead">Galley</span></td></tr><tr><td><a class="MenuItem" href="/App/MC/Gal/GalleyConfiguration.aspx" title="" ><span class="LeftMenuItemPrefix">»</span> Galley Configs</a></td></tr><tr><td><a class="MenuItem" href="/App/MC/Gal/BarAllocationMatrix.aspx" title="" ><span class="LeftMenuItemPrefix">»</span> Bar Alloc. Rules</a></td></tr><tr><td><span class="MenuHead">Email</span></td></tr><tr><td><a class="MenuItem" href="/App/MC/Eml/Email.aspx" title="" ><span class="LeftMenuItemPrefix">»</span> Emails</a></td></tr><tr><td><a class="MenuItem" href="/App/Sys/EmailsMissing.aspx" title="" ><span class="LeftMenuItemPrefix">»</span> Missing Emails</a></td></tr><tr><td><span class="MenuHead">Financial</span></td></tr><tr><td><a class="MenuItem" href="/App/MC/Fin/VatRate.aspx" title="" ><span class="LeftMenuItemPrefix">»</span> VAT Rates</a></td></tr><tr><td><a class="MenuItem" href="/App/MC/Fin/VatRateCountry.aspx" title="" ><span class="LeftMenuItemPrefix">»</span> VAT Rates per Country</a></td></tr><tr><td><a class="MenuItem" href="/App/MC/Fin/Currency.aspx" title="" ><span class="LeftMenuItemPrefix">»</span> Currencies</a></td></tr><tr><td><a class="MenuItem" href="/App/MC/Fin/CurrencyDenominationList.aspx" title="" ><span class="LeftMenuItemPrefix">»</span> Currency Denomination</a></td></tr><tr><td><a class="MenuItem" href="/App/MC/fin/ExchangeRate.aspx" title="" ><span class="LeftMenuItemPrefix">»</span> Exchange Rates</a></td></tr><tr><td><a class="MenuItem" href="/App/MC/Mat/PriceList.aspx" title="" ><span class="LeftMenuItemPrefix">»</span> Price List</a></td></tr><tr><td><a class="MenuItem" href="/App/MC/Mat/PriceTypeList.aspx" title="" ><span class="LeftMenuItemPrefix">»</span> Price Type List</a></td></tr><tr><td><span class="MenuHead">Work</span></td></tr><tr><td><a class="MenuItem" href="/App/MC/Work/Job.aspx" title="" ><span class="LeftMenuItemPrefix">»</span> Jobs</a></td></tr><tr><td><span class="MenuHead">Miscellaneous</span></td></tr><tr><td><a class="MenuItem" href="/App/MC/Misc/SealColors.aspx" title="" ><span class="LeftMenuItemPrefix">»</span> Seal Colors </a></td></tr></table><br />


            </div>
        </td>
    <tr>
</table>

1 个答案:

答案 0 :(得分:0)

通过在这两个函数上设置断点来验证。 检查是否触发了函数{MC_Page_links.lnk_Brands()。click();}后,该按钮在Web浏览器中可见,必须由MC_Brands_Page.lnk_CreateNewBrand()触发。单击(); 。如果按钮不可见,您将获得nosuchmethodexception。在那种情况下

包括延迟Thread.sleep(3000(或根据您的互联网/页面速度));之间  MC_Page_links.lnk_Brands()。click()和MC_Brands_Page.lnk_CreateNewBrand()。点击()并检查它是否有效。