selenium python stuck on this line
Question: Why does my code:

    
    
    self.driver.find_element_by_xpath("//*[text()[contains(.,'%s')]]" % ('Sorry'))
    

Get stuck and won't pass this line? Even if I do something like:

    
    
    driver.implicitly_wait(30)
    self.driver.find_element_by_xpath("//*[text()[contains(.,'%s')]]" % ('Sorry'))
    

Complete code:

    
    
    # gets stuck here
    if self.is_text_present('Hello'):
        print 'Hello'
    
    # rest of code
    
    def is_text_present(self, text):
        try:
            self.driver.find_element_by_xpath('//*[contains(text(), "%s")]' % (text))
        except NoSuchElementException, e:
            return False
        return True
    


Answer: Your XPath can be simplified to

    
    
    "//*[contains(text(),'%s')]" % ('Sorry')
    

Perhaps try something like:

    
    
    import contextlib
    import selenium.webdriver as webdriver
    import selenium.webdriver.support.ui as ui
    
    with contextlib.closing(webdriver.Firefox()) as driver:
        ...
        # Set up a WebDriverWait instance that will poll for up to 10 seconds
        wait = ui.WebDriverWait(driver, 10)
        # wait.until returns the value of the callback
        elt = wait.until(
            lambda driver: driver.find_element_by_xpath(
                "//*[contains(text(),'%s')]" % ('Sorry')
                ))
    

From [the docs](http://seleniumhq.org/docs/04_webdriver_advanced.html):

> This waits up to 10 seconds before throwing a TimeoutException or if it
> finds the element will return it in 0 - 10 seconds.

* * *

To debug the problem you might try saving the HTML source to a file right
before calling `find_element_by_xpath` so you can see what the driver is
seeing. Is the XPath valid for that HTML?.

    
    
    def is_text_present(self, text):
        with open('/tmp/debug.html', 'w') as f:
            time.sleep(5)  # crude, but perhaps effective enough for debugging here. 
            f.write(driver.page_source)
        try:
            self.driver.find_element_by_xpath('//*[contains(text(), "%s")]' % (text))
        except NoSuchElementException, e:
            return False
        return True
    

