Karry
Karry

Reputation: 33

I am trying Selenium Webdriver to find input element by class name

How to find element input class="Test_type"? I successfully got by id=Test1 but failing to find element by input class="Test_type"

I have tried:

WebElement mainform = driver.findElement(By.className("mainform"));

List<WebElement> postadchildone2 = mainform.findElements(By.className("formrow"))

for(WebElement formrow: postadchildone2)
{
    WebElement formfield = formrow.findElement(By.className("formfield"));
    WebElement inputclass = formfield.findElement(By.className("Test_type"))

}

Also tried using CSS selector & XPath, however still it says didn't found, because of the same class name it is not finding input class.

Under for loop I tried doing

formrow.getText()

It displays all the text results but it is not taking class name? I am stuck please help

HTML:

<div class="mainform">
  <div class="formrow">
    <div class="formrow">
      <div class="formrow">
        <div class="formfield">
          <div id="Test1" class="city_select">
            <div class="blank1"/>
            <div class="formrow " style="margin-left:10px;">
              <div class="formrow">
                <div class="formlabel">
                   <strong>You Are</strong>
                </div>
                <div class="formfield">
                  <label>
                    <input class="Test_type required" type="radio"/>
                    YouareTest
                  </label>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>

Upvotes: 2

Views: 36784

Answers (2)

eugene.polschikov
eugene.polschikov

Reputation: 7339

as you have the following piece of code:

<div class="formfield">
    <label>
    <input class="Test_type required" type="radio"/>
      YouareTest
    </label>
</div>

We need to outline input element somehow. So I recommend to locate this element using CSS selector because it is the fastest way. So 'input' tag has following class attribute= "Test_type required" , that means that to this element 2 CSS classes are applied:

  • Test_type
  • required When locating with CSS selector element, an element B with class attribute y will be notated as B.y, an element with 2 class attributes y and z will be notated as B.y.z then. So in terms of our task it will look like

      input.Test_type.required
    

But also you can notice that you have another element - div with class attribute =formfield as parent element. So to locate child element to parent with CSS in the structure like:

 <parent class="zz">
     <child 1>
        <child2> abaracadabra</child>
     </child 1>
    </parent>

parent.zz child2 => in this way

So the second part of our selector be like : div.formfield as it be parent selector in relation to input. So solution combo =

WebElement inputt=driver.findElement(By.cssSelector('div.formfield input.Test_type.required'));

Please try this one, and tell whether it works for you or not.

Upvotes: 6

Sham
Sham

Reputation: 840

Since you are trying to fetch with only partial class name, try using the below code

WebElement inputclass = formfield.findElement(By.CssSelector("Input[class*='Test_type'"))

Upvotes: 1

Related Questions