I am use this Python 3 code to get some download url from webpage, when the webpage opened, I want to send a click event to the element, and trigger to generate download url, then fetch the download url, but now I could not trigger click event,shows error like this:
File "/Users/dolphin/Library/Application Support/JetBrains/Toolbox/apps/PyCharm-P/ch-0/212.5457.59/PyCharm.app/Contents/plugins/python/helpers/pydev/pydevd.py", line 1483, in _exec
pydev_imports.execfile(file, globals, locals) # execute the script
File "/Users/dolphin/Library/Application Support/JetBrains/Toolbox/apps/PyCharm-P/ch-0/212.5457.59/PyCharm.app/Contents/plugins/python/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile
exec(compile(contents "\n", file, 'exec'), glob, loc)
File "/Users/dolphin/source/reddwarf/backend/pydolphin/dolphin/biz/music/fetch.py", line 19, in <module>
driver.find_element(By.CSS_SELECTOR, "aplayer-list-download iconfont icon-xiazai").click()
File "/usr/local/anaconda3/envs/pydolphin/lib/python3.9/site-packages/selenium/webdriver/remote/webdriver.py", line 1244, in find_element
return self.execute(Command.FIND_ELEMENT, {
File "/usr/local/anaconda3/envs/pydolphin/lib/python3.9/site-packages/selenium/webdriver/remote/webdriver.py", line 424, in execute
self.error_handler.check_response(response)
File "/usr/local/anaconda3/envs/pydolphin/lib/python3.9/site-packages/selenium/webdriver/remote/errorhandler.py", line 247, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"aplayer-list-download iconfont icon-xiazai"}
(Session info: chrome=96.0.4664.55)
and this is my code to trigger the download event:
from selenium.webdriver.chrome.service import Service
from selenium import webdriver
from selenium.webdriver.common.by import By
from webdriver_manager.chrome import ChromeDriverManager
class FetchMusic:
def __init__(self):
print("fetch...")
if __name__ == '__main__':
s = Service(ChromeDriverManager().install())
driver = webdriver.Chrome(service=s)
# driver.maximize_window()
driver.get('http://tool.liumingye.cn/music/?page=audioPage&type=migu&name=Unstoppable')
driver.implicitly_wait(5)
driver.find_element(By.CSS_SELECTOR, "aplayer-list-download iconfont icon-xiazai").click()
modal_body = driver.find_element(By.CLASS_NAME, "modal-body").get_attribute("href")
# results = driver.page_source
for result in modal_body:
print(result.text)
what should I do to make it work?
CodePudding user response:
You forgot to add class selector .
before classes names.
driver.find_element(By.CSS_SELECTOR, ".aplayer-list-download.iconfont.icon-xiazai").click()
and the next line will not work
modal_body = driver.find_element(By.CLASS_NAME, "modal-body").get_attribute("href")
because the modal body div has not attribute called href
, so in case you want to extract all URLs inside modal-body div you can use querySelector API from Javascript. see below
urls = [a.get_attribute('href') for a in driver.execute_script('return document.querySelectorAll(".modal-body a[href*=\'http\']")')]