python中的Firefox + Selenium:如何以交互方式获取元素html? [英] Firefox + Selenium in python: How to interactively get an element html?

查看:333
本文介绍了python中的Firefox + Selenium:如何以交互方式获取元素html?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Python + Selenium + Splinter + Firefox创建交互式Web搜寻器.

Im using Python + Selenium + Splinter + Firefox to create an interactive web crawler.

python脚本提供了这些选项,然后Selenium打开Firefox并发送一些命令.

The python script offers the options, then Selenium opens Firefox and sends some orders.

现在,我需要让python脚本知道用户想要与之交互的Web元素.

Right now, I need to let the python script know the web element that the user wants to interact with.

我当前使用的 方法 是:

The method I currently use is:

右键单击网站(Firefox)中的项目,然后单击检查". 元素",然后在Firefox检查器中单击,单击复制HTML",然后 手动将其提供给脚本,然后脚本便可以继续.

Right-click the item in the website (Firefox), click 'inspect element', then click in the Firefox inspector, click 'copy HTML', then feed it manually to the script, which will then be able to go on.

但是出于明显的原因,我认为这个过程远非完美.

But for obvious reasons I feel this process is far from perfection.

我一无所知,但是在阅读

I know nothing of javascript, but after reading other questions I get the feeling that javascript could actually be the solution.

Splinter允许运行javascript并将返回的值提取到python脚本中,因此,从理论上讲:

Splinter allows to run javascript and pick up the returning values into the python script, so, theoretically:

是否可以运行一个JavaScript代码,该代码将返回用户单击的下一个元素的html代码?因此,命名为 method 的方法只是右键单击所需的元素吗?

Would it be possible to run a javascript code that would return the html code of the next element the user clicks? So the named method would only be right-clicking the desired element?

澄清Amey的评论:

python脚本打开Firefox窗口,该控件仍保留在脚本中. 借助splinter,可以执行javascript代码,并在完成/信息返回后等待. 这意味着python脚本可以要求用户在其拥有的Firefox窗口中单击或右键单击,因此其目的是启动一个JavaScript,以捕获"用户单击的元素.

The python script opens a Firefox window, which control is still retained from the script. And with splinter, javascript code can be executed and waited upon completion / information return. This means that the python script can ask the user to click or right-click in the Firefox window that it owns, so the aim would be to launch a javascript that would "catch" which element the user clicks on.

JavaScript是否足以捕获所需的元素?

Would that be enough for javascript to catch the desired element?

推荐答案

这是一个有趣的问题.我的策略是使用Javascript将侦听器添加到您要定位的元素中.由于您未指定元素的类型,因此我使用了链接.不过,可以很容易地对此进行修改.

This was an interesting question. My strategy was to use Javascript to add listeners to the elements you're targeting. Since you didn't specify what types of elements, I used links. This could easily be adapted though.

单击某个元素时,侦听器将使用您指定的ID创建一个新的页面元素,并将value属性设置为相关信息.

When an element is clicked, the listener creates a new page element with an ID you specify and sets the value attribute to the relevant information.

然后,假设您已设置driver.implicitly_wait,则只需等待元素出现.

Then, assuming you've set driver.implicitly_wait, you can just wait for the element to appear.

driver.execute_script("for(var i = 0; i < document.links.length; i++){document.links[i].onclick = function clicked(){var e = document.createElement('a'); e.setAttribute('id','myUniqueID'); e.setAttribute('value', this); document.getElementsByTagName('body')[0].appendChild(e);};}")

clicked = driver.find_element_by_id('myUniqueID').get_attribute('value')

这篇关于python中的Firefox + Selenium:如何以交互方式获取元素html?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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