c# - Select an option from a dependent drop box Selenium web driver -


My application should be used in IE. I'm doing automated testing in which the first drop-box, the script is already selected to select an option in "Category", which is displayed in the second drop-box in the category, "Name". The script then selects an option in "name", and an associated page will appear.
"Name" has no choice until the selection in "Category". HTML Source:

  & lt; Select ID = "drop_category" & gt; & Lt; Option value = "" /> & Lt; Option value = "category 1" & gt; Lesson - Category 1 & lt; Option value = "Class 2" & gt; Lesson - Category 2 & lt; Id = "drop_name" & gt;   

After selecting the option "Category 1", the HTML source changes to the following:

  & lt; Id = "drop_category" & gt; & Lt; Option value = "" /> & Lt; Option value = "category 1" & gt; Lesson - Category 1 & lt; Option value = "Class 2" & gt; Lesson - Category 2 & lt; Id = "drop_name" & gt; & Lt; Option value = "" /> & Lt; Option value = "C1_Name1" & gt; Lesson - C1_Name1 & lt; Option value = "C1_Name2" & gt; Lesson - C1_Name2 & lt; Option value = "C1_Name3" & gt; To select "Category1" and "C1_Name3" for the script, my first version of the code is:  
  // Select the drop-in option -   

-box "category" stringText = "Category1"; Var dropCategory = new selection element (driver.edidEid ("id" ("drop_category")); DropCategory.SelectByText (stringText); // Choose the option in the drop box "name" stringText = "C1_Name3"; Var dropName = New selection element (driver id ("drop_Name")); DropName.SelectByText (stringText);

This code was not working because "name" is not listed and the script can not get the option with the text "C1_Name3", so I did not help endless wait wait , So I tried to catch the exception. This is the second version of the code:

  // Select the option in the drop-box "Category" stringText = "Category1"; Var dropCategory = new selection element (driver.edidEid ("id" ("drop_category")); DropCategory.SelectByText (stringText); // in the drop box, select "name" stringText = "C1_Name3" {var dropname = new selection element (try the driver.find element (by id ("drop_name"); dropName.SelectByText (stringText);} cache ( Knowledge Exception) {var dropName = new selection element (driver, fidelity (by id ("drop_name")); dropName.SelectByText (stringText);}   

This worked but invalid SelectorException Or sometimes it crashes because of StaleElementReferenceException exceptions. I do not know if this What to do to get work continuously Also, I'm new in the area, so I'm not sure that it is bad practice to write code as my 2nd version. Any help is greatly appreciated. < / div>

initial approach

Usually (in my experience) wait period There is also a need for sleep; So try to combine two.

For example, I want to By the time I found the element, I will try to loop. Pseudo Code is as follows:

  Boolean found = false; While (! Found) {try {var dropName = new selectElement (driver.FindElement (By.Id ("drop_Name"))); DropName.SelectByText (stringText); Found = true; } Catch (Knowledge Exception) {var dropnam = new selection element (driver, fidelity (by IID ("drop_name")); DropName.SelectByText (stringText); // Here comes a little sleep eg. Depending on your site's speed of 500 ms}}   

, that exception is also thrown when "the selector to find an element does not return a web element." Therefore, catching NoSuchElementException should suffice.

Alternative Approach

  • With your question & lt; Select id = "drop_Name" & gt; is present in the code from the beginning.
  • Therefore, a web aliment will search by driver.findElement always id drop_Name
  • the difference This is as long as you class 1 , drop_Name of any & lt; option & gt; Value .
  • So you can try the following wait function:

    (Note: the code is in Java; it can be easily ported to C #) < / P>

      Wait for Private Static Z Requirements () {While (true) {Thread sleep (1000); & Lt; WebElement & gt; Options = driver.findElement (By.id ("drop_Name")) .findElements (By.tagName ("option")); If (options.size ()> 0) {System.out.println ("Multiple option tags found, therefore options are loaded"); break; }}    

Comments

Popular posts from this blog

Verilog Error: output or inout port "Q" must be connected to a structural net expression -

jasper reports - How to center align barcode using jasperreports and barcode4j -

c# - ASP.NET MVC - Attaching an entity of type 'MODELNAME' failed because another entity of the same type already has the same primary key value -