pythonselenium获取html标签内容_python中的Firefox + Selenium:如何以交互方式获取元素html?...

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

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

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:

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 allows to run javascript and pick up the returning values into the python script, so, theoretically:

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?

Clarification for Amey's comment:

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.

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

解决方案

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.

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.

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值