Installing Selenium with Pip
Download Chromedriver, add in Path
#-*-coding:utf-8-*- fromSeleniumImportWebdriver fromSelenium.common.exceptionsImporttimeoutexception fromSelenium.webdriver.support.uiImportWebdriverwait#available since 2.4.0Import Time#Create A new instance of the browser driverDriver = Webdriver. Chrome ()##可以替换为IE (), Firefox () Chrome ( ) #go to the Google home pageDriver.get ("https://www.google.com") #find the element that's name attribute is Q (the Google search box)Inputelement = Driver.find_element_by_name ("Q") #type in the searchInputelement.send_keys ("cheese!") #submit the form. (although Google automatically searches now without submitting)Inputelement.submit ()#The page is ajaxy so the title is originally this:Print(Driver.title)Try: #we have a to-wait for the page to refresh and the last thing this seems to being updated is the titleWebdriverwait (Driver). Until (LambdaDriver:driver.title.lower (). StartsWith ("cheese!")) #You should see "cheese! -Google Search " Print(Driver.title)finally: Driver.quit ()
The simplest example of Python selenium