0

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"]"}

4
  • 1
    Checkout this answer on a similar question stackoverflow.com/a/74227905/7327747 Commented Oct 29, 2022 at 11:35
  • gives TimeoutException I wrapped it with wait.until(EC.presence_of_element_located((By.XPATH,"//.. Commented Oct 29, 2022 at 12:18
  • 1
    better show minimal working code with real URL for this page. Maybe it has something special in code which makes problem. Maybe this code is in <iframe> and it needs switch_to.frame(). OR maybe it uses ShadowDOM and it needs other solution. OR maybe server detected Selenium and it sends different HTML. Commented Oct 29, 2022 at 12:48
  • @furas if you need also minimal code snippet let me know Commented Oct 29, 2022 at 17:10

1 Answer 1

2

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"
Sign up to request clarification or add additional context in comments.

4 Comments

I did not know the reason, why. But from a youtube video I just code code below and that worked ` abc=driver.find_elements(By.XPATH,'//span[contains(@class,"a-size-medium")]') print(abc[1].text)` Thanks for your explanation. Now, I got what was difference
You are always welcome :)
You are great@Prophet, thanks for your diligent explanations I got really fast my foot in Selenium, XPaths.
It really makes me a pleasure to know I helped. In case you have more questions you can always ask here or ever write me directly, I have a private chat room on my profile. But better to ask public questions, there are a lot of highly professional people here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.