Selenium Alert behavior - selenium

Which of the following statements is TRUE when an alert is open in a browser? (Assume that
driver is a WebDriver object)
I. driver.findElements throws an exception
II. driver.close throws an exception
III. driver.close closes the current window.
IV. driver.findElements returns an empty list.
☐ (a) I, II.
☐ (b) I, III.
☐ (c) II, III.
☐ (d) III, IV.
My answer would be D(III and IV) as I know driver.findElements doesn't throws the exception and only returns the empty list when it doesn't able to find an element(https://www.softwaretestingmaterial.com/difference-between-findelement-and-findelements-methods/#:~:text=findElements%20method%20returns%20the%20list,t%20exist%20on%20the%20page.)
Your thoughts please. i found the answer as below
Answer B is correct
• In this case driver.findElements method would throw an exception, however
driver.close would close the window as expected. can someone explain this for me
driver.findElements
driver.close

Albeit Option D (III and IV) is the correct answer as:
close() closes the current window, quitting the browser if it's the last window currently open.
findElements() find all elements within the current page using the given By mechanism. This method will return as soon as there are more than 0 items in the found collection, or will return an empty list if the timeout is reached.

Related

Selenium Firefox driver window isn't closed when an exception raises

So lets assume I have some function using FirefoxDriver:
def init_firefox():
driver = webdriver.Firefox()
...
And during performing this function there might be an exception raised by some reason. I catch it by this way:
try:
init_firefox()
except Exception:
print('Exception raised!')
The issue I've encountered is that when init_firefox really raises an exception, Firefox window isn't closed and persists in RAM. Is there a way to close this single window without closing other ones?
Yes there is a way to close the window you want, The only thing you need in this case is for the driver to be in that specific window to be closed !!
This is a bit explanation on how to do so!!
I tested this code with chromedriver i already have in my machine
# x refers to the window you want to switch to x = 1 or 2 ..
window_to_close = driver.window_handles[x]
driver.switch_to.window(window_to_close)
driver.close()

Unable to switch to frame in loop in selenium

`I am trying to switch to particular frame using selenium python in loop. It gets executed the first time but after that gives timeout exception error. Here is the code:
Navigate.py:
def navigate_to_frame():
driver.switch_to.default_content()
//Navigation to the frame using execute script
action.py
def perform():
driver.switch_to.default_content()
driver.switch_to.frame(frame)
//Perform required actions
Start.py
for i in range(0,5):
navigate_to_frame()
perform()
The above code works for first time but in the second iteration it throws an exception
I found the solution somewhere else. Came back to answer it.
You are still in the first frame from your first iteration when you try to select from the second iteration.
You need to leave the frame it at the end of the first loop to reset where you are looking at. For example with. a "select frame relative=top".

How to get Exception source “Activity Description name”

When exceptions occur in a UIPath project I have an email that is sent out with the exception info included. There seems to be an issue though where I can only see where the error occured by looking at the selector information such as:
Cannot find the UI element corresponding to this selector:
<html app='chrome.exe' title='Microsoft Dynamics GP' />
<webctrl aaname='Add' idx='1'
parentid='a00000000000000008549000000030009000000000001000000000000' tag='DIV' />
This info and the stack trace or any other info is not really helpful for quickly finding the source of the problem. I have looked through the UIPath documentation and forum and found only the this question, which seemed to point to using the exception.Source to show the name of the activity where the error occurred. exception.Source only returns “UiPath.Core.Activities” though instead of "Type into Copy Job# 'INPUT'" in the following example:
This obviously causes a big problem with exception handling. How can I easily return the source with each exception?
When your selector fails, you will end up with a new object of type UiPath.Core.SelectorNotFoundException. However, until the team at UiPath decides to add the Display Name into the inner exception, there is little you can do in this particular case.
Take the following example - the first line shows the Inner Exception, and the second one in red is essentially just the exception being rethrown. Note that only the latter one contains the Display Name property.
The Source itself will usually be of type UiPath.Core.Activities, but since this is just the type's name, we don't have any link to the faulting object. Here's what you can do:
Add some details to your exception. You don't want to do this for each activity, but you could have certain blocks of try-catches (example: logging into the system consists of three individual activites, and they reside in one block).
Rethrow the exception. That way the Display Name will end up in the execution log file.

Will the Implicit wait will move on if findElement action is complete?

Implicit Wait : If wait is set, it will wait for specified amount of time for each findElement/findElements call. It will throw an exception if action is not complete.
Assume we set implicit wait to 10 secs. My question is will selenium move on to next step if findElement action is complete before 10 secs?
Yes. Setting implicit wait causes the driver object to wait for the set time if the element it is looking for is not found immediately. The driver object keeps polling the DOM every 500 milliseconds until it finds the element or the time-out expires.
This is the explanation from official Selenium documentation page:
An implicit wait is to tell WebDriver to poll the DOM for a certain
amount of time when trying to find an element or elements if they are
not immediately available. The default setting is 0. Once set, the
implicit wait is set for the life of the WebDriver object instance.
So, to answer your question in short, yes it continues with executing next steps as soon as it finds the element(s) it is looking for. You may also understand that to be the case from a simple experiment like #sircapsalot has shown in his answer.
Answer:
Yes. It will continue with the next step if it finds the element before the implicit timeout is hit.
Proof of concept:
#Test
public void test29800926() {
driver.get("http://ddavison.io/tests/getting-started-with-selenium.htm");
driver.manage().timeouts().implicitlyWait(30000, TimeUnit.MILLISECONDS);
System.out.println(driver.findElement(By.id("click")).getText());
}
Instead of waiting the total of 30 seconds that I set the implicit wait to (30000ms / 1000 = 30sec), it found it immediately and continued to print the text of the element.

Selenium SelectWindow Command not working

Selenium Select Window command fails and shows "Could not find window with title....". but if i Execute the Select Window command alone it passes the case and verifying the elements.
Code i used:
public void testDefaultlogo() throws Exception {
selenium.open("http://Sitename/samp.aspx");
selenium.type("ctl00_mainContentPlaceHolder_txt_LoginName", "uname");
selenium.type("ctl00_mainContentPlaceHolder_txt_Password", "pwd#12");
selenium.click("ctl00_mainContentPlaceHolder_btn_login");
selenium.waitForPageToLoad("60000");
selenium.click("ctl00_defaultLogo");
selenium.selectWindow("Sample~Window-ID");
verifyEquals("http://Sitename/index.html", selenium.getLocation());
selenium.close();
selenium.selectWindow ("null");
verifyTrue(selenium.isElementPresent("ctl00_defaultLogo"));
I mean by clicking one by one of the follwing commands in Selenium IDE it shows green but if i run the case it failed and shows as i mentioned above
What I can get from your code is that, clicking the "ctl00_defaultLogo" will popup another window. The possible issue could be that after write the command selenium.click("ctl00_defaultLogo") you need to wait for the popup to load.
The possible resolution, insert the command
selenium.WaitForPopUp("Sample~Window-ID", "5000");
before the
selenium.selectWindow("Sample~Window-ID");
This will cause selenium to wait for 5 secs before selecting the popup window.
Hope this helps!!!
some times it may not work for that u better use.
open_window("URL","WindowID or title or name");
selenium.selectWindow("WindowID or title or name");