Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
Clear All
new posts

    Try to Get column Element name of particular row From List grid

    I wrote a script for finding a name present in a particular column of List grid and it works fine accept sometimes its gives Error and my script fails.

    e.g :- Store name ( 17' LCD Monitor & Dr. Grip Pens - Black) from the column Item into List Array and then trying to compare this value to the given text values. Click image for larger version

Name:	Column name.PNG
Views:	555
Size:	4.5 KB
ID:	236697


    The Code and error is given below:-

    Code:-

    public void getNumbersOfRows() {

    List<String> lstr = new ArrayList<String>();

    String text = "No are available with your current Settings.";
    String text1 = "Text is loading your data...";

    By test = ByScLocator.scLocator("//ATListGrid[ID="fundAssetsGrid"]/body/");

    driver.waitForElementClickable(test);
    driver.waitForGridDone(test);

    WebElement fundAssetViewGridTable = driver
    .findElement(ByScLocator.scLocator("//ATListGrid[ID="fundAssetsGrid"]/body/"));

    List<WebElement> fundAssetViewGridTableAllRows = fundAssetViewGridTable.findElements(By.tagName("tr"));

    for (WebElement row : fundAssetViewGridTableAllRows) {
    if (row.getText().matches(text) || row.getText().matches(text1)) {
    break;
    } else if(row.getText().isEmpty()){
    break;
    }else{
    List<WebElement> cells = row.findElements(By.tagName("td"));

    for (WebElement cell : cells) {
    lstr.add(cell.getText());
    break;
    }
    }
    }

    for (WebElement row1 : fundAssetViewGridTableAllRows) {
    try {
    if(lstr.size() == 0){
    if (row1.getText().matches(text) || row1.getText().matches(text1)) {
    logger.info("Assets are Not Viewable in Asset Grid");
    break;
    }
    }
    } catch (NoSuchElementException e) {
    logger.error("Assets are Viewable in Asset Grid");
    }
    driver.waitForElementClickable(test);
    try {
    if(lstr.size()!=0){
    if (!(row1.getText().matches(text) || row1.getText().matches(text1))) {
    logger.info("Assets are Viewable in Asset Grid");

    logger.info("No of Assets present for this User is : " + (lstr.size()));
    break;
    }

    }
    } catch (NoSuchElementException e) {
    logger.error("Assets are Not Viewable in Asset Grid");
    }
    }
    lstr.clear();
    }



    Error:-

    Caused by: org.openqa.selenium.StaleElementReferenceException: Element not found in the cache - perhaps the page has changed since it was looked up
    For documentation on this error, please visit: http://seleniumhq.org/exceptions/sta...reference.html
    Build info: version: '2.53.0', revision: '35ae25b1534ae328c771e0856c93e187490ca824', time: '2016-03-15 10:43:46'
    System info: host: 'pranav-laptop', ip: '1.8.0.12', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_66'
    Driver info: driver.version: unknown
    at <anonymous class>.fxdriver.cache.getElementAt(resource://fxdriver/modules/web-element-cache.js:9454)
    at <anonymous class>.Utils.getElementAt(file:///C:/Users/pkothari/AppData/Local/Temp/anonymous1159940559189834308webdriver-profile/extensions/fxdriver@googlecode.com/components/command-processor.js:9039)
    at <anonymous class>.WebElement.getElementText(file:///C:/Users/pkothari/AppData/Local/Temp/anonymous1159940559189834308webdriver-profile/extensions/fxdriver@googlecode.com/components/command-processor.js:12092)
    at <anonymous class>.DelayedCommand.prototype.executeInternal_/h(file:///C:/Users/pkothari/AppData/Local/Temp/anonymous1159940559189834308webdriver-profile/extensions/fxdriver@googlecode.com/components/command-processor.js:12661)
    at <anonymous class>.fxdriver.Timer.prototype.setTimeout/<.notify(file:///C:/Users/pkothari/AppData/Local/Temp/anonymous1159940559189834308webdriver-profile/extensions/fxdriver@googlecode.com/components/command-processor.js:625)



    Actually i am getting error at this position always (row.getText().matches(text) || row.getText().matches(text1)).

    I am not refreshing page or its not done automatic then why i am getting error? Please help.
    Waiting for your reply..





    Last edited by pkothari; 8 Apr 2016, 08:14.

    #2
    As we've covered with you previously, direct access to DOM elements is not supported, as the DOM structure is not documented and subject to change without notice (we frequently have to adjust it to work around browser bugs).

    Aside from this test using an unsupported approach, it looks like it also assuming DOM elements in a grid body will stay the same while user actions are taken, but in fact new DOM elements are created for many different actions (eg sorts) and the grid will also redraw elements when new data is loaded, or based on programmatic actions that trigger redraws.

    Comment


      #3
      Can you please guide me about the solution for my problem as i tried many thing to work this but not able to and now i am stuck due to this. So please show me path to work my script.

      Comment


        #4
        We don't see a purpose to your script so it's hard to recommend an approach here.

        As we have informed you multiple times previously, the purpose of using Selenium / WebDriver is to simulate user action. Here, you seem to be trying to read an entire dataset and compare to known values; if this test actually makes sense to write at all, it should probably be done with just JavaScript, not WebDriver.

        Alternatively, Selenium / WebDriver tests sometimes spot-check values in data - but this can be done by just using an scLocator for a cell and checking it's value, so it wouldn't involve any of the unsupported approaches you're taking here.

        Comment


          #5
          Ok i got that but can you please guide me how to find particular element name of column of row via Sclocator.
          Because we are able to find ScLocator for whole row and as i want to element present in column from that particular row.
          like i need 17" LCD Monitor and Dr. Grip Pens - Black names from both rows present. So with ScLocator how can i find particular name?

          Comment


            #6
            For basic usage information like this, you can just read the Automated Testing and Selenium overviews. There are already examples of how to get locators for cells, and an explanation of the format for grid locators.

            Comment


              #7
              Hey, I am able to find column element with the way it is mention in the Automated Testing document. I missed this part in Document so not able to figure out the solution. But your guidance helps me. Thanks alot

              Comment


                #8
                Hi,

                I am able to get value of column with the way you suggested but i want to get values from each row and i am getting error and i supposed its of Firefox, So can you please check and guide me whats the actual problem and whats the solution for that?
                Below is the code and output Error is given.


                public void getNumbersOfRows() {

                //Vector<String> str = new Vector<String>();

                List<String> lstr = new ArrayList<String>();

                String text = "No Assets are available with your current Settings."

                String text1 = "Text is loading your data...";

                By test = ByScLocator.scLocator("//ATListGrid[ID="fundAssetsGrid"]/body/");
                driver.waitForElementClickable(test);
                driver.click(test);

                WebElement fundAssetViewGridTable = driver.findElement(ByScLocator.scLocator("//ATListGrid[ID="fundAssetsGrid"]/body/"));

                List<WebElement> fundAssetViewGridTableAllRows = fundAssetViewGridTable.findElements(By.tagName("tr"));

                for (int i = 0; i < fundAssetViewGridTableAllRows.size() - 1; i++) {
                try {
                By Asset1 = ByScLocator .scLocator("scLocator=//ATListGrid[ID="fundAssetsGrid"]/body/row["+ i + "]/col[fieldName=assetNameShort||0]");
                driver.waitForElementClickable(Asset1);

                //String abc = driver.getValue(Asset1).toString();
                String abc = driver.getText(Asset1).toString();

                lstr.add(abc);

                } catch (NoSuchElementException e) {
                By noAsset = ByScLocator
                .scLocator("scLocator=//ATListGrid[ID="fundAssetsGrid"]/body/");
                String abc = driver.getText(noAsset).toString();

                lstr.add(abc);
                }
                }
                //System.out.println(lstr);
                if (lstr.contains(text) || lstr.contains(text1)) {
                lstr.clear();
                }

                for (WebElement row1 : fundAssetViewGridTableAllRows) {
                try {
                if (lstr.size() == 0) {
                logger.info("Assets are Not Viewable in Asset Grid");
                break;
                }
                } catch (NoSuchElementException e) {
                logger.error("Assets are Viewable in Asset Grid");
                }
                try {
                if (lstr.size() != 0) {

                logger.info("Assets are Viewable in Asset Grid");

                logger.info("No of Assets present for this User is : "
                + (lstr.size()));
                break;
                }

                } catch (NoSuchElementException e) {
                logger.error("Assets are Not Viewable in Asset Grid");
                }
                }
                lstr.clear();



                ERROR:-

                org.openqa.selenium.WebDriverException: arguments[0] is undefined
                Command duration or timeout: 151 milliseconds
                Build info: version: '2.53.0', revision: '35ae25b1534ae328c771e0856c93e187490ca824', time: '2016-03-15 10:43:46'
                System info: host: 'pranav-laptop', ip: '10.8.0.102', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_66'
                Driver info: org.openqa.selenium.firefox.FirefoxDriver
                Capabilities [{applicationCacheEnabled=true, rotatable=false, handlesAlerts=true, databaseEnabled=true, version=45.0.1, platform=WINDOWS, nativeEvents=false, acceptSslCerts=true, webStorageEnabled=true, locationContextEnabled=true, browserName=firefox, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true}]
                Session ID: a5056ce4-06ae-47f1-b4fc-1a6fee519d7d







                Comment


                  #9
                  We keep reminding you of this, but your code continues to rely on undocumented details of the DOM which are subject to change. Right now you are assuming that the number of <tr>s inside the body element is the number of rows - this assumption is pretty much always wrong except in certain narrow circumstances, and may well be the reason your code is crashing. We can't tell, because we don't have enough information to run the code.

                  Please also remember to use CODE tags around code you post, otherwise, the code loses indentation and becomes unreadable.

                  Comment


                    #10
                    As per your suggestion i changed my code and instead of taking values from row wise i took values from specific element by sclocator and still sometimes i am getting error while getting text from that particular element element. especially at this step "String abc = webtest.getText().toString();".
                    So i am sending you the updated code and error, please guide me

                    Code:
                    public List<String> getNameOfRowstest() {
                    List<String> lstr = new ArrayList<String>();
                    
                    By test = ByScLocator.scLocator("//ListGrid[ID="fundGrid"]/body/");
                    driver.waitForElementClickable(test);
                    driver.click(test);
                    driver.waitForGridDone(test);
                    
                    try {
                    By test0 = ByScLocator.scLocator(
                    "scLocator=//ListGrid[ID="fundGrid"]/body/row[fundID=25988||Name=CORPORATION||NameShort=CORPORATION||0]/col[fieldName=NameShort||0]");
                    WebElement webtest = driver.findElement(test0);
                    
                    if (webtest.isDisplayed()) {
                    
                    driver.waitForElementClickable(test);
                    String abc = webtest.getText().toString();;
                    lstr.add(abc);
                    }
                    } catch (NoSuchElementException e) {
                    present1 = false;
                    }
                    
                    try {
                    By test1 = ByScLocator.scLocator(
                    "scLocator=//ListGrid[ID="fundGrid"]/body/row[fundID=46161||Name=FACEBOOK%20CL%20A||NameShort=FACEBOOK%20CL||1]/col[fieldName=NameShort||0]");
                    WebElement webtest1 = driver.findElement(test1);
                    
                    if (webtest1.isDisplayed()) {
                    driver.waitForElementClickable(test1);
                    String abc = webtest1.getText().toString();;
                    lstr.add(abc);
                    }
                    } catch (NoSuchElementException e) {
                    present1 = false;
                    }
                    
                    try {
                    By test2 = ByScLocator.scLocator(
                    "scLocator=//ListGrid[ID="fundGrid"]/body/row[fundAssetID=15910||Name=GOOGLE||NameShort=GOOGLE||2]/col[fieldName=NameShort||0]");
                    WebElement webtest2 = driver.findElement(test2);
                    
                    if (webtest2.isDisplayed()) {
                    driver.waitForElementClickable(test2);
                    String abc = webtest2.getText().toString();;
                    lstr.add(abc);
                    }
                    } catch (NoSuchElementException e) {
                    present1 = false;
                    }
                    
                    
                    
                    
                    try {
                    if (lstr.size() == 0) {
                    logger.info("values are Not Viewable in Grid");
                    }
                    } catch (NoSuchElementException e) {
                    logger.error("values are Not Viewable in Grid");
                    }
                    try {
                    if (lstr.size() != 0) {
                    
                    logger.info("values are Not Viewable in Grid");
                    
                    logger.info("No of values present for this User is : " + (lstr.size()));
                    }
                    
                    } catch (NoSuchElementException e) {
                    logger.error("values are Not Viewable in Grid");
                    }
                    return lstr;
                    }
                    Error:-

                    org.openqa.selenium.StaleElementReferenceException: Element not found in the cache - perhaps the page has changed since it was looked up
                    Command duration or timeout: 6.14 seconds
                    For documentation on this error, please visit: http://seleniumhq.org/exceptions/sta...reference.html
                    Build info: version: '2.53.0', revision: '35ae25b1534ae328c771e0856c93e187490ca824', time: '2016-03-15 10:43:46'
                    System info: host: 'pranav-laptop', ip: '10.7.0.22', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_66'
                    Driver info: org.openqa.selenium.firefox.FirefoxDriver
                    Capabilities [{applicationCacheEnabled=true, rotatable=false, handlesAlerts=true, databaseEnabled=true, version=45.0.1, platform=WINDOWS, nativeEvents=false, acceptSslCerts=true, webStorageEnabled=true, locationContextEnabled=true, browserName=firefox, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true}]
                    Session ID: b8afb31c-77fd-4de5-8459-c62f0cb3b104





                    Last edited by Isomorphic; 25 Apr 2016, 13:44.

                    Comment


                      #11
                      You should use the APIs that we provide off of SmartClientWebDriver, where available, in preference to the native WebDriver APIs, as our methods often contain fixes for various issues we've observed. For example, consider using SmartClientWebDriver.verifyText() and SmartClientWebDriver.verifyValue(), both of which take our special locators. You also need to be more specific about which line of code is triggering the exception that you reported, as it's not clear from what you posted. (Some ways of accomplishing that would be to add extra code to log various diagnostic messages, step through the code in Eclipse, or simply remove code from the end until you no longer see the exception.)

                      Finally note that we test against the version of the Selenium libraries shipped with the SDK, which appears to be older than what you're using. If you haven't yet tried using the shipped version of Selenium, you might do that to see whether the exception still shows up. You may also want to check whether using Firefox 38 ESR avoids the problem.


                      Comment


                        #12
                        => I understand that but that's not a problem i am facing. I am trying to get a specific column value by using this webdriver API

                        String abc = webtest.getText().toString(); // WebDriver API

                        => And as you suggested , I also used SmartclientWebDriver API for doing same task


                        By test1 = ByScLocator.scLocator(
                        "scLocator=//ListGrid[ID="fundGrid"]/body/row[fundID=46161||Name=FACEBOOK%20CL%20A||NameShort=FACEBOOK%20CL||1]/col[fieldName=NameShort||0]");

                        String abc = driver.getText(test1).tostring(); // SmartClientWebDriver API


                        => But still i am getting this error

                        ERROR:-

                        org.openqa.selenium.StaleElementReferenceException: Element not found in the cache - perhaps the page has changed since it was looked up
                        Command duration or timeout: 5.13 seconds
                        For documentation on this error, please visit: http://seleniumhq.org/exceptions/sta...reference.html
                        Build info: version: '2.53.0', revision: '35ae25b1534ae328c771e0856c93e187490ca824', time: '2016-03-15 10:43:46'
                        System info: host: 'pranav-laptop', ip: '10.7.0.22', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_66'
                        Driver info: org.openqa.selenium.firefox.FirefoxDriver
                        Capabilities [{applicationCacheEnabled=true, rotatable=false, handlesAlerts=true, databaseEnabled=true, version=45.0.1, platform=WINDOWS, nativeEvents=false, acceptSslCerts=true, webStorageEnabled=true, locationContextEnabled=true, browserName=firefox, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true}]
                        Session ID: 92e1a0df-114c-4b64-b582-84333fd4e64c

                        I am getting error at this specific step always, So please give me solution for this


                        Comment


                          #13
                          Again, we pointed out SmartClientWebDriver.verifyText() as an alternative API to use here.

                          Comment

                          Working...
                          X