P
P
Python Newbie2021-09-06 08:55:07
Python
Python Newbie, 2021-09-06 08:55:07

Selenium doesn't select from a list by variable. What to do?

The website has the following code:

<div class="form-group">
    <label for="id_field_7089">1. Какова Ваша температура?</label> 
    <span data-required-id="id_field_7089">*</span> 
    
        <select name="field_7089" class="form-control" required="" id="id_field_7089">
  <option value="" selected="">---------</option>

  <option value="404">Нормальная</option>

  <option value="405">Выше нормы</option>

</select>
    
</div>


I wrote the code:
q = driver.find_elements_by_class_name('form-control')
q_click = q[0]
time.sleep(1)
select = Select(q_click).select_by_visible_text(answers[0])
q_click = q[1]
time.sleep(1)
print(answers[0])


Imports:
from selenium import webdriver
from selenium.webdriver.support.select import Select


When I run the code I get an error that: Could not locate element with visible text: Normal
It can't locate element by visible text even though the text is correct.

But if the code is like this:
q = driver.find_elements_by_class_name('form-control')
q_click = q[0]
time.sleep(1)
select = Select(q_click).select_by_visible_text('Нормальная')
q_click = q[1]
time.sleep(1)
print(answers[0])

That all works.

Why is this happening?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Drill, 2021-09-06
@Levman5

I can only assume that answers[0] contains a string with a slightly different set of characters. Try like this:

select = Select(q_click).select_by_visible_text(answers[0].strip())

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question