1

How to extract the list ['Cat1', 'Cat2', 'Cat3'] from the following block which is within the popup using Selenium python API.

...
<div class="categoryPopup" id="categoryPopup">
<div class="categoriesList"><li id="addCategory_231329891" class="edit_form_add_category">
            <input id="checkCategory_231329891" class="fl" type="checkbox"><label for="checkCategory_231329891"><span class="title fl">Cat1</span></label>
            <div class="cb"></div>
        </li><div class="cb"></div><li id="addCategory_231329901" class="edit_form_add_category">
            <input id="checkCategory_231329901" class="fl" type="checkbox"><label for="checkCategory_231329901"><span class="title fl">Cat2</span></label>
            <div class="cb"></div>
        </li><div class="cb"></div><li id="addCategory_231330011" class="edit_form_add_category">
            <input id="checkCategory_231330011" class="fl" type="checkbox"><label for="checkCategory_231330011"><span class="title fl">Cat3</span></label>
            <div class="cb"></div>
</div>
...

Tried to use the following code to no avail:

from selenium import webdriver
browser = webdriver.Firefox()
browser.get("http://targeURL")
# series of successful browser.get_element_by...
browser.get_element_by_class_name("categoriesList") # fails with NoSuchElementException
rok
  • 9,403
  • 17
  • 70
  • 126

1 Answers1

2

Try using cssSelector as follows. I am assuming you want to select all the checkboxes. The following code will return the list of inputs. You also should be using _elements_ to find the collection of elements. See the api doc.

from selenium import webdriver
browser = webdriver.Firefox()
browser.get("http://targeURL")
# series of successful browser.get_element_by...
browser.find_elements_by_css_selector("#categoryPopup input")
Saifur
  • 16,081
  • 6
  • 49
  • 73
  • Thank you, though still getting NoSuchElementException – rok Mar 14 '15 at 21:59
  • 1
    @user1264304 is there any `iframe` involved? – Saifur Mar 14 '15 at 22:05
  • 2
    Yes, it does. If the elements inside the `iframe` you need to switch to inside the frame first then look for the element. See [this](http://stackoverflow.com/questions/18924146/selenium-and-iframe-in-html) – Saifur Mar 14 '15 at 22:09
  • Thank you, i tried `browser.switch_to.active_element` (active element is u" iframe") and then the lines you suggested. Still getting the same error. Tried getting the xpath and css from Firebug. Finding by them results still in `NoSuchElementException` – rok Mar 18 '15 at 21:50
  • 1
    @user1264304 Nope. You need to use `switch_to_frame("name of frame")` See [this](http://selenium-python.readthedocs.org/en/latest/api.html?highlight=frame#selenium.webdriver.remote.webdriver.WebDriver.switch_to_frame). `switch_to.active_element()` and `switch_to_frame("name of frame")` are not same – Saifur Mar 18 '15 at 23:24
  • Thank you. I haven't used `switch_to_frame("name of frame")` because the frame doesn't have a name attribute - `` – rok Mar 19 '15 at 09:22
  • You can use `xpath` or `cssSelctor` or any element locating strategies to locate the `iframe`. Eg: `driver.switch_to_frame(driver.find_element_by_css_selector("[iframe-event-listener='messageHandler(event)']"))` see [this](http://stackoverflow.com/questions/7534622/selecting-an-iframe-using-python-selenium) – Saifur Mar 19 '15 at 12:15