tags:

views:

694

answers:

2

Hi all I am using this function to get name's of the input fields from browser. Problem is that in couple of my sites input fields have the same position, so i cant cycle thrue them correctly. Any ideas how to do this cycle in some different way as thrue position? Thank you.


public void hladame_fieldy ()
{
      //fieldy
      string nazov_fieldu;
      decimal celkovy_pocet_fieldov = selenium.GetXpathCount ("//input[@type='text']");
      string field = "@type='text'";
      int b = 1;
      for (b = 1;b<=celkovy_pocet_fieldov;b++)
      {
       nazov_fieldu = selenium.GetAttribute("xpath=//input[position()="+b+" and "+field+"]@name");
       Console.WriteLine(nazov_fieldu);
      }
      Console.WriteLine ("Celkovy pocet fieldov je = " + celkovy_pocet_fieldov);
     }
A: 

Since you have the amount of elements you can just go through them as an array

public void hladame_fieldy ()
{
                //fieldy
                string nazov_fieldu;
                decimal celkovy_pocet_fieldov = selenium.GetXpathCount ("//input[@type='text']");
                string field = "@type='text'";
                int b = 1;
                for (b = 1;b<=celkovy_pocet_fieldov;b++)
                {
                        nazov_fieldu = selenium.GetAttribute("xpath=//input[" + b + "]@name");
                        Console.WriteLine(nazov_fieldu);
                }
                Console.WriteLine ("Celkovy pocet fieldov je = " + celkovy_pocet_fieldov);
        }

That way you just go through all the input elements in the DOM from top to bottom.

AutomatedTester
thank you for your help, but this doesnot solve my problem because fields are on the same position... for example 3 cycle returns 2 fields and after that in 6 cycle from 9 cycles program finish with error that in 6 cycle is no field.
Peter_Tester
this: "xpath=//input[" + b + "]@name" get me two values in third cycle... i need different acces to fields :(
Peter_Tester
oooo sorry my fault, its working nice thank you very much :)
Peter_Tester
I believe the reason for that is the position() returns not the index in the rows set you choose with the rest of the XPath expression, but the index of this element among its siblings.
Grzegorz Oledzki
It won't return you its siblings. It will go from the top of the DOM down to the bottom and find the item that relates to it. e.g If you have 10 input elements , //input[4] will return the 5th element (index is zero based) from the top of the DOM. This is not necessarily the 5th Element from the top that is visible because it depends on how the page is created.
AutomatedTester
yep thats the problem, and for the solution with array: xpath //input[" + b + " and @type='text'] returns me always all 9 fields so then the funkction takes always first field and get me the name of only the first field :(
Peter_Tester
my final solution :) :string nazov_fieldu; decimal celkovy_pocet_fieldov = selenium.GetXpathCount ("//input[@type='text']"); int b = 1; string pomoc = ""; for (b = 1;b<=celkovy_pocet_fieldov;b++) { nazov_fieldu = selenium.GetAttribute("xpath=//input[@type='text'" + pomoc +"]@name"); pomoc = pomoc + " and @name!= '" + nazov_fieldu + "'"; Console.WriteLine(nazov_fieldu); }
Peter_Tester
A: 

final solution:

public void hladame_fieldy ()

 {
            //fieldy
            string nazov_fieldu;
            decimal celkovy_pocet_fieldov = selenium.GetXpathCount ("//input[@type='text']");
            int b = 1;
            string pomoc = "";
            for (b = 1;b<=celkovy_pocet_fieldov;b++)
            {
                    nazov_fieldu = selenium.GetAttribute("xpath=//input[@type='text'" + pomoc +"]@name");
                    pomoc = pomoc + " and @name!= '" + nazov_fieldu + "'";
                    Console.WriteLine(nazov_fieldu);
            }
            Console.WriteLine ("Celkovy pocet fieldov je = " + celkovy_pocet_fieldov);
 }
Peter_Tester