Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Mechanize Python隐藏HTML元素_Python_Html_Web Scraping_Mechanize_Hidden - Fatal编程技术网

使用Mechanize Python隐藏HTML元素

使用Mechanize Python隐藏HTML元素,python,html,web-scraping,mechanize,hidden,Python,Html,Web Scraping,Mechanize,Hidden,所以我正在编写一个Python脚本,检查黑板(学校界面站点)的更新。但是我从脚本返回的HTML与在浏览器中查看的HTML并不完全相同。我不确定这是一个饼干问题还是我遗漏了什么 USERNAME = '' PASSWORD = '' updates = 0 site = 'http://schoolsite.edu' browser = mechanize.Browser() browser.open(site) browser.select_form(nr = 0) browser.fo

所以我正在编写一个Python脚本,检查黑板(学校界面站点)的更新。但是我从脚本返回的HTML与在浏览器中查看的HTML并不完全相同。我不确定这是一个饼干问题还是我遗漏了什么

USERNAME = ''
PASSWORD = ''

updates = 0  
site = 'http://schoolsite.edu'

browser = mechanize.Browser()
browser.open(site)
browser.select_form(nr = 0)
browser.form['j_username'] = USERNAME
browser.form['j_password'] = PASSWORD
browser.submit()

#it brings back an empty form, just submit it.
browser.select_form(nr = 0)
browser.submit()

html_resp = browser.response().read()
所讨论的HTML如下所示(这来自脚本)


活动更新
我期望它看起来像什么(从Chrome/实际浏览器)


活动更新
1.
我真正想要的是最后一行中的“1”数字,但我觉得可见性属性阻碍了它。请注意,我从Mechanize获得的cookie与在浏览器中获得的cookie相同。(不完全相同,但id、名称等相同。)

有什么想法吗

非常感谢您的任何意见。

非常肯定其中涉及javascript

另一种解决方案是通过以下方式自动化真正的浏览器:


哇!令人惊叹的。谢谢@alecxe。成功了!我不想处理Firefox的打开和关闭,所以我使用了这个。幸好你添加了关于PhantomJS的评论。我还不得不为它添加一个延迟来获取徽章计数。所以最后几行看起来像:
badge\u count=WebDriverWait(driver,10)。直到(EC.presence\u of_element\u located((By.ID,“badgeTotalCount”))
sleep(2)
打印(badge\u count.text)
我知道这并不优雅,但它可以工作。
<span id="badgeTotal" style="visibility: hidden" title="">
<span class="hideoff" id="badgeAXLabel">Activity Updates</span>
<span class="badge" id="badgeTotalCount" title=""></span>
<span id="badgeTotal" style="visibility: visible;" title="">
<span class="hideoff" id="badgeAXLabel">Activity Updates</span>
<span class="badge" id="badgeTotalCount" title="">1</span>
from selenium import webdriver
from selenium.common.exceptions import TimeoutException
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.Firefox()  # could also be headless: webdriver.PhantomJS()
driver.get('http://schoolsite.edu')

# submit a login form
username = driver.find_element_by_name('j_username')
password = driver.find_element_by_name('j_password')

username.send_keys(USERNAME)
password.send_keys(PASSWORD)

username.submit()

# wait for the badge count to appear
badge_count = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "badgeTotalCount")))

print(badge_count.text)