Changes

Jump to navigation Jump to search
4,379 bytes added ,  13:47, 21 September 2020
no edit summary
{{Project|Has project output=How-to|Has sponsor=McNair ProjectsCenter|Has Image=selenium.jpg
|Has title=Selenium Documentation
|Has owner=Peter Jalbert,
For example, the following could be used to scroll to the bottom of the page:
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
 
===Clicking===
If an element is clickable (such as a link or a button), you can click on that element by doing:
element.click()
 
This will route you to the linked page, or execute the action of the button. The driver will now be on the new page, and commands will deal with elements on the new page.
 
===Web Elements===
This area contains methods that can be called on a web element.
 
element.get_attribute(attr)
 
This function gets an attribute of the web element by using the attribute identifier. For example, element.get_attribute("href") would get the URL of a button or link.
 
===Open a New Window===
OPTION 1: Cntrl + Click
-----------------------
 
This method will utilize Action Chains and Keys. Action Chains simply queue commands, and the string of commands are executed once the perform() method is called. First, import these dependencies:
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
 
Next, select the element using one of the selectors listed above. We will assume this web element is stored in the variable called element. Then, add a key down command to Action Chains using the Shift Key to simulate the shift button being pressed down:
ActionChains(driver).key_down(Keys.SHIFT).perform()
 
The Shift button is now "pressed" down. A click will now be a Shift + Click.
element.click()
 
This will simulate a Shift + Click on a link. A new window should open with the new link.
Execute a key up command with Action Chains on the Shift key so your future commands are not combined with shift:
ActionChains(driver).key_up(Keys.SHIFT).perform()
 
Now the browser is displaying a new window, but the driver thinks it is still on the previous window. To change this, access the window handlers:
handles = driver.window_handles
 
And switch the driver's "focus" to the new window (the last window in this list of windows is the newest one):
driver.switch_to_window(handles[-1])
 
Now you can execute commands on the new window! To exit this window, use:
driver.close()
 
When closing a window, you will also need to return the driver to the previous handler by repeating the code:
handles = driver.window_handles
driver.switch_to_window(handles[-1])
 
 
OPTION 2: Open Blank Window + get()
-----------------------------------
 
This method finds URLs that you wish to traverse, and opens up a new window for each URL you would like to visit. It utlizies Javascript injection and the GET method.
 
PROS: This method allows you to check a URL string for correctness before visiting the page. This can save you trouble from TimeOut Errors if someone has linked a rotten link on their webpage.
 
This assumes you have a URL retrieved by using element.get_attribute("href") or some other method. We assume this is stored in the variable url.
 
First, open a new window by injecting javascript:
driver.execute_script("window.open('');")
 
Now the browser is displaying a new window, but the driver thinks it is still on the previous window. To change this, access the window handlers:
handles = driver.window_handles
 
And switch the driver's "focus" to the new window (the last window in this list of windows is the newest one):
driver.switch_to_window(handles[-1])
 
Now, you can execute commands on the new window. To get to the webpage you want to visit:
driver.get(url)
 
Now you can interact with anything on the new page using the current driver. To exit this window, use:
driver.close()
 
When closing a window, you will also need to return the driver to the previous handler by repeating the code:
handles = driver.window_handles
driver.switch_to_window(handles[-1])
 
 
===Downloading Files===
In general, I recommend using Selenium to do browsing, and use another method to download the file.
driver.current_url
 
This will get the current url. From there, standard libraries such as wget or urlretrieve can be used to download the file if the url ends in .pdf, or the html of the page if it is a regular webpage.
 
If you are trying to retrieve a body of text, find the element using selectors. Then:
element.text
 
will retrieve the text in that element as a string. This can then be written to a file in any way you see fit.
 
[http://stackabuse.com/download-files-with-python/ Helpful link for downloading files in python]
 

Navigation menu