【发布时间】:2013-04-11 00:25:00
【问题描述】:
我正在使用 Selenium 进行网络爬虫,我想在 Selenium 模拟点击假链接后获取由 JavaScript 编写的元素(例如链接)。
我试过get_html_source(),但它不包含JavaScript编写的内容。
我写的代码:
def test_comment_url_fetch(self):
sel = self.selenium
sel.open("/rmrb")
url = sel.get_location()
#print url
if url.startswith('http://login'):
sel.open("/rmrb")
i = 1
while True:
try:
if i == 1:
sel.click("//div[@class='WB_feed_type SW_fun S_line2']/div/div/div[3]/div/a[4]")
print "click"
else:
XPath = "//div[@class='WB_feed_type SW_fun S_line2'][%d]/div/div/div[3]/div/a[4]"%i
sel.click(XPath)
print "click"
except Exception, e:
print e
break
i += 1
html = sel.get_html_source()
html_file = open("tmp\\foo.html", 'w')
html_file.write(html.encode('utf-8'))
html_file.close()
我使用 while 循环单击一系列虚假链接,这些链接触发 js-actions 以显示额外内容,而这些内容正是我想要的。但是 sel.get_html_source() 没有给出我想要的。
有人可以帮忙吗?非常感谢。
【问题讨论】:
-
请包含您已经编写的代码,并指出其中的哪一部分给您带来了问题