简体   繁体   中英

Firefox + Selenium in python: How to interactively get an element 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')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM