Ghost.py Returns List

I just installed Ghost.py to clean up a site requiring javascript. Is there anyway to get an iterable list of forms on the current page, just like the mechanization module with mechanize.Browser().forms() ? Or if I can not transfer the page (after downloading the entire javascript file) to the machine library and let it fill in / submit forms?

+4
source share
1 answer

Selenium can do this for you if you do not mind the browser displaying on your screen. It can also work without a head, but it is more difficult. A simple solution:

 from selenium import webdriver driver = webdriver.Firefox() url = "http://www.w3schools.com/html/html_forms.asp" driver.get(url) # get a list of the page forms as Selenium WebElements # (webdriver API ref: http://selenium-python.readthedocs.org/en/latest/api.html) forms = driver.find_elements_by_xpath(".//form") for i, form in enumerate(forms): print i, form.text # the last form, index number 5, has input tags of type "text" and "submit" """ <form name="input0" target="_blank" action="html_form_action.asp" method="get"> " Username: " <input type="text" name="user" size="20"> <input type="submit" value="Submit"> </form> """ # get the input WebElements from this form WebElement inputs = forms[5].find_elements_by_xpath(".//input") # write text to the text input, then submit the form inputs[0].send_keys('hihi frds!') inputs[1].submit() 
0
source

Source: https://habr.com/ru/post/1482905/


All Articles