if statement selenium python webdriver - python

I want to check to see if a certain window is open and if it is than to proceed to log out however if it is not than to open up the widget and then logout out.
The widget is inside of another frame. I don't want to have to switch frames just to see if the widget is open if I do not have to.
My Code:
window=driver.find_element_by_id("DR44")
if window.is_displayed():
userdropdown=driver.find_element_by_id("Menu").click()
logout=driver.find_element_by_id("df456").click()
else:
LaunchMenu=driver.find_element_by_id("launch").click()
bvWidget=driver.find_element_by_id("54353sfd").click()
launch= driver.find_element_by_id("3rfs").click()
userdropdown=driver.find_element_by_id("userMdfd243l").click()
logout=driver.find_element_by_id("efdf343").click()
My error: Unable to locate element
I want to check to see if the title of the widget is found on the page if so then proceed to log out and if not open up the widget and then log out.

You can not interact any element inside an iframe if you dont switch to iframe. First switch to iframe by driver.switch_to_frame() and you also dont need to assign an element to variable to click to. see below:
driver.switch_to_frame(iframe)
if driver.find_element_by_id("DR44").is_displayed():
driver.find_element_by_id("Menu").click()
driver.find_element_by_id("df456").click()
else:
driver.find_element_by_id("launch").click()
driver.find_element_by_id("54353sfd").click()
driver.find_element_by_id("3rfs").click()
driver.find_element_by_id("userMdfd243l").click()
driver.find_element_by_id("efdf343").click()
To be noted: iframe is a list of iframe ids

Related

Selenium Python Error: "stale element reference: element is not attached to the page document"

HTML Image
I'm using selenium to get some information from a site, but the links that I need to click on require double clicking. Here's the code (I've included an attachment of the image of the HTML)
PATH = "C:\Program Files (x86)\msedgedriver.exe"
driver = webdriver.Edge(PATH)
action = ActionChains(driver)
from selenium import webdriver
from selenium.webdriver import ActionChains
frame = search_by_XPATH('//[#id="frmMain"]/table/tbody/tr/td[2]/div[2]').get_attribute('id').replace("_Div","_Frame")
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.ID, frame)))
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.ID,"fraTree_Frame")))
rows = driver.find_elements(By.XPATH,'//*[#id="ctl00_treChart_RadTreeView"]/ul/li/div/span[2]')
for row in rows:
# row.click()
action.double_click(row).perform()
sleep(1)
I'm accessing different pages to get the links, and although each page has the same content the "id" of the frame changes with each new page I access. So that's why I use the 'get_attribute' function to get the id of the frame of the page.
I'm using action chains right now to perform the double click, but every time I do I get the stale element reference: element is not attached to the page document error. I know it's not an issue with the XPATH I'm using to access the element because I can perform a element.click() function on it just fine and the element receives the click. Nothing changes after the first click except the element gets highlighted. The element's xpath doesn't change and its still on the page. I also tried doing the action.click().perform() on the element but that produces the same error.
I've tried clicking on other parts of the element that could receive the click (the div, the span2,etc) but I always get the error. The 'span2' is the part that holds the text of the link (although its not an 'a' tag so its not really a link in that sense.)
I don't understand why the element would be able to receive the click for the .click() function but then be unable to receive the double click. I'm using the double_click() function on another element previously in my code and that works as expected.

Selenium Python - dealing with checkboxes is_selected()

I have simple python code presented on the picture.
The thing is that eula checkbox is making me problems.
I am trying not to check it if it is not selected and to ignore checking if it is.
But, if I don't use eula variable on line 41 I get an error: stale element reference: element is not attached to the page document.
If I uncomment this line and use it, than this if block doesn't work and my checkbox gets unchecked.
python code
html structure
If your checkbox checked="checked".So you need to check first whether selected or not.If selected do not click.Try this code.
element=driver.find_element_by_id("eulaAccepted")
if element.is_selected():
print('Check box is already selected')
else:
element.click()

Selenium click on next button

How can i get selenium to click on the next button from the HTML code below?
<span class="action-btn" data-bind="visible: viewModel.page() < viewModel.pages(), click: viewModel.changePage(+1);">></span>
Initially I wrote:
elm = driver.find_element_by_class_name('action-btn')
elm.click()
time.sleep(4)
But I noticed that in the website, there are other buttons with similar names as well. Such as action-btn customize and action-btn right
With my current code, its basically just clicking on action-btn customize and my guess is because this button name comes before the code that I intended to click on.
How should I be writing my code instead?
Update
Here is a screen shot of the frame. The yellow highlight is what I am trying to click on.
Looking at your screenshot it seems the action-btn you want to click is the last one in pager class.
And, if you have only one div element with class pager, you could select all action-btn inside it and get the last one:
elem = driver.find_element_by_css_selector("div.pager span.action-btn:last-of-type");
Here are more details about :last-of-type selector:
The :last-of-type CSS pseudo-class represents the last sibling with the given element name in the list of children of its parent element.

Python-Selenium-How to find out if it's a window or iframe or popup

I am trying to automate some test case using Python Selenium Webdriver. I click on a button which opens up a new window/iframe/popup. Which needs some data to be be added in text box and choose some from the drop box. I have two questions,
How would I find if its a window/iframe/popup from the firepath in Mozilla?
How to get the handle?
This is the xpath and content I see in firepath when I select the new window/iframe/popup(image attached),
html/body/div[11]
I have the code here,
zenoss_url = "http://xxxxx/"
xpath_uname = "html/body/form/div/div/input[1]"
xpath_pwd = "html/body/form/div/div/input[2]"
xpath_submitButton = "html/body/form/div/div/input[3]"
xpath_eventsTab = "html/body/div[7]/div/div/div/div[2]/ul/li[2]/div/div/a"
driver = webdriver.Firefox()
driver.get(zenoss_url)
handle = driver.find_element_by_xpath(xpath_uname)
handle.send_keys(zenoss_uname)
handle = driver.find_element_by_xpath(xpath_pwd)
handle.send_keys(zenoss_pwd)
driver.find_element_by_xpath(xpath_submitButton).submit()
driver.implicitly_wait(10)
driver.find_element_by_xpath("html/body/div[7]/div/div/div/div[2]/ul/li[2]/div/div/a").click()
driver.find_element_by_xpath("html/body/div[3]/div/div/div/div[1]/div/div/div[1]/div/div/div[7]/em/button").click()
driver.implicitly_wait(5)
window = driver.switch_to_window(?????)
I am not sure what "name" i should replace with ?????
Appreciate any help.
Your path of html/body/div[11] is only the xpath from the scope of the frame. When navigating to frames and subframes there are a few approaches you can take; from looking for the index of the frame to looking for the name of the frame. If you use firepath to inspect an element inside the frame, it will only give you the xpath for the element the scope of that frame. Frames work like boxes inside one another; in order to get to the second box, you have to open the first box and look inside.
//In C#
driver.switchTo().Frame("FirstFrameFound");
//To close the box again and get back to the overall main box.
driver.switchTo().defaultContent();
If your 2nd frame is a child of the first frame, you have to dive into the first, and then switch to the 2nd to interact with elements inside.
//In C#
//Switch to the first frame
driver.switchTo().Frame("FirstFrameFound");
//Switch to the second frame
driver.SwitchTo().Frame("SecondFrame");
//Now look for your element that you wish to interact with.
driver.FindElement(By.XPath("//html/body/div[11]"));
//To close the box again and get back to the overall main box.
driver.switchTo().defaultContent();
Please look into this post here for more information.
How to navigate a subframe inside a frameset using Selenium WebDriver?
If you are switching to a window there are multiple approaches as well. My preferred approach is this method:
//When dealing with new window and the parent.
//Switch to the last window that opened
driver.SwitchTo().Window(driver.WindowHandles.Last());
//Perform your actions
driver.Close();
//Switch back to the first window
driver.SwitchTo().Window(driver.WindowHandles.First());

How to find xpath of an element in a window that's inside another "frame window"

I'm trying to find the xpath of an element located in a window that's opened in another window for use in a python script for selenium.
The page is essentially a frame which hosts another page inside (think browser within a browser window).
I can find the xpath of the window itself that's inside the outside browser window, but I can't seem to use xpath to select any of the elements on the inner window.
This is the overall xpath to select the element I want.
/html/body/div/div[2]/table/tbody/tr/td[2]/form/div/div/div/div/div/table[2]/tbody/tr/td/div/div/div/div/div[2]/div/span/iframe/html/body/table/tbody/tr/td/div[3]/div[2]/div[2]/table/tbody/tr/td[2]/div/a[1]
The xpath to select the inner window is:
/html/body/div/div[2]/table/tbody/tr/td[2]/form/div/div/div/div/div/table[2]/tbody/tr/td/div/div/div/div/div[2]/div/span/iframe
The xpath to select the item inside the inner window (if directly on inner window's page) is:
/html/body/table/tbody/tr/td/div[3]/div[2]/div[2]/table/tbody/tr/td[2]/div/a[1]
I've tried using all these methods, but haven't been able to select elements on the inner page.
Here is the Python code:
survivorFrame = driver.find_elements_by_xpath(".//*[#id='theSurvivorIframe']")[0]
driver.switch_to_frame(survivorFrame)
elem1 = driver.find_elements_by_xpath(".//*[#id='lea1_ileinner']/a[1]")
survivorOwner = re.search('>(.*)<', elem1)
print elem1
print survivorOwner
You have to switch control to inside the iframe and then try and interact with the webelement
iframe = driver.find_elements_by_tag_name('iframe')[0]
driver.switch_to_frame(iframe)
and to come out of the iframe back to the main window
driver.switch_to_default_content()
Try using this :
driver.switch_to.frame(driver.find_element_by_id('yourframeid'));
where the yourframeid is the ID for your frame or iframe

Categories

Resources