Today we are going to see the basic things which we are going to use while making the selenium WebDriver scripts using python language. So below is some short example which we are going to see the uses of each line statement
The above script can be saved to a file (eg: - python_org_search.py ), then you can use it like this
Python python_org_search.py
The selenium module should already be installed in the python environment you are running.
Example
The selenium.webdriver module provides all WebDriver implementations. Currently supported WebDrivers are: Firefox, Chrome, IE and Remote. ` Keys` class provides support for keyboard keys, such as: RETURN, F1, ALT, etc.
From selenium import webdriver
From selenium.webdriver.common.keys import Keys
Next, create an instance of Firefox WebDriver
driver = webdriver.Firefox()
The driver.get method will open the address filled in the URL, WebDriver will wait until the page is fully loaded (in fact, wait until the "onload" method is executed), then return to execute your script.It's worth noting that if your page is loaded with a lot of Ajax, WebDriver may not know when the page is fully loaded:
driver.get("http://www.google.com")
The next line is to use assert to confirm whether the title contains the word "Python". (Annotation: the assert statement will throw an exception after the statement returns false)
Assert "Python" in driver.title
WebDriver provides a number of ways for you to query elements in a page like: find_element_by_* .For example, the input input box containing the name attribute can be found by the find_element_by_name method. The detailed search method can be viewed in the fourth section element search:
Element = driver.find_element_by_name("address of webelement")
Next, we sent a keyword that works like you type the keyword with the keyboard. Special keys can be entered using the Keys class, which inherits from selenium.webdriver.common.keys . To be safe, we clear any pre-filled text in the input box (eg "Search") to avoid us. Search results are affected:
Element.clear()
Element.send_keys("content text")
Element.send_keys(Keys.RETURN)
If there is any content present in the text box then the first line of code will delete all the content present in the text box.
The second line of code will send or enter the text in the text bow. If we enter the text without clearing it then it will type the text next to the text which is already present in text box.
After submitting the page, you will get all the results. To ensure that certain results are found, use `assert` as follows:
Assert "No results found." not in driver.page_source
Finally, close the browser window, you can also use the quit method instead of the close method, quit will close the entire browser, and _close - will only close a tab, if you only open a tab, most browsers The default behavior is to close the browser:
Driver.close()
This is very basic selenium webdriver script. We are going to see more detailed scripts in coming article tutorials. Do let us know your views in comment section below.
From selenium import webdriver
From selenium.webdriver.common.keys import Keys
driver = webdriver.Firefox ()
driver.get ( "http://www.google.com" )
Assert "Python" in driver.title
Element = driver.find_element_by_name ( " address of webelement " )
Element.clear ()
Element.send_keys ( "content text" )
Element.send_keys ( Keys.RETURN )
Assert "No results found." not in driver.page_source
driver.close ()
The above script can be saved to a file (eg: - python_org_search.py ), then you can use it like this
Python python_org_search.py
The selenium module should already be installed in the python environment you are running.
Example
The selenium.webdriver module provides all WebDriver implementations. Currently supported WebDrivers are: Firefox, Chrome, IE and Remote. ` Keys` class provides support for keyboard keys, such as: RETURN, F1, ALT, etc.
From selenium import webdriver
From selenium.webdriver.common.keys import Keys
Next, create an instance of Firefox WebDriver
driver = webdriver.Firefox()
The driver.get method will open the address filled in the URL, WebDriver will wait until the page is fully loaded (in fact, wait until the "onload" method is executed), then return to execute your script.It's worth noting that if your page is loaded with a lot of Ajax, WebDriver may not know when the page is fully loaded:
driver.get("http://www.google.com")
The next line is to use assert to confirm whether the title contains the word "Python". (Annotation: the assert statement will throw an exception after the statement returns false)
Assert "Python" in driver.title
WebDriver provides a number of ways for you to query elements in a page like: find_element_by_* .For example, the input input box containing the name attribute can be found by the find_element_by_name method. The detailed search method can be viewed in the fourth section element search:
Element = driver.find_element_by_name("address of webelement")
Next, we sent a keyword that works like you type the keyword with the keyboard. Special keys can be entered using the Keys class, which inherits from selenium.webdriver.common.keys . To be safe, we clear any pre-filled text in the input box (eg "Search") to avoid us. Search results are affected:
Element.clear()
Element.send_keys("content text")
Element.send_keys(Keys.RETURN)
If there is any content present in the text box then the first line of code will delete all the content present in the text box.
The second line of code will send or enter the text in the text bow. If we enter the text without clearing it then it will type the text next to the text which is already present in text box.
After submitting the page, you will get all the results. To ensure that certain results are found, use `assert` as follows:
Assert "No results found." not in driver.page_source
Finally, close the browser window, you can also use the quit method instead of the close method, quit will close the entire browser, and _close - will only close a tab, if you only open a tab, most browsers The default behavior is to close the browser:
Driver.close()
This is very basic selenium webdriver script. We are going to see more detailed scripts in coming article tutorials. Do let us know your views in comment section below.
No comments:
Post a Comment