1from selenium import webdriver
2from selenium.webdriver.common.keys import Keys
3
4driver = webdriver.Firefox()
5driver.get("http://www.python.org")
6assert "Python" in driver.title
7elem = driver.find_element_by_name("q")
8elem.clear()
9elem.send_keys("pycon")
10elem.send_keys(Keys.RETURN)
11assert "No results found." not in driver.page_source
12driver.close()
13
1import os
2from selenium import webdriver
3from selenium.webdriver.common.keys import Keys
4
5# get the path of ChromeDriverServer
6dir = os.path.dirname(__file__)
7chrome_driver_path = dir + "\chromedriver.exe"
8
9# create a new Chrome session
10driver = webdriver.Chrome(chrome_driver_path)
11driver.implicitly_wait(30)
12driver.maximize_window()
13
14# Navigate to the application home page
15driver.get("http://www.google.com")
16
17# get the search textbox
18search_field = driver.find_element_by_name("q")
19
20# enter search keyword and submit
21search_field.send_keys("Selenium WebDriver Interview questions")
22search_field.submit()
23
24# get the list of elements which are displayed after the search
25# currently on result page using find_elements_by_class_name method
26lists= driver.find_elements_by_class_name("r")
27
28# get the number of elements found
29print ("Found " + str(len(lists)) + " searches:")
30
31# iterate through each element and print the text that is
32# name of the search
33
34i=0
35for listitem in lists:
36 print (listitem.get_attribute("innerHTML"))
37 i=i+1
38 if(i>10):
39 break
40
41# close the browser window
42driver.quit()
43