title: selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element (XPath)
body: <p>I'm trying to code a python script using selenium to automatically pick something randomly for dinner by using the inputted location. However, I've been getting these error messages: <code>selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {&quot;method&quot;:&quot;xpath&quot;,&quot;selector&quot;:&quot;//*[@id=&quot;tsuid11&quot;]/div[2]/div/a/div/div[3]/div&quot;}</code></p>
  <p>I don't really understand why this error is happening. I've even watched the entire process load multiple times and am also certain the XPath value is correct.</p>
  <p>This is my code:</p>
  <pre><code>import requests
  import random
  import time
  from selenium import webdriver
  from selenium.webdriver.common.by import By
  from selenium.webdriver.support.ui import WebDriverWait
  from selenium.webdriver.support import expected_conditions as EC
  
  location = input(&quot;Please enter your postal code: &quot;)
  
  driver = webdriver.Chrome(&quot;&lt;path to chromedriver.exe&gt;&quot;)
  
  query = &quot;food near &quot; + location
  print(&quot;Please give us a moment...&quot;)
  driver.get(&quot;https://www.google.com/search?q=&quot; + query)
  time.sleep(3)
  
  #this helps to click the &quot;View All&quot; option to see the entire list of restaurants nearby
  view_all = driver.find_element_by_xpath('//*[@id=&quot;rso&quot;]/div[1]/div/div/div/div/div[5]/div/g-more-link/a/div/span[1]')
  view_all.click()
  time.sleep(10)
  
  #This is where I can't seem to find the element by its XPath
  name = driver.find_element_by_xpath('//*[@id=&quot;tsuid11&quot;]/div[2]/div/a/div/div[3]/div')
  print(name)
  
  driver.close()
  </code></pre>
  <p>I have also already searched this error up and got this: <a href="https://stackoverflow.com/questions/27112731/selenium-common-exceptions-nosuchelementexception-message-unable-to-locate-ele">here</a></p>
  <p>Based on the answer, the person mentioned &quot;Could be a race condition where the find element is executing before it is present on the page&quot;. However, I've already added the <code>time.sleep()</code> function to mitigate that.</p>
  <p>Any help would be appreciated :)</p>
  <p><strong>Update</strong>: I got it to work by replacing <code>find by XPath</code> to <code>find by CSS Selector</code>. However, this is just a work around, I'm still going to try to figure this one out. Thanks for all the solutions, but unfortunately none of them worked for me.</p>
  <ul>
  <li>my View All button works perfectly fine on my end</li>
  <li>the webdriver wait function also didn't allow me to find the element (I think time.sleep does the exact same thing)</li>
  <li>and the element is not inside an iframe</li>
  </ul>
  <p>After some further probing, I've tested the full XPath: <code>/html/body/div[6]/div/div[7]/div[1]/div/div/div[2]/div[2]/div/div/div/div/div/div/div/div[1]/div[4]/div[3]/div[2]/div/a/div/div[3]/div</code> on the browser console itself, and did not find anything. By digging through the layers, I've noticed that it stops at <code>/html/body/div[6]/div/div[7]/div[1]/div/div/div[2]/div[2]/div/div/div/div/div/div/div/div[1]/div[4]/div[3]/div[2]/div</code>, right before the <code>a</code> tag. I'm not sure why as of now, will update again if I find anything.</p>
  <p><strong>Update 2</strong>:
  I used class name instead of XPath which is much more consistent for my output. That fixed everything for me :) Hope this helps.</p>