Search for CSS selector using label text

Asked

Viewed 226 times

-1

Hello,

I’m looking to automate a form that has 4 fields in which the only difference is the label. I’ve searched a lot, but I couldn’t find an answer that would help me. I’m automating with Selenium and Python. The HTML snippet I need to search has this structure:

<div class="formGroup"><label>Estado Expedidor RG<span class="danger"> * </span></label>
  <select required="" class="ant-select ant-select-enabled ant-select-selection ant-select-selection--single  dispatch_state"></select>
</div>

I would like to search by State Shipper RG. The search I did was as follows:

combobox_element = driver.find_element_by_css_selector('.formGroup .dispatch_state')

However, I can’t find the other fields that are in the same form, because they have the same attributes and classes informed above, differentiating only in the label. How can I add in the search I informed earlier, the label I want to use as reference. I know an id or even an attribute name would help, but it doesn’t have.

Thank you so much for your help and I’m sorry if I failed to inform you of any important information.

1 answer

0

Well, I managed to get around (not the way I’d like) by inspecting the field I wanted to manipulate, right-clicking > Copy > Copy selector. and copied the path in the search parameter that I reported in doubt.

So it was like this:

    combobox_element = driver.find_element_by_css_selector(
    '#form-driver > div:nth-child(1) > div > div > div > div > div > div:nth-child(2) > div:nth-child(1) > div > select'
    )

Browser other questions tagged

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