Path Selection in Selenium (Python) Not Working

skippy130

So I’ve been trying to use Selenium in Python to select some values on a drop down menu that acts as a filter for a list. The code I’ve written and the relevant xml is down below.

…
<div id=“example_wrapper” class=“dataTables_wrapper”>
    <div id=“example_filler” class=“dataTables_filter”>…</div>
    <div class=“column-filter-wdigets”>
    <div class=“column-filter-widget col-lg-2”>
        <select class=“form-control input-sm widget-2”>
            <option value>Level</option>
            <option value=“1”>1</option>
            <option value=“2”>2</option>
            <option value=“3”>3</option>
            <option value=“4”>4</option>
            <option value=“5”>5</option>
            <option value=“6”>6</option>
            <option value=“7”>7</option>
            <option value=“8”>8</option>
            <option value=“9”>9</option>
        </select>
....
import requests, bs4, time
from selenium import webdriver
from selenium.webdriver.support.ui import Select

print("\n\n")

browser = webdriver.Firefox()
browser.get('https://www.dnd-spells.com/spells')
selector = "div.column-filter-widget.col-lg-2"

try:
    time.sleep(5)
    levelList = browser.find_element_by_css_selector(selector)
    print(type(levelList))
    levelList.click()
    levelListSelector = Select(levelList)
    levelListSelector.select_by_value('3')
except:
    print("First: No matches :/ \n\n")


try:
    selector = "div > select" 
    levelOptions = browser.find_element_by_css_selector(selector)
    print(levelOptions.text())
except:
    print("Second: No matches :/ \n\n")

The issue is that while the first selector works fine, I see the drop down menu list open, the second one doesn’t return a match. I have tried as many different combinations to grab that select tag as I can, going from very specific to very generic, but nothing works. It always just says no matches found.

Having reached the end of my rope I decided to see if maybe I could get that select tag if I made it the first selection I make. So I changed selector = "div.column-filter-widget.col-lg-2" to selector = "div.column-filter-widget.col-lg-2 select" at the very top and commented out the second try/except block. Sure enough that worked. But I have no idea why.

Could someone explain why this is the case? Can we not access the same element twice from the same web driver or even a subelement? I've gotten to a point where I can move on with what I want to do but I'd like to understand the issue here so I can learn and better deal with it in the future if need be. Thank you.

KunduK

Text is property not a function that is why you are getting exceptions. Try this

 try:
        selector = "div > select" 
        levelOptions = browser.find_element_by_css_selector(selector)
        print(levelOptions.text)
    except:
        print("Second: No matches :/ \n\n")

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related