Akash
Akash

Reputation: 101

How to access multiple text boxes in selenium

i am trying fill the text boxes. Each time you get into the page the the number of text boxes will be different and the name of them will be different.

<div class="fb-single-line-text">
    <div class="display-flex" data-test-single-line-text-input-wrap="true">
<!---->
      <input name="urn:li:fs_easyApplyFormElement:(urn:li:fs_normalized_jobPosting:2620509178,30257397,numeric)" id="urn:li:fs_easyApplyFormElement:(urn:li:fs_normalized_jobPosting:2620509178,30257397,numeric)" class="ember-text-field ember-view fb-single-line-text__input" aria-required="true" aria-describedby="urn:li:fs_easyApplyFormElement:(urn:li:fs_normalized_jobPosting:2620509178,30257397,numeric)-error-message" data-test-single-line-text-input="" type="text">
    </div>
</div>

the above lines are the html code of first text box

<div class="fb-single-line-text">
    <div class="display-flex" data-test-single-line-text-input-wrap="true">
<!---->
      <input name="urn:li:fs_easyApplyFormElement:(urn:li:fs_normalized_jobPosting:2620509178,30257389,numeric)" id="urn:li:fs_easyApplyFormElement:(urn:li:fs_normalized_jobPosting:2620509178,30257389,numeric)" class="ember-text-field ember-view fb-single-line-text__input" aria-required="true" aria-describedby="urn:li:fs_easyApplyFormElement:(urn:li:fs_norm
alized_jobPosting:2620509178,30257389,numeric)-error-message" data-test-single-line-text-input="" type="text">
    </div>
</div>

So i see all of them class name in comman so i tried to get all the eemphasized textlemnts inside the class . but i could not find a possible way for it. can someone help me with that

Upvotes: 1

Views: 381

Answers (1)

cruisepandey
cruisepandey

Reputation: 29362

There are multiple ways to deal with this situation.

  1. use find_elements to find out how many input boxes are present in UI, also you could send different message to those input box.

    total_no_of_input_with_div_single_line_text = len(driver.find_elements(By.CSS_SELECTOR, "div.fb-single-line-text input"))
    print(total_no_of_input_with_div_single_line_text)
    

Now you can send_keys to individual elements like this :-

total_no_of_input_with_div_single_line_text[0].send_keys('some string')

you can always change the index above from 0 to 1, 2 etc.

second in a loop like this :

for ele in driver.find_elements(By.CSS_SELECTOR, "div.fb-single-line-text input"):
  ele.send_keys('some value here')
  1. other way would be to use xpath indexing to locate the element.

    (//div[@class='fb-single-line-text']/descendant::input)[1]
    

should locate the first web element input in the page, also you have the access to change that to [2], [3] and so on..

Upvotes: 2

Related Questions