Python Can';t在Selenium中查看完整的页面源代码

Python Can';t在Selenium中查看完整的页面源代码,python,selenium,selenium-webdriver,bs4,Python,Selenium,Selenium Webdriver,Bs4,当我通过Chrome手动导航到站点后查看源HTML时,我可以看到完整的页面源代码,但是通过selenium加载页面源代码时,我没有得到完整的页面源代码 from bs4 import BeautifulSoup from selenium import webdriver import sys,time driver = webdriver.Chrome(executable_path=r"C:\Python27\Scripts\chromedriver.exe") driver.get('

当我通过Chrome手动导航到站点后查看源HTML时,我可以看到完整的页面源代码,但是通过selenium加载页面源代码时,我没有得到完整的页面源代码

from bs4 import BeautifulSoup
from selenium import webdriver
import sys,time


driver = webdriver.Chrome(executable_path=r"C:\Python27\Scripts\chromedriver.exe")
driver.get('http://www.magicbricks.com/')


driver.find_element_by_id("buyTab").click()

time.sleep(5)
driver.find_element_by_id("keyword").send_keys("Navi Mumbai")

time.sleep(5)
driver.find_element_by_id("btnPropertySearch").click()

time.sleep(30)

content = driver.page_source.encode('utf-8').strip()

soup = BeautifulSoup(content,"lxml")

print soup.prettify()

该网站可能正在阻止或限制selenium的用户代理。一个简单的测试是更改用户代理,看看是否这样做。有关此问题的更多信息:

引述:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
opts = Options()
opts.add_argument("user-agent=whatever you want")

driver = webdriver.Chrome(chrome_options=opts)
尝试以下方法:

import time
time.sleep(5)
content = driver.execute_script("return document.getElementsByTagName('html')[0].innerHTML")
而不是
driver.page\u source


动态网页通常需要用JavaScript呈现。

你能用webdriver添加你错过的网页源代码吗?你有没有试过在
时间之后加上sleep(5)
或其他任意时间http://www.magicbricks.com/)?这可能是因为页面加载速度不够快,无法让您正在寻找的组件可用。此外,我注意到该站点在您开始使用时会出现一个弹出窗口。由于这个弹出窗口,我不得不点击“btnPropertySearch”按钮两次。不过我能看到所有的源代码。你能详细说明一下你看不到的东西吗?