locating element via selenium xpath

Question:

I am trying to locate below element from enter link description here with this code x_title=driver.find_element(By.XPATH,'//h2/a/span[@class="a-size-medium"]')

it gives an error of selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//h2/a/span[@class="a-size-medium"]"}

<div class="a-section a-spacing-none puis-padding-right-small s-title-instructions-style">
   <h2 class="a-size-mini a-spacing-none a-color-base s-line-clamp-2">
      <a class="a-link-normal s-underline-text s-underline-link-text s-link-style a-text-normal" href="/Love-Languages-Secret-that-Lasts/dp/080241270X/ref=sr_1_1?qid=1667035762&amp;refinements=p_n_feature_eighteen_browse-bin%3A8622846011&amp;rnid=8622845011&amp;s=books&amp;sr=1-1">
         <span class="a-size-medium a-color-base a-text-normal">The 5 Love Languages: The Secret to Love that Lasts</span>
     </a> 
   </h2>
<div class="a-row a-size-base a-color-secondary"><div class="a-row"><a class="a-link-normal puis-light-weight-text s-underline-text s-underline-link-text s-link-style s-link-centralized-style" href="/dp/B07VVJPJ5Z?binding=kindle_edition&amp;searchxofy=true&amp;qid=1667035762&amp;sr=1-1"><span>Part of: The 5 Love Languages Series (11 books)</span> </a> <span class="a-letter-space"></span><span class="a-size-base a-color-secondary puis-light-weight-text"> | </span><span class="a-letter-space"></span><span class="a-size-base puis-light-weight-text">by </span><a class="a-size-base a-link-normal puis-light-weight-text s-underline-text s-underline-link-text s-link-style s-link-centralized-style" href="/Gary-Chapman/e/B01IAEQ73Q?ref=sr_ntt_srch_lnk_1&amp;qid=1667035762&amp;sr=1-1">Gary Chapman</a> <span class="a-letter-space"></span><span class="a-size-base a-color-secondary puis-light-weight-text"> | </span><span class="a-letter-space"></span><span class="a-size-base a-color-secondary puis-light-weight-text a-text-normal">Jan 1, 2015</span></div></div></div>

Here is xpath of element;
//*[@id="search"]/div[1]/div[1]/div/span[1]/div[1]/div[2]/div/div/div/div/div/div[2]/div/div/div[1]/h2/a/span

Edit: I have also tried x_title=driver.find_element(By.XPATH,'//div[@class="s-title-instructions-style"]//span[@class="a-size-medium"]')

error trace: selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//div[@class="s-title-instructions-style"]//span[@class="a-size-medium"]"}

Asked By: xlmaster

||

Answers:

Your mistake is that elements have multiple class names. Not only a-size-medium and s-title-instructions-style.
So, instead //div[@class="s-title-instructions-style"]//span[@class="a-size-medium"] it should be

"//div[contains(@class,"s-title-instructions-style")]//span[contains(@class,"a-size-medium")]"

You can also use this CSS Selector, it looks shorter

".s-title-instructions-style .a-size-medium"
Answered By: Prophet
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.