需要从硒服务器中转储带有元素ID的整个DOM树 [英] Need to dump entire DOM tree with element id from selenium server

查看:79
本文介绍了需要从硒服务器中转储带有元素ID的整个DOM树的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用python硒进行Web自动化测试.自动化的关键部分是在HTML页面中为用户可见的对象找到正确的元素.以下API大部分时间都可以使用,但并非所有时间都可以使用.

I have been using python selenium for web automation testing. The key part of automation is to find the right element for a user-visible object in a HTML page. The following API will work most of the time, but not all the time.

find_element_by_xxx,  xxx can be id, name, xpath, tag_name etc. 

当HTML页面过于复杂时,我想搜索dom树.想知道是否可以要求Selenium服务器序列化整个DOM(具有可用于通过WebDriver服务器执行操作的元素ID).客户端(python脚本)可以执行自己的搜索算法以找到正确的元素.

When HTML page is too complicated, I would like to search the dom tree. Wonder if it's possible to ask the selenium server to serialize the entire DOM (with the element id that can be used to perform action on through webdriver server). Client side (python script) can do its own search algorithm to find the right element.

请注意,python selenium可以通过

Note that python selenium can get the entire html page by

drv.page_source

但是,从硒服务器的角度来看,对此解析不会给出内部元素ID,因此没有用.

However, parsing this doesn't give the internal element id from selenium server's point of view, hence not useful.

释义以使其更清楚(感谢@alecxe):这里需要的是硒服务器中所有DOM元素的序列化表示(保留其DOM结构),可以将该序列化表示发送到客户端(python硒测试应用程序)可以自己进行搜索.

Paraphrase it to make it more clear (thanks @alecxe): what's needed here is a serialized representation of all the DOM elements (with their DOM structure preserved) in the selenium server, this serialized representation can be sent to the client side (a python selenium test app) which can do its own search.

推荐答案

尝试:

find_elements_by_xpath("//*")

那应该匹配文档中的所有元素.

That should match all elements in the document.

更新(以匹配问题的细化):

UPDATE (to match question refinements):

使用javascript并以字符串形式返回DOM:

Use javascript and return the DOM as a string:

execute_script("return document.documentElement.outerHTML")

这篇关于需要从硒服务器中转储带有元素ID的整个DOM树的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆