Skip to main content
added 224 characters in body
Source Link
Dhiman
  • 4.6k
  • 8
  • 27
  • 54

I am creating a test for presenting a demo to my management and client using Selenium WebDriver in C#. In this test I need to find an element (a radio button), but for that I want to use the nested find element statement. Although, I can find the required element directly using ID or Name but, I want to do it in other way round (let me explain why).

The element which is I need to select can be selected by using following code and its working.

driver.FindElement(By.Id("exp-3")).Click();

But, my requirement is to pass both "the Find Logic" and its respective to be selected "Value" via some CSV or Excel file; so I need this (kind of):

driver.FindElement(By.TagName("Years of Experience")).FindElement(By.Id("exp-3")).Click();

and after using external source it will look like this:

driver.FindElement(By.TagName(From CSV File 1)).FindElement(By.Id(From CSV File 2)).Click();

i.e. find the div/control first and then find the required element and, using this I need to change only the CSV files without changing the Find logic and its value (as per me).

This, is the HTML of the page:

<div class="control-group">
<strong>
<label class="control-label" for="exp">Years of Experience</label>
</strong>
<input id="exp-0" type="radio" value="1" name="exp"/>
1    
<input id="exp-1" type="radio" value="2" name="exp"/>
2    
<input id="exp-2" type="radio" value="3" name="exp"/>
3    
<input id="exp-3" type="radio" value="4" name="exp"/>
4    
<input id="exp-4" type="radio" value="5" name="exp"/>
5    
<input id="exp-5" type="radio" value="6" name="exp"/>
6    
<input id="exp-6" type="radio" value="7" name="exp"/>
<label class="radio inline" for="exp-6">7 </label>
</div>

Let me know how I can do this and if there is a better/efficient way to achieve the same.

P.S. I don't want to use XPath as UI of the actual application is dynamic, so XPath of the elements will change during run time, while other parameters like ID and Name will not change (as discussed with Developers).

I am creating a test for presenting a demo to my management and client using Selenium WebDriver in C#. In this test I need to find an element (a radio button), but for that I want to use the nested find element statement. Although, I can find the required element directly using ID or Name but, I want to do it in other way round (let me explain why).

The element which is I need to select can be selected by using following code and its working.

driver.FindElement(By.Id("exp-3")).Click();

But, my requirement is to pass both "the Find Logic" and its respective to be selected "Value" via some CSV or Excel file; so I need this (kind of):

driver.FindElement(By.TagName("Years of Experience")).FindElement(By.Id("exp-3")).Click();

and after using external source it will look like this:

driver.FindElement(By.TagName(From CSV File 1)).FindElement(By.Id(From CSV File 2)).Click();

i.e. find the div/control first and then find the required element and, using this I need to change only the CSV files without changing the Find logic and its value (as per me).

This, is the HTML of the page:

<div class="control-group">
<strong>
<label class="control-label" for="exp">Years of Experience</label>
</strong>
<input id="exp-0" type="radio" value="1" name="exp"/>
1    
<input id="exp-1" type="radio" value="2" name="exp"/>
2    
<input id="exp-2" type="radio" value="3" name="exp"/>
3    
<input id="exp-3" type="radio" value="4" name="exp"/>
4    
<input id="exp-4" type="radio" value="5" name="exp"/>
5    
<input id="exp-5" type="radio" value="6" name="exp"/>
6    
<input id="exp-6" type="radio" value="7" name="exp"/>
<label class="radio inline" for="exp-6">7 </label>
</div>

Let me know how I can do this and if there is a better/efficient way to achieve the same.

I am creating a test for presenting a demo to my management and client using Selenium WebDriver in C#. In this test I need to find an element (a radio button), but for that I want to use the nested find element statement. Although, I can find the required element directly using ID or Name but, I want to do it in other way round (let me explain why).

The element which is I need to select can be selected by using following code and its working.

driver.FindElement(By.Id("exp-3")).Click();

But, my requirement is to pass both "the Find Logic" and its respective to be selected "Value" via some CSV or Excel file; so I need this (kind of):

driver.FindElement(By.TagName("Years of Experience")).FindElement(By.Id("exp-3")).Click();

and after using external source it will look like this:

driver.FindElement(By.TagName(From CSV File 1)).FindElement(By.Id(From CSV File 2)).Click();

i.e. find the div/control first and then find the required element and, using this I need to change only the CSV files without changing the Find logic and its value (as per me).

This, is the HTML of the page:

<div class="control-group">
<strong>
<label class="control-label" for="exp">Years of Experience</label>
</strong>
<input id="exp-0" type="radio" value="1" name="exp"/>
1    
<input id="exp-1" type="radio" value="2" name="exp"/>
2    
<input id="exp-2" type="radio" value="3" name="exp"/>
3    
<input id="exp-3" type="radio" value="4" name="exp"/>
4    
<input id="exp-4" type="radio" value="5" name="exp"/>
5    
<input id="exp-5" type="radio" value="6" name="exp"/>
6    
<input id="exp-6" type="radio" value="7" name="exp"/>
<label class="radio inline" for="exp-6">7 </label>
</div>

Let me know how I can do this and if there is a better/efficient way to achieve the same.

P.S. I don't want to use XPath as UI of the actual application is dynamic, so XPath of the elements will change during run time, while other parameters like ID and Name will not change (as discussed with Developers).

Source Link
Dhiman
  • 4.6k
  • 8
  • 27
  • 54

Problem with selecting an Element from a div using nested FindElement option

I am creating a test for presenting a demo to my management and client using Selenium WebDriver in C#. In this test I need to find an element (a radio button), but for that I want to use the nested find element statement. Although, I can find the required element directly using ID or Name but, I want to do it in other way round (let me explain why).

The element which is I need to select can be selected by using following code and its working.

driver.FindElement(By.Id("exp-3")).Click();

But, my requirement is to pass both "the Find Logic" and its respective to be selected "Value" via some CSV or Excel file; so I need this (kind of):

driver.FindElement(By.TagName("Years of Experience")).FindElement(By.Id("exp-3")).Click();

and after using external source it will look like this:

driver.FindElement(By.TagName(From CSV File 1)).FindElement(By.Id(From CSV File 2)).Click();

i.e. find the div/control first and then find the required element and, using this I need to change only the CSV files without changing the Find logic and its value (as per me).

This, is the HTML of the page:

<div class="control-group">
<strong>
<label class="control-label" for="exp">Years of Experience</label>
</strong>
<input id="exp-0" type="radio" value="1" name="exp"/>
1    
<input id="exp-1" type="radio" value="2" name="exp"/>
2    
<input id="exp-2" type="radio" value="3" name="exp"/>
3    
<input id="exp-3" type="radio" value="4" name="exp"/>
4    
<input id="exp-4" type="radio" value="5" name="exp"/>
5    
<input id="exp-5" type="radio" value="6" name="exp"/>
6    
<input id="exp-6" type="radio" value="7" name="exp"/>
<label class="radio inline" for="exp-6">7 </label>
</div>

Let me know how I can do this and if there is a better/efficient way to achieve the same.