Selenium: Testing pop-up windows - python

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).

Related

Clicking opens a new link

selenium/chromedriver
When clicking a button, a new tab is opened when I do it through a GUI browser.
Python Selenium seems to have no problem clicking the button, as it gives me no errors. The errors come in the next step, when I need to find an element in the clicked page. I had selenium take a screenshot and it still shows the first page.
Presumably it clicked the button, created a new tab, and didn't switch over?
How do I switch to the new tab, or even verify the new tab exists in the first place?
Thank you!
When you open a browser with selenium, it stores a handle for each tab/window it is controlling in a list called window_handles, for example:
from selenium import webdriver
driver = webdriver.Firefox()
print(driver.window_handles)
...should give you something like ['6b7af9bb-f299-462e-a79a-2b8fda63f388']
When you open a new tab, a new handle for that window/tab should be added to that list. To then switch to the tab you want, use driver.switch_to.window(), for example (continuing above example):
driver.switch_to.window(driver.window_handles[1])
Note: you could also use driver.switch_to_window, but this is deprecated in favor of the above example.
Also, just a tip for debugging, it can be helpful to use the python repl so you can follow what the browser is doing in real time.

How can I click (bypass) a submit button using Python Selenium, when the button gets disabled after using .clear() or .send_keys()?

As the title says, how can I .click() a button using Selenium, when the button gets "disabled" after using the method clear or send_keys?
Before:
That's the page status when I open it's url... but then right after I run my code to find the textbox and replace it's value, the element gets disabled (maybe by some sort of JS) right after I clear it's content or write something to it using send_keys.
After:
Code:
txt_value = driver.find_element_by_xpath('//input[#id="txtValor4"]')
txt_value.clear() #this disables the button
txt_value.send_keys(str(123,45)) #this also disables the button
My question is:
How can I bypass this website protection and press the Continuar button?
I thought about disabling JS, but the whole website relies on it to produces the requires documents.. wrong alternative.
So I thought about using the button properties to simulate the pressing of the button... just don't know if it's possible, or how I could do this.
Another option was blocking only the JS that disables the button maybe mapping where the command comes from using the inspect element and network tools...
So is there any way to achieve this?
ps.: I can't give the URL because it requires my login data.
Ok, so you can't directly do this through normal means. Selenium WebDriver is made to simulate real use of a browser. It may be possible however to use the execute_script function. (Java Selenium has a built in JavascriptExecutor class, I assume this is python's version.) The execute_script function allows Selenium to perform tasks that a human interacting with a browser can't do.
driver.execute_script("document.getElementById('buttonid').click()")
Or something along those lines should work. Hope that helps you out.
If you don't get any solution with selenium and javascript, you can use Sikuli concept. To click that element, take the image of the 'Continuar' button and save it in resources folder.
String elementImg=Path of the Image;
screen.click(elementImg);
I could bypass this using driver.execute_script("document.getElementById('txtValor4').‌​value = 123.45"), to pass the values into the textbox, so the button didn't got disabled and I could press the Continue button.
Even bypassing this, the result wasn't the expected! The value that I entered was supposed to be corrected by some sort of interest. But bypassing this, the value isn't corrected.
Today the user that asked the program told me that everytime I change the value inside this textbox, I must press the Calculate button.
So, instead of inefficiently bypassing this disable method, I could solve my problem using:
b = driver.find_element_by_xpath('//input[#id="txtValor4"]')
b.clear()
b.send_keys('123.45')
driver.find_element_by_xpath('//input[#id="btnCalcular4"]').click()
driver.find_element_by_xpath('//input[#id="btnContinuar4"]').click()
This way the tax value is corrected by interest and the website generate the .pdf with the exact value that I was expecting.
Many thanks for everyone that put some time and effort trying to help me.

Unable to highlight text after selenium webdriver click()

I am using python code such as below to click an element within an iframe on an angularjs page.
browser = webdriver.Ie()
browser.switch_to.frame('name')
browser.find_element_by_id('value').click()
After using click() I am unable to manually highlight text that appears on the page. Why might this happen? How can I restore the ability to highlight text with the mouse?
I have tried switching back to default content, but this makes no difference. Any ideas?
Other strange effects after using click(): Links and buttons don't work while using the mouse manually unless double-clicked. These would normally require single click. It is as if there is an invisible overlay blocking text selection or clicking links. Radio buttons and menus still work.
Edit: The site uses silverlight and I am wondering if this is related to the problem that results from using click().

Python, Selenium Webdriver: The element is not visible for Firefox.

I'm trying to iteract with a webelement on a page that has edit link that opens a popup. In opened popup I have simple input field and Apply/Cancel buttons. In my script I do the following to enter some text to the input field:
def enter_text(self, text, action):
if self.is_element_present(self._input_locator):
self.selenium.find_element(*self._input_locator).send_keys(text)
if action == 'Apply':
self.selenium.find_element(*self._apply_button_locator).click()
elif action == 'Cancel':
self.selenium.find_element(*self._cancel_button_locator).click()
When I run my script in Chrome - everything works fine, all webelements are found and input text is entered to the field. But when I run exact same script in Firefox - it opens popup window (which means it became visible for Webdriver) with input field and 2 buttons but the text is not getting entered to the field which causes error:
ElementNotVisibleException: Message: u'Element is not currently visible and so may not be interacted with'
Why can this happen if the popup is actually opened (and I can see it) but Webdriver tells that it is not visible? Also, I put several sleeps just to get sure that popup loaded and then the text is entered but it did not help.
Any help will be appreciated.
Two ideas:
1: make sure your HTML is valid, eg by running it thru the w3c validator. broken html is a common cause of different behaviours in different browsers.
2: have you definitely switched selenium over to the popup, using, eg:
for handle in self.selenium.window_handles:
# identify popup window's handle somehow
self.selenium.switch_to_window(handle)
As an aside, if you haven't come across selenium.implicitly_wait(3), it's dead useful as a way of avoiding time.sleeps and wait-for constructs..
Witch Firefox version and Webdriver version are you using?
Please try the following:
Update Webdriver to the last version
If you are using firefox 17-18, downgrade it to lower version (I guess FF12 will work)
Please try that and tell me what happens,
Regards

How to control IE Explorer with pywinauto

Ok, so I want to control a IE Explorer with pywinauto. I would like to select text fields on the page and edit them. Is there a way to do this with pywinauto without clicking where the text field are? The pages will be the same every time, but not the data that is entered into them. The window may not be in the same place every time, so I couldn't do WrapperObject.Click(coords=fixed_pos). Sample code is preferred. Thanks!
Unfortunately no. You can control the window of IE, all the toolbars and buttons, address bar and search bar, etc. But not content of pages within.
For this task, you need other tools, such as Selenium http://seleniumhq.org/
As for pywinauto,I strongly recommend to use the utility SWAPY http://code.google.com/p/swapy/. I am the Author of SWAPY. With it you can easily determine whether it is possible to manage the a control with pywinauto or not.

Categories

Resources