2
<div class="ui-dialog-buttonpane ui-widget-content ui-helper-clearfix">
    <div class="ui-dialog-buttonset">
        <button class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" type="button" role="button" aria-disabled="false">
            <span class="ui-button-text">

                Export

            </span>
        </button>
        <button class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" type="button" role="button" aria-disabled="false">
            <span class="ui-button-text">

                Cancel

            </span>
        </button>
    </div>
</div>

For above html, I want to select Export button. But the following codes didn't work. What's wrong?

find_element_by_css_selector('Export').click()
find_element_by_xpath(".//*[contains(text(), 'Export')]").click()
find_element_by_link_text('Export').click()
2
  • What's your result when you set to a variable e.g rslt = find_element_by_link_text('Export').click() Commented Mar 20, 2014 at 12:33
  • NoSuchElementException: Message: u'Unable to locate element Commented Mar 20, 2014 at 12:37

1 Answer 1

2
find_element_by_css_selector('Export').click()

This will not work because it's looking for an <Export> element which obviously does not appear.

find_element_by_link_text('Export').click()

This will not work because the button is not an <a>. by_link_text will only look for <a> elements.

find_element_by_xpath(".//*[contains(text(), 'Export')]").click()

Not completely sure why this wouldn't be working, but i'm not an xpath guy.. I preach CSS.

Try this:

find_element_by_css_selector('div.ui-dialog-buttonpane > div.ui-dialog-buttonset > button:nth-child(1)").click()
Sign up to request clarification or add additional context in comments.

5 Comments

yes your css preaching really worked! could you explain this code?
i would be happy to know meaning of button:nth-child(1)
sure! take a look at my blog post, I explain more about css selectors, and how my thought process was: ddavison.github.io/css/2014/02/18/effective-css-selectors.html
per the nth-child() thing: stackoverflow.com/questions/19909458/…
Thanks a lot it helped !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.