问题:Python Selenium 没有加载整页源码

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)

当我访问汤时,href 为空

<a href="" id="download" rel="nofollow">Download</a>

当我使用时间延迟时,它似乎工作正常,但我想知道如何使用 WebDriverWait 来确保加载 idu003ddownload 的 href。

解答

WebDriverWait等到下载按钮出现href

element = WebDriverWait(driver, 5).until(
        EC.presence_of_element_located((By.XPATH, './/a[@id="download" and @href!=""]'))
)
Logo

更多推荐