显示隐藏的网页表

时间:2018-08-28 17:06:50

标签: python html selenium

我正在尝试刮擦桌子,但似乎无法使其可见。

在浅紫色区域中展开“代码历史记录”部分后,表格位于此page上。登录凭据在下面,但也很容易从试用帐户中获取:

  • 用户名= jd@mailinator.com
  • 密码= m%$)-Y95 * ^。1Gin +

下面的图形说明了我要获取的数据。我对底行感兴趣:

enter image description here

这是我正在使用的代码:

from selenium import webdriver
driver_path = "path to chromedriver.exe"
url_login = "https://www.findacode.com/signin.html"
url_code = "https://www.findacode.com/code.php?set=CPT&c="
username = 'jd@mailinator.com'
password = 'm%$)-Y95*^.1Gin+'

options = webdriver.ChromeOptions()
options.add_argument('headless')
driver = webdriver.Chrome(executable_path=driver_path, chrome_options=options)

driver.get(url_login)
form = driver.find_element_by_name('login')
form.find_element_by_name('id').send_keys(username)
form.find_element_by_name('password').send_keys(password)
form.find_element_by_xpath("//input[@value='Sign In']").submit()

driver.get(url_code+'0001U')
driver.find_element_by_id('history').click()

在这一点上,当我查看driver.page_source时,我期望表格的元素是可见的,但事实并非如此。我的思维有何缺陷?

1 个答案:

答案 0 :(得分:1)

此站点在需要时加载页面片段(也称为延迟加载)。因此,当页面的该部分扩展时,将加载实际内容。当您的“试用版”到期时,这有帮助,服务器可以将通用内容退回以防止未经授权的访问。

我可以看到3种补救方法:

  1. 等待#history.click()和内容div加载后数据可用(以下.sectionbody div不为空)。
  2. 登录后直接调用相同的URL,即可从片段中获取数据。即.get("https://www.findacode.com/logs/codepage_stats.php?section=sh_history_div&set=CPT&c=0001U")
  3. 利用其内置的自动打开功能,选中相应的复选框一次,然后在以后的请求中正常加载所有期望的数据。