当前位置: 首页 > 面试题库 >

How can I get html content written by JavaScript with Selenium/Python [duplicate]

丁均
2023-03-14
问题内容

This question already has answers here :

Get HTML source of WebElement in Selenium WebDriver using
Python
(15 answers)

Closed 6 years ago.

I’m doing web-crawling with Selenium and I want to get an element(such as a
link) written by JavaScript after Selenium simulating clicking on a fake link.

I tried get_html_source(), but it doesn’t include the content written by
JavaScript.

Code I’ve written:

    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()

I use a while-loop to click a series of fake links which trigger js-actions to
show extra content, and that content is what I want. But sel.get_html_source()
didn’t give what I want.

Anybody may help? Thanks a lot.


问题答案:

Since I usually do post-processing on the fetched nodes I run JavaScript
directly in the browser with execute_script. For example to get all a-tags:

js_code = "return document.getElementsByTagName('a')"
your_elements = sel.execute_script(js_code)

Edit: execute_script and get_eval are equivalent except that get_eval
performs an implicit return, in execute_script it has to be stated
explicitly.



 类似资料:
  • 我有一个数据框我想选择列A的值在[2,3]中的行 为此,我编写了一个简单的for循环: 有没有任何内置函数可以代替使用for循环来实现这一点?

  • Python Python 诞生之初就被誉为最容易上手的编程语言。进入火热的 AI 人工智能时代后,它也逐渐取代 Java,成为编程界的头牌语言。 Python 是一门新手友好、功能强大、高效灵活的编程语言,学会之后无论是想进入数据分析、人工智能、网站开发这些领域,还是希望掌握第一门编程语言,都可以用 Python 来开启无限未来的无限可能! 语言排行榜 编程之旅 Python 适合谁来学习? 想

  • 一些相关名词 Python 是一门简单的高级动态语言,首次发布于 1991 年。它语法简单,使用缩进来定义代码块。 Python 支持命令式程序设计、面向对象、函数式编程、面向方面的程序设计、泛型等多种编程范式, 是一门优秀的多范式语言。 名词解释 技术名词 名词 解释 Python 通常指 Python 语言本身,并不包括可执行程序,但是在口语中常常与 Python 解释器混用。 CPython

  • 为了在python中运用mongols,我提供了一个pymongols。它包括http_server和web_server。 仓库在pymongols 依赖 mongols python2,3 devel 安装 很简单,cd pymongols && make clean && make && sudo make install 修改Makefile中的PYVERSION变量即可轻松适配开发者版本

  • Python (发音:[ 'paiθ(ə)n; (US) 'paiθɔn ]n.蟒蛇,巨蛇 ),是一种面向对象的解释性的计算机程序设计语言,也是一种功能强大而完善的通用型语言,已经具有十多年的发展历史,成熟且稳定。Python 具有脚本语言中最丰富和强大的类库,足以支持绝大多数日常应用。 Python 语言的特点: 简单————Python是一种代表简单主义思想的语言。阅读一个良好的Python程

  • 用Python编写的代码看起来与用其他传统编程语言(如C或Pascal)编写的代码非常相似。 还有人说,Python的语法是从C语言中大量借用的。这包括许多类似于C语言的Python关键字。 Python包括条件语句和循环语句,可用于准确提取数据以进行取证。 对于流控制,它提供if/else , while和循环遍历任何“可迭代”对象的高级for语句。 if a < b: max = b

  • 由于我们需要Python用于计算取证的所有活动,让我们一步一步地移动并了解如何安装它。 Step 1 - 转到https://www.python.org/downloads/并根据系统上的操作系统下载Python的安装文件。 Step 2 - 下载软件包/安装程序后,单击exe文件以开始安装过程。 安装完成后,您将看到以下屏幕。 Step 3 - 下一步是在系统中设置Python的环境变量。 S

  • 试图使re.split正常工作。输入=“A1 a2 a3,a4,A5”