Python Selenium没有加载整页源代码

时间:2016-07-12 17:19:54

标签: python selenium web

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来确保id = download的href加载。

2 个答案:

答案 0 :(得分:0)

WebDriverWait等到下载按钮有href

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

答案 1 :(得分:0)

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Chrome()
driver.get("https://www.youtube2mp3.cc/")
try:
    element = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.ID, "download"))
    )