【问题标题】:Python Selenium not loading full page sourcePython Selenium 未加载整页源代码
【发布时间】:2016-07-12 17:19:54
【问题描述】:

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。

【问题讨论】:

    标签: python selenium web


    【解决方案1】:

    WebDriverWait 等到下载按钮出现href

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

    【讨论】:

      【解决方案2】:
      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"))
          )
      

      【讨论】:

        猜你喜欢
        • 2020-12-13
        • 2017-01-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-12-13
        相关资源
        最近更新 更多