Python selenium and fuzzy matching
Question: I'm using `Selenium` to populate some drop down menus. These dropdown menus
are fairly dynamic.

What I have though are values that could be in the dropdown, for example:

    
    
    <select>
        <option>Red, wooly, jumper, large, UK</option>
        <option>Blue, wooly, jumper, small, USA</option> 
        <option>Red, wooly, scarf, small, UK</option>
    </select>
    

Ideally, what I'm looking to do is select the option that closest matches the
following string

    
    
    'Red, wooly, small, UK'
    

This would select the 3rd item from the dropdown

Could this be done with some kind of matcher? If so, how would I select the
correct element from the dropdown?

Thanks


Answer: have you tried using a regular expression?? Python regex to match the third
line, or even using pythons builtin .find() method. Since you're using
selenium you can find all the options elements, iterate over each element,
check the text of each element, and compare it to your string.

For example

    
    
    elem = browser.find_elements_by_tag_name("option") 
    for ele in elem:
      if ele.get_attribute("innerHTML").find('Red') > -1 and ele.get_attribute("innerHTML").find('wolly') > -1 and ele.get_attribute("innerHTML").find('small') > -1 and ele.get_attribute("innerHTML").find('small') > -1:
        #TODO
    

However that gets kind of long so I would use a regex, for example:

    
    
    import re
    elem = browser.find_elements_by_tag_name("option") 
    for ele in elem:
      m = re.search(r'(Red,.+wooly,.+small,.+UK)', ele.get_attribute("innerHTML"))
      if m:
        print m.group(1)
    

if `.get_attribute("innerHTML")` doesn't get the inner text try .text()

