2

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).

1 Answer 1

1

You can findElements by name="exp", which will give you list of elements. Then you can iterate over this list, interrogating each element about other relevant properties (or comparing to separately read CSV file), and pick the right one.

2
  • Seems, it will solve my purpose, as tried part of your solution and it worked for me.
    – Dhiman
    Commented Oct 28, 2015 at 16:04
  • It worked for me. I prefer to deal with complexity in my code, not in my selectors :-) And I will do almost anything to avoid XPath. Commented Oct 28, 2015 at 16:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.