python - I cannot click the Admin button nested inside the iFrame -
i automating our web application using python selenium webdriver. log application , want click administration button. when run code cannot find administration button xpath. have tried few different ways.
if enter //div[7]/div/div in selenium ide , click find highlights administration button. not know why won't find when run code. prefer use css faster xpath. need please.
i following error:
selenium.common.exceptions.nosuchelementexception: message: unable locate element: {"method":"xpath","selector":"html/body/div[2]/div[2]/div/div[2]/div/div[2]/div/div[7]/div/div"}
i inspect html element. full html follows:
<html style="overflow: hidden;"> <head> <body style="margin: 0px;"> <html style="overflow: hidden;"> <head> <body style="margin: 0px;"> <iframe id="__gwt_historyframe" style="position: absolute; width: 0; height: 0; border: 0;" tabindex="-1" src="javascript:''"> <html> </iframe> <noscript> <div style="width: 22em; position: absolute; left: 50%; margin-left: -11em; color: red; background-color: white; border: 1px solid red; padding: 4px; font-family: sans-serif;"> web browser must have javascript enabled in order application display correctly.</div> </noscript> <script src="spinner.js" type="text/javascript"> <script type="text/javascript"> <script src="clearcore/clearcore.nocache.js" type="text/javascript"> <script defer="defer"> <iframe id="clearcore" src="javascript:''" style="position: absolute; width: 0px; height: 0px; border: medium none;" tabindex="-1"> <!doctype html> <html> <head> <meta charset="utf-8"> <script> <script type="text/javascript"> <script type="text/javascript"> </head> <body> </html> </iframe> <div style="position: absolute; z-index: -32767; top: -20cm; width: 10cm; height: 10cm; visibility: hidden;" aria-hidden="true"> </div> <div style="position: absolute; left: 0px; top: 0px; right: 0px; bottom: 0px;"> <div style="position: absolute; z-index: -32767; top: -20ex; width: 10em; height: 10ex; visibility: hidden;" aria-hidden="true"> </div> <div style="position: absolute; overflow: hidden; left: 0px; top: 0px; right: 0px; bottom: 0px;"> <div style="position: absolute; left: 0px; top: 0px; right: 0px; bottom: 0px;"> <div style="position: absolute; z-index: -32767; top: -20ex; width: 10em; height: 10ex; visibility: hidden;" aria-hidden="true"> </div> <div style="position: absolute; overflow: hidden; left: 1px; top: 1px; right: 1px; bottom: 1px;"> <div class="gwt-tablayoutpanel" style="position: absolute; left: 0px; top: 0px; right: 0px; bottom: 0px;"> <div style="position: absolute; z-index: -32767; top: -20ex; width: 10em; height: 10ex; visibility: hidden;" aria-hidden="true"> </div> <div style="position: absolute; overflow: hidden; left: 0px; top: 0px; right: 0px; height: 30px;"> <div class="gwt-tablayoutpaneltabs" style="position: absolute; left: 0px; right: 0px; bottom: 0px; width: 16384px;"> <div class="gwt-tablayoutpaneltab gegqewxck gwt-tablayoutpaneltab-selected" style="background-color: rgb(254, 255, 238);"> <div class="gwt-tablayoutpaneltab gegqewxck" style="background-color: rgb(254, 255, 238);"> <div class="gwt-tablayoutpaneltab gegqewxck" style="background-color: rgb(254, 255, 238);"> <div class="gwt-tablayoutpaneltab gegqewxck" style="background-color: rgb(254, 255, 238);"> <div class="gwt-tablayoutpaneltab gegqewxck" style="background-color: rgb(254, 255, 238);"> <div class="gwt-tablayoutpaneltab gegqewxck" style="background-color: rgb(254, 255, 238);"> <div class="gwt-tablayoutpaneltab gegqewxck" style="background-color: rgb(254, 255, 238);"> <div class="gwt-tablayoutpaneltabinner"> <div class="gwt-html">administration</div> </div> </div> </div> </div> <div style="position: absolute; overflow: hidden; left: 0px; top: 30px; right: 0px; bottom: 0px;"> </div> </div> <div style="position: absolute; overflow: hidden; top: 1px; right: 1px; width: 30px; height: 25px;"> <div style="position: absolute; overflow: hidden; left: 0px; top: -25px; right: 0px; height: 25px;"> </div> </div> </div> <div style="display: none;" aria-hidden="true"></div> </body> </html>
my code follows:
element.py
from selenium.webdriver.support.ui import webdriverwait class basepageelement(object): def __set__(self, obj, value): driver = obj.driver webdriverwait(driver, 100).until( lambda driver: driver.find_element_by_name(self.locator)) driver.find_element_by_name(self.locator).send_keys(value) def __get__(self, obj, owner): driver = obj.driver webdriverwait(driver, 100).until( lambda driver: driver.find_element_by_name(self.locator)) element = driver.find_element_by_name(self.locator) return element.get_attribute("value")
locators.py
from selenium.webdriver.common.by import class mainpagelocators(object): submit_button = (by.id, 'submit') usernametxtbox = (by.id, 'unid') passwordtxtbox = (by.id, 'pwid') submitbutton = (by.id, 'button') administrationbutton = (by.css_selector, 'div.gwt-html.firepath-matching-node') administrationbuttonxpath = (by.xpath, '//html/body/div[2]/div[2]/div/div[2]/div/div[2]/div/div[7]/div/div') administrationbuttoncss = (by.css_selector, '/body/div[2]/div[2]/div/div[2]/div/div[2]/div/div[7]/div/div') administrationbuttonxpath2 = (by.xpath, 'html/body/div[2]/div[2]/div/div[2]/div/div[2]/div/div[7]/div/div/text()') administrationbuttonxpath3 = (by.xpath, '//div[7]/div/div') contentframe = (by.id, 'clearcore')
page.py
from element import basepageelement locators import mainpagelocators selenium.common.exceptions import nosuchelementexception selenium.common.exceptions import noalertpresentexception class searchtextelement(basepageelement): class basepage(object): def __init__(self, driver): self.driver = driver class loginpage(basepage): search_text_element = searchtextelement() def userlogin_valid(self): username_textbox = self.driver.find_element(*mainpagelocators.usernametxtbox) username_textbox.clear() username_textbox.send_keys("riaz.ladhani") password_textbox = self.driver.find_element(*mainpagelocators.passwordtxtbox) password_textbox.clear() password_textbox.send_keys("test123") submitbutton = self.driver.find_element(*mainpagelocators.submitbutton) submitbutton.click() #mydriver.find_element_by_xpath(xpaths['usernametxtbox']).clear() def clickadministration_button(self): #administrationbutton = self.driver.find_element(*mainpagelocators.administrationbutton) content_frame = self.driver.find_element(*mainpagelocators.contentframe) self.driver.switch_to.frame(content_frame) #self.driver.switch_to.frame(*mainpagelocators.contentframe) #self.driver.switch_to().frame(*mainpagelocators.contentframe) #administrationbuttoncss = self.driver.find_element(*mainpagelocators.administrationbuttoncss) #administrationbuttonxpath= self.driver.find_element(*mainpagelocators.administrationbuttonxpath) #administrationbuttonxpath= self.driver.find_element(*mainpagelocators.administrationbutton_css_regex) #administrationbuttoncss2 = self.driver.find_element(*mainpagelocators.administrationbuttoncss2) adminbutton = self.driver.find_element(*mainpagelocators.administrationbuttonxpath3) adminbutton.click()
loginpage_testcase.py
import unittest selenium import webdriver import page class loginpage_testcase(unittest.testcase): def setup(self): self.driver = webdriver.firefox() self.driver.get("http://my-pc.company.local:8080/clearcore") def test_login_valid_user(self): login_page = page.loginpage(self.driver) login_page.userlogin_valid() login_page.clickadministration_button() def teardown(self): self.driver.close() if __name__ == "__main__": unittest.main()
as “administration button” located under frame id “clearcore” , not in webpage. reason why element unable locate while executing code.
so before clicking button need switch frame either using
1. driver.switch_to_window("windowname") 2. driver.switch_to_frame("framename")
once done working on frames, have come parent frame can done using:
driver.switch_to_default_content()
Comments
Post a Comment