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

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

Related

How to interact with a separate pop-up window/website with Selenium / Chrome Driver in Python?

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

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.

id of xpath is getting changed every time in selenium python 2.7 chrome

one more problem i hv,i asked similar question earlier and i tried that method but not able use that methon in this problem so pls help me. it's element
html code is - Filters 
So basically, question is that there is one button its kind of toggle button and i want click on that button to select device like Desktop, Tablet & Mobile all check boxes are already (default) selected now i have to uncheck or deselect device, to do this, first i have to click on that toggle button , when i click on toggle button its id (gwt-uid-598) 598 is getting changed every time or every refresh. Can you pls help me, what should or which method should i follow in this case.
i am using below python code.
Click on device Filters
elem = driver.find_element_by_xpath('//*[#id="gwt-uid-598"]/div/div/span')
elem.click()
Thanks in advance.
Good question.
Try to use another selector, for example: css class or use xpath method contains().
Example: //div[contains(text(), "checkbox")]
I can help you if you can provide source code of the page or needed element.

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.

Selenium: Testing pop-up windows

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

Categories

Resources