Is it possible to somehow select a specific element from the drop-down list on the page using the splinter module in Python

Is there any way to select a specific element from the drop-down list on the page via the splinter module in Python?

I have the following HTML code:

<select id="xyz">
   <optgroup label="Group1">
      <option value="1">pick1</option>
      <option value="2">pick2</option>
   </optgroup>
   <optgroup label="Group2">
       <option value="3">pick3</option>
       <option value="4">pick4</option>
   </optgroup>
</select>

Suppose I need to select the pick3 option. How can i do this?

+4
source share
4 answers

First find the item selectusing find_by_id()and use select()to select an option:

element = browser.find_by_id('xyz').first
element.select('3')

An alternative solution would be to use find_by_xpath()and click():

element = browser.find_by_xpath('//select[@id="xyz"]//option[@value="3"]').first
element.click()
+6
source

try

browser.find_option_by_text('pick3').first.click() 
+1
source

, , . select 'select (option_value)' xpath:// [@ name= "% s" ]/option [@value = "% s" ] ', . xpath , optgroups, .

element = browser.find_by_xpath ('//select [@id = "xyz" ]// [@value = "3" ]'). first element.click() , .

0

select_by_text() -

browser.find_by_id('xyz').select_by_text("pick3")
0

Source: https://habr.com/ru/post/1541831/


All Articles