Is it possible to save current state of Selenium browser?
To understand I will provide an example:
Let's say that there is a web page. I clicked on the button where I found many other buttons. I want to check each of those buttons sequentially. The problem is that each of this buttons needs to obtain browsers information like referrer which should be the first page after first click etc.
In this case I would need to have those information saved in a browser because if I clicked on the second button the referrer would be page which was recently opened. I can't click on a third right after that. I would have to go back, but some web pages does not allow browsers 'back'. Another advantage is that I would not have to send a new request to the server.
Something like this:
for but in driver.find_elements_by_class_name('button'):
state = driver.save_state()
but.click()
# do stuff
driver.load_state()
There is another solution. The web browser usually uses the cache to save the state of it. Therefore, you can save the cache before clicking each button then reuse the cache to get back to before clicking state.
I already use that solution in my project. It will work perfectly if nothing is saved into database or another third party.
Related
I am trying to automate a process with Selenium, and am having troubles figuring out how to switch between open windows while the program is running.
After clicking on the button, it opens another website that has a separate url, which is unique each time it is opened. I need to switch Selenium from interacting with the original website to this new popup within the browser, caused by the original website. The new window shows that it is also controlled by Chromedriver with the bit at the top that says "Chrome is being controlled by automated test software." Additionally, the actual website opened will be the same, just the fine print after the '.com/' is different.
How would I go about doing this? Also, how would I switch back? (If this is even possible)
For example:
driver=webdriver.Chrome(service=s)
driver.get("https://originalwebsite.com/")
driver.find_element(By.XPATH, 'buttons-xpath').click()
# (popup opens up now)
# *switch to popup website here*
driver.find_element(By.XPATH, 'button-on-new-website-xpath').click()
driver.find_element(By.XPATH, 'second-button-on-new-website-xpath').click()
# *popup website closes*
# switch back to original website / window
Thanks!
I have tried to use driver.navigate in a variety of ways but generally have no clue what I am doing. Thanks again!
The comment from ALex Break led to the answer.
All I had to do was:
handles = driver.window_handles
driver.switch_to.window(handles[x])
#handles[x] is the index of the list handles that has the handle I want to switch
#to stored in it
I access a few websites to download client data every 30 minutes. Selenium (python) helps me automatically fill out all the sign-in information and control all the subsequent selection/clicking steps. With this set-up, I don't ever need to manually click anything. However, with one particular page https://services.ehousingplus.com/Default.htm, I can't seem to fill out the 'User Name'/'Password' nor can I click the Login button. If fact, I can't use the browser.find_element_by_xpath() to control any elements on the webpage at all. Not sure if it uses any special technology. Please help.
I use code:
browser = webdriver.Firefox()
browser.get("https://services.ehousingplus.com/Default.htm")
browser.find_element_by_css_selector('#igtxtmyAuthentication_utxeUserName')
There is a notice on the webpage saying it supports only IE 8/9. But I think that restriction is only on the subsequent steps after logging in.
The elements are inside of a frame. Selenium can only access elements in the current frame. In order to access the login elements on that page, you need to switch to the frame that contains the elements.
You'll need to use:
browser.get("https://services.ehousingplus.com/Default.htm")
browser.switch_to_frame("HDSApplications")
browser.find_element_by_css_selector('#igtxtmyAuthentication_utxeUserName')
In order to move back to the top frame, you'll need to use:
browser.switch_to_default_content()
I want to create a webpage. In it there is a go_to button named goto_button. Whenever the user presses this button the new webpage will be called and the old one will be destroyed. To do this, I have to put a button on the web page, in the html file. However how can I switch from one web page to another? The go_to webpage address is www.kleol.com
if button is pressed
go to web-page kleol // how can I do that?
NOTE: I am new to Django.
Simple HTML tag like the following will work:
Go to Kleol
There's nothing that is django related.It is simple html.
I have an issue when trying to test a web application with Selenium/Python. Basically I can't test elements of a pop-up window.
A scenario: I can test all elements for a page. But when I go to click on a button that opens up a small pop up box I can't test the elements on the popup. It's like the pop up isn't in focus or active.
I can test elements on the next page. For example click a button, brings me on to next page, and I can work with elements on the 'next' page. So it the problem seems to be popup specific.
I could post code but to be honest it might confuse at this stage. I may post code in a later post, thanks
There is a property called switch_to
Q: How do I handle pop up windows?
A: WebDriver offers the ability to cope with multiple windows. This is done by using the WebDriver.switch_to.window(knownName) method to switch to a window with a known name.
If the name is not known, you can use WebDriver.window_handles to obtain a list of known windows.
You may pass the handle to switch_to.window(handleName)
For example I used driverName.switchTo.window(driverName.getWindowHandle()) to get a hold of popups for which I didn't want to look for names.
Additional references:
http://code.google.com/p/selenium/wiki/FrequentlyAskedQuestions
For the Selenium RC API, you need to use the SelectWindow command to switch to the pop-up window. The window can be specified either by its name (as specified on the JavaScript window.open() function) or its title. To switch back to the main window, use SelectWindow(None).
I have written a script that goes through a bunch of files and snips out a portion of the files for further processing. The script creates a new directory and creates new files for each snip that is taken out. I have to now evaluate each of the files that were created to see if it is what I needed. The script also creates an html index file with links to each of the snips. So I can click the hyperlink to see the file, make a note in a spreadsheet to indicate if the file is correct or not and then use the back button in the browser to take me back to the index list.
I was sitting here wondering if I could somehow create a delete button in the browser next to the hyperlink. My thought is I would click the hyperlink, make a judgment about the file and if it is not one I want to keep then when I get back to the main page I just press the delete button and it is gone from the directory.
Does anyone have any idea if this is possible. I am writing this in python but clearly the issue is is there a way to create an htm file with a delete button-I would just use Python to write the commands for the deletion button.
You could make this even simpler by making it all happen in one main page. Instead of having a list of hyperlinks, just have the main page have one frame that loads one of the autocreated pages in it. Put a couple of buttons at the bottom - a "Keep this page" and a "Delete this page." When you click either button, the main page refreshes, this time with the next autocreated page in the frame.
You could make this as a cgi script in your favorite scripting language. You can't just do this in html because an html page only does stuff client-side, and you can only delete files server-side. You will probably need as cgi args the page to show in the frame, and the last page you viewed if the button click was a "delete".
You would have to write the web page in Python. There are many Python web frameworks out there (e.g. Django) that are easy to work with. You could convert your entire scripting framework to a web application that has a worker thread going and crawling through html pages, saving them to a particular location, indexing them for you to see and providing a delete button that calls the system's delete function on the particular file.
Rather than having your script output static HTML files, with a little amount of work you could probably adapt your script to run as a small web application with the help of something like web.py.
You would start your script and point a browser at http://localhost:8080, for instance. The web browser would be your user interface.
To achieve the 'delete' functionality, all you need to do is write some Python that gets executed when a form is submitted to actually perform the deletion.
Well I finally found an answer that achieved what I wanted-I did not want to learn a new language-Python is hard enough given my lack or experience
def OnDelete(self, event):
assert self.current, "invalid delete operation"
try:
os.remove(os.path.join(self.cwd, self.current))