2

I want to get a "data-href" link from a button. I have used Selenium.I can successfully enter in my desired page but when I am trying to get the link from the button nothing happen,but throws an exception selenium.common.exceptions.NoSuchElementException: I don't have any idea where I am going wrong. my HTML:

<div class="lecture-attachment lecture-attachment-type-audio" 
         id="lecture-attachment-7274677">
   <div class="attachment-data"></div>


    <div class="audioloader" data-audioloader="AttachmentDrop" data- 
audioloader-name="ESLPod1103.mp3" data-audioloader-type="audio/mpeg" 
data-audioloader- 
url="https://www.filepicker.io/api/file/Ius016cNTJmPRjUuCCp7" data- 
audioloader-initialized="true">
<div class="audioloader__placeholder">
   <button data- 
     href="https://www.filepicker.io/api/file/Ius016cNTJmPRjUuCCp7" 
     target="_blank" style="
     border: 0;
     outline: 0;
     background: transparent;
     cursor: pointer;
    ">
  <span class="audioloader__icon glyphicon glyphicon-play"></span>
  <span class="audioloader__name">ESLPod1103.mp3</span>
   </button>
  </div>
 </div>
</div>

Python code

from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException, \
WebDriverException
driver = webdriver.Chrome(executable_path='/Users/lmuser/chromedriver')
driver.get ("https://sso.teachable.com/secure/147717/users/sign_in? 
clean_login=true&reset_purchase_session=1")

driver.find_element_by_id("user_email").send_keys("***")
driver.find_element_by_id("user_password").send_keys("***")
driver.find_element_by_name("commit").click()
driver.get("https://tv.eslpod.com/courses/239156/lectures/3732329")
for i in driver.find_elements_by_xpath('//*[@id="lecture-attachment- 
7274677"]/div[2]/div/button'):
    print(i.get_attribute("data-href"))

1 Answer 1

1

Please check the number 7274677 in the html structure is not changing everytime you are executing the script.

If its not changing, Try the xpath:
driver.find_elements_by_xpath("//div[@id='lecture-attachment-7274677']//button")

If its changing, Try the xpath:
driver.find_elements_by_xpath("//div[contains(@id,'lecture-attachment')]//button")

Sign up to request clarification or add additional context in comments.

4 Comments

Happy to help you, though wanted to ask, was the id number getting changed or not?
id number didn't changed. btw how did you build the xpath for the button? I tried by right click on the tag and find Xpath for chrome.but as you it didn't worked. can you suggest me any resource for learning how to build xpath?
Honestly, have practiced it a lot from different websites and stackoverflow, though there is a tool named "chropath" for chrome browser which is a great tool to get the xpath, though it also gives some complex xpaths depending on the website you are using for automation.
You could have just used the css: .lecture-attachment button - simpler? Don't over-complicate these answers with xpath please.

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.