Changes

Jump to navigation Jump to search
2,890 bytes added ,  13:37, 22 December 2017
no edit summary
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===
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===

Navigation menu