Wednesday, 15 February 2012

java - Selenium WebDriver can't locate element in iframe even when switching to it -



java - Selenium WebDriver can't locate element in iframe even when switching to it -

i have searched lot of forum topîcs solve issue didn't find similar problem. i'm writing tests selenium webdriver using java.

the website i'm testing uses iframe visible content user loaded into. looks :

<html> <head> (...) </head> <body> <iframe id="portaliframe"> <html> <body> <h2 id="aui_3_2_0_1594">etat des agents</h2> ... </body> </html> </iframe> </body> </html>

my problem next : finding element in iframe doesn't work. when want locate such element first run method switch iframe :

protected void switchiframe() { webdriverwait wait = new webdriverwait(driver, 10); by = by.id("portaliframe"); seek { wait.until(expectedconditions.frametobeavailableandswitchtoit(by)); } grab (timeoutexception e) { e.printstacktrace(); } }

and can illustration find h2 element:

driver.findelement(by.xpath("//h2[contains(text(),'some text')]"));

what can't understand selenium doesn't manage find elements on webpage. instance managed find h2 element, click on menu link load page when seek find 1 time again < h2 > element on new page, "element not found error" , if seek add together switchiframe() before that, selenium says cannot find iframe.

it seems happen "randomly" don't know solve issue. issue same on chromedriver , firefoxdriver. don't utilize others drivers because firefox , chrome used private website.

edit i'm sorry can't paste html content because it's intranet website , can't share here. illustration of h2 element seek locate < h2 id="aui_3_2_0_1594">etat des agents< /h2 >

also problem quite similar issue : selenium webdriver not able find elements not nowadays in page source nowadays in html when seen through developer tools issue appens not on h2 elements illustration on "a" elements want click on.

thanks

it appeared wasn't waiting plenty time when looking html elements. increased timeout of explicit wait method. i'm not talking switchiframe() i'm referring method used wait elements nowadays :

so, i've increased timeout 3 6 seconds , seems work great now.

protected webelement waitforelementvisible(by by) { webdriverwait wait = new webdriverwait(driver,6); webelement element = null; seek { element = wait.until(expectedconditions.visibilityofelementlocated(by)); } grab (timeoutexception e) { //method logging error error("timeout : element " + by.tostring() + " not visible"); } homecoming element; }

java iframe selenium webdriver selenium-chromedriver

No comments:

Post a Comment