Python+selenium Automated Software Testing (6th): Selenium PHANTOMJS page parsing using

Source: Internet
Author: User
Tags xpath dcap

We all know selenium is a web-based automated testing tool that can operate multiple browsers on multiple platforms, such as running a browser, accessing a page, clicking a button, submitting a form, browser window adjustment, right mouse button and drag-and-drop action, drop-down boxes and dialog box processing, and so on, we use it when crawling, The main is selenium can render the page, run the page JS, as well as its click button, submit the form and other operations.

 from Import  = webdriver. PHANTOMJS () driver.get ("http://www.xxxxxx.com"= driver.title Print Data

Why should we use PHANTOMJS?

Introduced

PHANTOMJS is a WebKit-based JavaScript API. Anything you can do on a WebKit browser can do it. Not only is it an invisible browser (a browser without a UI interface), it provides such things as CSS selectors, web standards support, DOM manipulation, JSON, HTML5, Canvas, SVG, and so on, as well as handling file I/O, allowing you to read and write files to the operating system. The usefulness of PHANTOMJS is very extensive, such as the front-end Interface automation testing (need to combine Jasmin), network Monitoring, Web page screenshots and so on.

Install under Windows:

Pip Install Selenium

PHANTOMJS uses a simple way to use:

 fromSeleniumImportWebdriverbrowser= Webdriver. Phantomds ('D:\phantomjs.exe')#Browser initialization, win under the need to set PHANTOMJS path, Linux under empty canURL ='http://www.xxxxxx.com ' # set access path addressBrowser.get (URL)#Open Web pagetitle = Browser.find_elements_by_xpath ('xxxxxx')#get elements with XPath forTinchTitle#Traverse Output    PrintT.text#output where text    PrintT.get_attribute ('class’)#Output attribute valueBROWSER.QIIIT ()#close the browser. Remember to close in the task browser when an exception occurs

Let's take a simple comparison, and first review the operation of selenium Webdriver

 from Import  =//www.xxxxxx.com/") dniver.find_element_by_id ('xxxxxxxx' ) ). Send_keys ("nxxxxxx") dniver.find_element_by_id ("xxxxxxxx " ). Click () driver.quit ()

Using PHANTOMJS

 fromSeleniumImportWebdriverdriver=Webdriver. PHANTOMJS () driver.set_window_size (xxx,xxx)#Browser SizeDriver.get ("https://www.xxx.com/") dniver.find_element_by_id ('xxxx'). Send_keys ("xxxx") dniver.find_element_by_id ("xxxxxx"). Click ()Printdriver.current_urldriver.quit ()

Through the above two cases we should be able to see a difference between the relevant!!
Write a simple assertion to determine if the URL obtained by PHANTOMJS is correct:

ImportUnitTest fromSeleniumImportWebdriverclassTestone (unittest. TestCase):defsetUp (self): Self.driver=Webdniver. Phantomds () self.driver.set_window_size (xxx, XXX)defTest_url (self): Self.driver.get ("https://www.xxx.com") self.driver.find_element_by_id ('xxxxxx'). Send_keys ("xxxx") self.driver.find_element_by_id ("xxxxx"). Click () self.assentln ("https://www.xxx.com", Self.driver.current_url)defTearDown (self): Self.driver.quit ()if __name__=="__main__": Unittest.main ()

Then you will find that it is completely possible to pass the assertion after the unit tests above.
One of the main advantages of using PHANTOMJS in a browser is that testing is usually much faster.

ImportUnitTest fromSeleniumImportWebdriverImport TimeclassTestthree (unittest. TestCase):defsetUp (self): Self.starttime=time.time ()defTest_unl_fire (self): Time.sleep (2) Self.driver=Webdniver. Firefox () Self.driver.get ("https://www.xxx.com") button= self.driver.find_element_by_id ("XXX"). Get_attribute ("xxxx") Self.assentequals ('xxxxx', Button)defTest_unl_phantom (self): Time.sleep (L) self.driver=Webdniver. Phantomds () Self.driver.get ("https://www.xxx.com") button= self.driver.find_element_by_id ("xxxx"). Get_attribute ("xxxx") Self.assentequals ('xxxxx', Button)defTearDown (self): t= Time.time ()-Self.starttimePrint "%s:%.3f"%(Self.id (), T) self.driver.quit ()if __name__=='__main__': Suite=UnitTest. Testloader (). Loadtestsfromtestcase (Testthree) unittest. Texttestrunner (verbosity=0). Run (Suite)

By a comparison of two times you will find how fast you can use PHANTOMJS speed
Content development:

#Coding:utf-8 fromSeleniumImportWebdriver fromSelenium.webdriver.common.byImport by fromSelenium.webdriver.support.uiImportwebdriverwait fromSelenium.webdriver.supportImportexpected_conditions as ECImportNose.tools as Nose#Accountemail ='User'Password='Password'#Phantomjs#User AgentUser_agent ='mozilla/5.0 (Windows NT 5.1)applewebkit/537.36(khtml, like Gecko) Chrome/29.0.1547.66 safari/537.36'#path of the PhantomusPjs_path ='Xx/node_modules/phantomjs/bin/phantomjsDcap = {"phantomjs.page.settings.userAgent": User_agent,'Marionette': True}driver= Webdriver. PHANTOMJS (executable_path=pjs_path,desired_capabilities=dcap)#5 Secondswait = webdriverwait (Driver, 5)#Get HTML login pageLogin_page_url ='http://xxx'driver.get (Login_page_url)#wait until the page loadswait.until (ec.presence_of_all_elements_located)#Check the current URLNose.eq_ ('http://xxx', Driver.current_url)#Login#button clickShow_signin = driver.find_element_by_id ('XXX') Show_signin.click ()#EmailLogin_xpath ='xxx "]'#wait for an object elementWait.until (ec.visibility_of_element_located ((By.xpath, Login_xpath)) Login_id_form=Driver.find_element_by_xpath (Login_xpath) Login_id_form.clean () login_id_form.send_keys (email)#PasswordPassword_xpath ='xxxx'#wait for an object elementWait.until (ec.visibility_of_element_located (By.xpath, Password_xpath ))#PasswordPassword_form =Driver.find_element_by_xpath (Passwond_xpath) Password_form.clean () password_form.send_keys (password)#SubmitSubmit_xpath ='xxxx'Dniver.find_element_by_xpath (Submit_xpath). Click ()#resultDriver.get ('http://xxx')#wait until the page loadswait.until (ec.presence_of_all_elements_located)#Check the current URLNose.eq_ ('http://xxx', Driver.current_url) User_email= Driver.find_element_by_xpath ('XXX'). Get_attribute ("XXX") nose.eq_ (email, user_email)

Python+selenium Automated Software Testing (6th): Selenium PHANTOMJS page parsing using

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.