-1

I am trying to capture a button to click on it. the source url is:

The goal is to provide free information to website users and owners regarding website security status.
                <br>
                <br>
                Use it wisely or we'll have to take it away.
            </p>
        </div><div class="ui-dialog-buttonpane ui-widget-content ui-helper-clearfix"><div class="ui-dialog-buttonset"><button type="button" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only ui-state-focus" role="button" aria-disabled="false"><span class="ui-button-text">Accept</span></button></div></div></div></body></html>

What I tried:

from selenium import webdriver
driver = webdriver.PhantomJS()
url='http://example.com'
driver.get(url)
driver.page_source
driver.find_element_by_xpath('//button[@type="button"]/span[@class="ui-button-text"]/text()').click()
driver.quit()

The Error Message is:

File "u.py", line 8, in <module>
    driver.find_element_by_xpath('//button[@type="button"]/span[@class="ui-button-text"]/text()').click()
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 230, in find_element_by_xpath
    return self.find_element(by=By.XPATH, value=xpath)
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 662, in find_element
    {'using': by, 'value': value})['value']
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 173, in execute
    self.error_handler.check_response(response)
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/errorhandler.py", line 166, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.InvalidSelectorException: Message: u'Error Message => \'The result of the xpath expression "//button[@type="button"]/span[@class="ui-button-text"]/text()" is: [object Text]. It should be an element.\'\n caused by Request => {"headers":{"Accept":"application/json","Accept-Encoding":"identity","Connection":"close","Content-Length":"149","Content-Type":"application/json;charset=UTF-8","Host":"127.0.0.1:57776","User-Agent":"Python-urllib/2.7"},"httpVersion":"1.1","method":"POST","post":"{\\"using\\": \\"xpath\\", \\"sessionId\\": \\"7b17cc00-6500-11e4-9c4e-e1b4bf9ba927\\", \\"value\\": \\"//button[@type=\\\\\\"button\\\\\\"]/span[@class=\\\\\\"ui-button-text\\\\\\"]/text()\\"}","url":"/element","urlParsed":{"anchor":"","query":"","file":"element","directory":"/","path":"/element","relative":"/element","port":"","host":"","password":"","user":"","userInfo":"","authority":"","protocol":"","source":"/element","queryKey":{},"chunks":["element"]},"urlOriginal":"/session/7b17cc00-6500-11e4-9c4e-e1b4bf9ba927/element"}' ; Screenshot: available via screen 

UPDATE:

When I use it in the following way it works:

from lxml import html
cont="""<br>
                    <br>
                    Use it wisely or we'll have to take it away.
                </p>
            </div><div class="ui-dialog-buttonpane ui-widget-content ui-helper-clearfix"><div class="ui-dialog-buttonset"><button type="button" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only ui-state-focus" role="button" aria-disabled="false"><span class="ui-button-text">Accept</span></button></div></div></div></body></html>
"""

tree=html.fromstring(cont)
print tree.xpath('//button[@type="button"]/span[@class="ui-button-text"]/text()')

returns:

['Accept']
MLSC
  • 5,872
  • 8
  • 55
  • 89
  • 3
    The error message tells you everything you need to know. Removed the `/text()` from the XPath expression. – Artjom B. Nov 05 '14 at 15:33
  • But without usig selenium I tested that xpath structure and returned me `Accept` – MLSC Nov 05 '14 at 15:37
  • 3
    Yes, the text not the element. Text is not clickable. You need the surrounding element for that. – Artjom B. Nov 05 '14 at 15:38
  • please see the update part – MLSC Nov 05 '14 at 15:45
  • 2
    Yes, of course it returns the text. But you can't click a text node. You need to click the span which contains the text. Text cannot have a click handler. I don't know how I can make it clearer. Have you tried my suggestion from the first comment? – Artjom B. Nov 05 '14 at 15:48

1 Answers1

2

Use this:

driver.find_element_by_xpath(
    '//button[@type="button"]/span[@class="ui-button-text"]').click()

without the /text() at the end of your XPath expression. What Artjom B. was explaining to you in the comments is that if you use find_element_by_xpath, the XPath expression you pass to this method must resolve to an HTML element. The XPath you were giving it is valid as far as XPath is concerned, yes. But the fact that it is a valid XPath expression is not enough. The value of the expression you have in your question is a string, not an element, but Selenium absolutely needs an element.

Louis
  • 146,715
  • 28
  • 274
  • 320
  • +1 Thank you... I want to get source of page after clicking on Accept, So what I should do now? – MLSC Nov 05 '14 at 18:34
  • 1
    That's a completely different question from the one you originally asked here. See [this question](http://stackoverflow.com/q/7263824/1906307) for ways to do it. If the methods there do not help, then post a new question and *make sure to explain why your question is not a duplicate of the one I gave a link to*. Otherwise, it is going to be closed as duplicate. – Louis Nov 05 '14 at 18:36