title: Search for Error Elements in Selenium After .submit() Using Python
body: <p>I have some selenium code to input various search terms into a website's search field using the following code:</p>
  
  <pre><code>browser = webdriver.Chrome()
  browser.get(url)
  
  search_box1 = browser.find_element_by_id('searchText-0')
  search_box2 = browser.find_element_by_id('searchText-2-dateInput')
  
  search_box1.send_keys("Foobars")
  search_box1.send_keys("2013")
  
  search_box1.submit()
  </code></pre>
  
  <p>and then I have more code written to grab the number of hits that result from the given search query. However, for some values of "Foobars" in particular years, there are no hits and the query results in a page like this:</p>
  
  <pre><code>&lt;body class="search"&gt;
      &lt;div id="skip"&gt;...&lt;/div&gt;
      &lt;div style = "display:non;"&gt;...&lt;/div&gt;
      &lt;div id="container" class="js"&gt;
          &lt;div id="header"&gt;...&lt;/div&gt;
          &lt;div id="search"&gt;...&lt;/div&gt;
          &lt;div id="helpContent"&gt;...&lt;/div&gt;
          &lt;div id="main-body" class="noBg"&gt;
             &lt;div class="error"&gt;
                &lt;div&gt;Sorry. There are no articles that contain all the keywords you entered.&lt;/div&gt;
                &lt;p&gt;Possible reasons:
                &lt;/p&gt;
                &lt;ul&gt;&lt;/ul&gt;
                &lt;p&gt;&lt;/p&gt;
                &lt;p&gt;&lt;/p&gt;
            &lt;/div&gt;
          &lt;/div&gt;
  </code></pre>
  
  <p>How can I check that the search query is this rather than the page I get when there are hits from the search query? I was going to implement an <code>if</code> statement to check that the search query returned something, but I can't seem to figure out the right syntax to get the error element I need to do this. I've tried things like:</p>
  
  <pre><code>Error=browser.find_element_by_name('error')
  </code></pre>
  
  <p>or</p>
  
  <pre><code>Error=browser.find_element_by_xpath("//div[@class='error']")
  </code></pre>
  
  <p>But I keep getting the error:</p>
  
  <pre><code>selenium.common.exceptions.NoSuchElementException: Message: u'no such element\n
  </code></pre>
  
  <p>I want to identify the error element so I can do something like</p>
  
  <pre><code>if Error == "There are no articles that contain all the keywords you entered":
        do something
  else:
        do something else
  </code></pre>
  
  <p>or even better, something that will tell me if the error exists to use for the conditional. Any help would be much appreciated.</p>