Browser This big frame we can control, just a small page element and can nai us!!! Previous "Web Automation Test-code Two" all the way to find elements have described many methods of acquiring elements, on the basis of which we recognize several elements of the most common related operations? (ˉ"ˉ?)
|
Java |
Python |
Ruby |
Input content |
SendKeys (*value) |
Send_keys (*value) |
Send_keys (*value) |
Click Element |
Click () |
Click () |
Click |
Clear text |
Clear () |
Clear () |
Clear |
Submit Form |
Submit () |
Submit () |
Submit |
Gets the text of the element |
GetText () |
Text |
Text |
Gets the specified property value of the element |
GetAttribute (String attributename) |
Get_attribute (String attributename) |
Attribute (String attributename) |
* * Code Time * *
(。 ﹏。 *) In fact, "get the text of the element" This method I want to get the text of the Baidu input box, but do not know why although the code does not error but always get the text (because it is the input text box problem?) ╮(╯▽╰)╭). So I thought, since we have the "get the specified attribute value of the element" method, then we get its value property values directly < ( ̄︶ ̄) [go!]
Java
1 Packagejavatest;2 3 Importorg.openqa.selenium.By;4 ImportOrg.openqa.selenium.WebDriver;5 ImportOrg.openqa.selenium.firefox.FirefoxDriver;6 7 Public classTest {8 Public Static voidMain (string[] arg)throwsinterruptedexception9 {TenWebdriver Driver =Newfirefoxdriver (); OneDriver.get ("http://www.baidu.com/"); A -Driver.findelement (By.id ("kw")). SendKeys ("Java");//assign value to Baidu input box -Driver.findelement (By.id ("kw")). Submit ();//Submit to Baidu input box theSystem.out.println (Driver.findelement (by.id ("kw")). GetAttribute ("value"));//get Baidu Input Box value -Thread.Sleep (2000); -Driver.findelement (By.id ("kw")). Clear ();//clear Baidu Input Box -Thread.Sleep (2000); +Driver.findelement (By.id ("su")). Click ();//Click on the Baidu Search button -Thread.Sleep (2000); +System.out.println (Driver.findelement (By.id ("CP")). GetText ());//display Baidu record information text A atDriver.close ();//End - } -}
Python
1 fromTimeImport*2 fromSeleniumImportWebdriver3 fromSelenium.webdriver.common.byImport by4 5 #start the Firefox browser6Driver =Webdriver. Firefox ()7Driver.get ('http://www.baidu.com')8 9Driver.find_element (By.xpath,"//*[@class = ' s_ipt ')"). Send_keys ('Python')#assign value to Baidu input boxTenDriver.find_element_by_xpath ("//input[@type = ' Submit ']"). Submit ()#Submit to Baidu input box One Print(Driver.find_element (By.id,'kw'). Get_attribute ('value'))#get Baidu Input Box value ASleep (2)#Sleep 2 seconds -Driver.find_element (By.id,'kw'). Clear ()#clear Baidu Input Box -Sleep (2) theDRIVER.FIND_ELEMENT_BY_ID ('su'). Click ()#Click on the Baidu Search button -Sleep (2) - Print(Driver.find_element (By.id,'CP'). Text)#display Baidu record information text - +Driver.close ()#End
Ruby
1 classBaidu2Require'RubyGems'3Require'Selenium-webdriver'4 5 #Open Firefox and enter the URL6Driver = Selenium::webdriver. for: Firefox7Driver.navigate.to"http://www.baidu.com"8 9Driver.find_element (: Name,'WD'). Send_keys ('Ruby')#assign value to Baidu input boxTenDriver.find_element (: ID,'kw'). Submit#Baidu input Box submission OnePuts Driver.find_element (: ID,'kw'). Attribute ('value')#get Baidu Input Box value ASleep (2)#Sleep 2 seconds -Driver.find_element (: XPath,"//*[@class = ' s_ipt ')"). Clear#clear Baidu Input Box -Sleep (2) theDriver.find_element (: ID,'su'). Click#Click on the Baidu Search button -Sleep (2) -Puts Driver.find_element (: ID,'CP'). Text - +Driver.close#Exit Program -End
"Web Automation Test-code Four" common methods-common element operations