Selenium 网页的源代码似乎不完整。
driver = webdriver.Chrome()
driver.get('https://www.youtube2mp3.cc/')
vid_name = driver.find_element_by_id('input')
vid_name.send_keys('https://www.youtube.com/watch?v=NVbH1BVXywY')
driver.find_element_by_id('button').click()
element = WebDriverWait(driver, 5).until(
EC.presence_of_element_located((By.ID, 'download'))
)
url = driver.page_source
url = str(url)
soup = BeautifulSoup(url,"html.parser")
print(soup)
当我访问 soup 时,href 是空的
<a href="" id="download" rel="nofollow">Download</a>
当我使用时间延迟时,它似乎工作正常,但我想知道如何使用 WebDriverWait 来确保带有 id=download 的 href 加载。
最佳答案
让 WebDriverWait
等到下载按钮出现 href
element = WebDriverWait(driver, 5).until(
EC.presence_of_element_located((By.XPATH, './/a[@id="download" and @href!=""]'))
)
关于Python Selenium 未加载整页源代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38335292/