Home > Software design >  Selenium Python - Select input-element berore span-element
Selenium Python - Select input-element berore span-element

Time:12-01

Web page have element with structure like:

<label>
    <input type="checkbox" name="storage_locations" value="3" style="vertical-align: middle;">
    <span style="padding-left: 6px;">example.domain.com: [Text] Text_1 (Sometext)</span>
</label>
<label>
    <input type="checkbox" name="storage_locations" value="3" style="vertical-align: middle;">
    <span style="padding-left: 6px;">example.domain.com: [Text] Text_2 (Sometext)</span>
</label>

<label>
    <input type="checkbox" name="storage_locations" value="3" style="vertical-align: middle;">
    <span style="padding-left: 6px;">example.domain.com: [Text] Text_3 (Sometext)</span>
</label>

How i can select input-element located in label with span-element which contains 'Text 2' (for example)?

I know that i could find span-element using this code:

example = driver.find_element(By.XPATH,'//*/label/span[contains(text(),"Text_2")') 

But i'm new in selenium-python and haven't ideas what to do next

CodePudding user response:

There are always multiple ways to do things.

You can try with css selector:

driver.find_element(By.CSS_SELECTOR, "body > label:nth-child(2) > input[type=checkbox]")

Or with name:

driver.find_element(By.NAME, "storage_locations")

You can always do it with xpath too. Hope this helps.

CodePudding user response:

I solved my question with help of two array.

text_list = 'text1,text2,text3,ect'

text_array = text_list.split(',')
input_checkbox_array = driver.find_elements(By.XPATH,'//*[xpath_to_find_checboxes')
span_text_array = driver.find_elements(By.XPATH,'//*[xpath_to_find_inputs')

for datastore in datastores_array:
    i = 0
    while datastore not in span_text_array[i].text:
        i  = 1
    actions.move_to_element(input_checkbox_array[i])
    actions.click(input_checkbox_array[i])
    actions.perform()
  • Related