SFro SFro - 6 months ago 44
Python Question

Clicking button within span with no ID using Selenium

I'm trying to have click a button in the browser with Selenium and Python.

The button is within the following

<div id="generate">
<i class="fa fa-bolt"></i>
<span>Download Slides</span>
<div class="clear"></div>

Chrome's dev console tells me the button is within
but I have no idea how to reference the button for a


Well, if you just want to click on an element without an id or name, I'd suggest three ways to do it:

  1. use xpath:

  2. use CSS selector:

    driver.find_element_by_css_selector('#generate > span')
  3. Just try .find_element_by_tag_name() like:


    Note that this way first try to get the generate <div> element by it's id, and then finds all the <span> elements under that <div>.

    Finally, gets the first <span> element use [0].