Input with equal class Lenium

Asked

Viewed 475 times

0

I am trying to fill out an online form automatically and the fields are in the same class, using Selenium and python I can fill in the first field with the campo01 = CHROMEDRIVER.find_element_by_class_name("jss14") but as I do with others who have the same class jss14 ?

What separates one from the other is only the DIV they are in, the first input is in DIV jss70 and the second input in DIV jss80

HTML

"Primeiro Input"

<input aria-invalid="false" type="text" class="MuiInputBase-input MuiOutlinedInput-input jss14 MuiInputBase-inputMarginDense MuiOutlinedInput-inputMarginDense" value="">

"Segundo Input"

<input aria-invalid="false" type="text" class="MuiInputBase-input MuiOutlinedInput-input jss14 MuiInputBase-inputMarginDense MuiOutlinedInput-inputMarginDense" value="">

My Code

campo01 = CHROMEDRIVER.find_element_by_class_name("jss14")
campo01.send_keys(n_li)
campo01.send_keys(Keys.RETURN)

I have tried with each xpath but I get the error message : Selenium.common.exceptions.Nosuchelementexception: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="root"]/div/div/div/div/div[1]/div[2]/div[2]/div/div/input"}

Xpath I am using in the first input: //*[@id="root"]/div/div/div/div/div/div[1]/div[2]/div/div/input

Xpath I am using in the second input: //*[@id="root"]/div/div/div/div/div/div[1]/div[2]/div[3]/div/div/input

1 answer

0

The method find_element_by_class_name returns only the first element of the page that has the given class. In your case it seems more appropriate to use find_elements_by_class_name(plural), because it returns a list of all the elements belonging to the given class, in the order in which it appears in the page’s source code. It would soon become possible to access all form inputs.

Below is an example:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

driver = webdriver.Chrome()
driver.get('urldapágina')
form_inputs = driver.find_elements_by_class_name('sjj14')

inputs_data = ('dados_campo_01', 'dados_campo_02', 'dados_campo_03')
for inp, data in zip(form_inputs, inputs_data):
    inp.send_keys(data)
    inp.send_keys(Keys.RETURN)


driver.close()

Browser other questions tagged

You are not signed in. Login or sign up in order to post.