получить значение атрибута по селену python - PullRequest
0 голосов
/ 27 февраля 2019

Я хочу получить ссылку "data-href" от кнопки.Я использовал Selenium. Я могу успешно войти на желаемую страницу, но когда я пытаюсь получить ссылку с кнопки, ничего не происходит, но выдает исключение selenium.common.exceptions.NoSuchElementException: я понятия не имею, где я нахожусьпошло не так.мой 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

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 Ответ

0 голосов
/ 27 февраля 2019

Убедитесь, что число 7274677 в структуре html не меняется при каждом выполнении сценария.

Если он не меняется, попробуйте xpath:
driver.find_elements_by_xpath("//div[@id='lecture-attachment-7274677']//button")

Если он меняется, попробуйте xpath:
driver.find_elements_by_xpath("//div[contains(@id,'lecture-attachment')]//button")

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...