Selenium Webdriver - Unable to find element after page refresh/redirect

10,651

Solution 1

Do it this way...

driver.switchTo().frame(driver.findElement(By.xpath("//iframe[@name='a1']"))); // switching to iframe

followed by

driver.switchTo().frame("f1"); // switch to frame

and then your desired action...

driver.findElement(By.id("abcd")).click();

Solution 2

This is because of the iframe. You need to switch to it first:

driver.switchTo().frame(0);
driver.findElement(By.id("abcdef")).click();

where 0 is a frame index.

Solution 3

See doc on implicit wait here

I guess you should do a implicit wait until your chosen element is available

modify these code to suit your chosen element:

WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.id("someid")));
Share:
10,651
Siva
Author by

Siva

Updated on June 05, 2022

Comments

  • Siva
    Siva almost 2 years

    I am trying to write some UI test cases for an SAP-webUI (web based) application. After login, it shows the dashboard ( Workcenter's ) screen.

    Now the problem is, I am able to open the page, enter U/N, Pwd and login through Selenium. After i press the "Login" button the URL changes and the page got redirected/refreshed.

    E.g. URL before login : https://a/b/c/d/e/f/g.htm?sap-client=001&sap-sessioncmd=open

    E.g. URL after successful Login : https://a/b(bDsdfsdsf1lg==)/c/d/e/f/g.htm

    After this im unable to perform any action or press any link in any part of the page. I tried with all the possible attributes ( css, xpath, id ). Webdriver was unable to find any element on the page. It shows the error "No element found" alone.

    I am using java with Selenium Web Driver.

    Please find the html structure of the webpage below

    <html><body><div><div><iframe>#document<html><head></head><frameset><frameset><frame>#document<html><head></head><body><form><div><div><table><tbody><tr><td><div><ul><li><a id=abcdef></a></li></ul></div></td></tr></tbody></table></div></div></form></body></html></frame></frameset></frameset></html></iframe></div></div></body></html>
    

    Actually i want to click a linkmenu "abcd", which is inside iframe and frame as shown in the below HTML code

    <html><head></head><body><iframe name=a1><html><head></head><frameset><frameset name=fs1><frame name=f1><html><head></head><body><table><tbody><tr><td><ul><li><a id=abcdef>

    I tried the below code as well.

    driver.switchTo().frame("a1"); driver.findElement(By.id("abcd")).click();

    OR

    driver.findElement(By.xpath("//*[@id='abcd']")).click();

    After using the above code, still im getting the error "No such element"

    Kindly suggest

    Regards, Siva

  • Siva
    Siva about 9 years
    Hi, After using the above code, still im getting the error "No such element" as below. org.openqa.selenium.NoSuchElementException: no such element Actually i want to click a linkmenu "abcd", which is inside iframe and frame as shown below <iframe name=a1><frameset><frameset name=fs1><frame name=f1><a id = abcd> I tried the below code as well. driver.switchTo().frame("a1"); driver.findElement(By.id("abcd")).click(); OR driver.findElement(By.xpath("//*[@id='abcd']")).click(); Kindly suggest Regards, Siva
  • Siva
    Siva about 9 years
    Hi, Still im gettitng "No element found" error org.openqa.selenium.NoSuchElementException: no such element
  • Siva
    Siva about 9 years
    Please find the HTML hierarchy till the link menu <html><head></head><body><iframe name=a1><html><head></head><frameset><frameset name=fs1><frame name=f1><html><head></head><body><table><tbody><tr><td><ul><‌​li><a id=abcdef>
  • Vivek Singh
    Vivek Singh about 9 years
    and at which line u r getting exception?
  • Siva
    Siva about 9 years
    org.openqa.selenium.NoSuchFrameException: no such frame (Session info: chrome=41.0.2272.101) (Driver info: chromedriver=2.14.313457 (3d645c400edf2e2c500566c9aa096063e707c9cf),platform=Windows NT 6.1 SP1 x86_64) (WARNING: The server did not provide any stacktrace information)
  • Siva
    Siva about 9 years
    Capabilities [{platform=XP, acceptSslCerts=true, javascriptEnabled=true, browserName=chrome, chrome={userDataDir=C:\Users\AppData\Local\Temp\scoped_dir91‌​52_7396}, rotatable=false, locationContextEnabled=true, mobileEmulationEnabled=false, version=41.0.2272.101, takesHeapSnapshot=true, cssSelectorsEnabled=true, databaseEnabled=false, handlesAlerts=true, browserConnectionEnabled=false, webStorageEnabled=true, nativeEvents=true, applicationCacheEnabled=false, takesScreenshot=true}]
  • Siva
    Siva about 9 years
    In the below command driver.findElement(By.id("abcd")).click();
  • Siva
    Siva about 9 years
    Hi, Im getting no frame found error only in chrome. In IE and firefox it is working fine. Can you provide me the suitable code to use in chrome @alecxe
  • Siva
    Siva almost 9 years
    Hi, The above code works fine in IE. But not in chrome. If i execute it in chrome it shows 'No frame found' Kindly help
  • Siva
    Siva almost 9 years
    Hi, Im getting no frame found error only in chrome. In IE and firefox it is working fine. Can you provide me the suitable code to use in chrome @alecxe
  • Vivek Singh
    Vivek Singh almost 9 years
    Hi..please provide the html of chrome.