Hi Ashish,
I am following video "Working with HTML frame,Ajax,Auto-suggests,Browser cookies.In this I am trying to get text from login iframe of Paytm but I am I getting no such element exception then I have made click and wait function as it's concept explained by you but I am not sure it's correct or not.Could you please let me know where I am wrong. I am pasting my code and attaching screenshot of exception I got.
Thanks in advance.
package framesandcookies;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebElement;
import org.testng.annotations.Test;
import base.TestBase;
public class Paytm extends TestBase{
@Test
public void paytm() throws InterruptedException {
launchBrowser("Chrome");
driver.get("http://paytm.com");
Thread.sleep(5000);
waitForPageToLoad();
driver.findElement(By.xpath("//*[@id='app']/div/div[2]/div[2]/div[3]/div[4]/div")).click();
clickAndWait("X", "Y");
List<WebElement>frames=driver.findElements(By.tagName("iframe"));
System.out.println("Total no. of frames are "+frames.size());
for(int i=0;i<frames.size();i++) {
//switch the ith frame
driver.switchTo().frame(i);
//check the presence of element,if present then break
int s=driver.findElements(By.xpath("//*[@id='main-container']/div/div/div/div/div[2]/qr-code-login/div/div[2]/div")).size();
if(s==0)
driver.switchTo().defaultContent();
else
break;
}
driver.switchTo().frame(0);
String text=driver.findElement(By.xpath("//*[@id='main-container']/div/div/div/div/div[2]/qr-code-login/div/div[2]/div")).getText();
System.out.println(text);
driver.switchTo().defaultContent();//main window
}
public void waitForPageToLoad() throws InterruptedException {
JavascriptExecutor js=(JavascriptExecutor) driver;
int i=0;
//this function wait to load page 100% -wait for max 20 sec
while(i!=0){
String state=(String)js.executeScript("return document.readyState;");
System.out.println(state);//complete,loading,interactive
if(state.equals("complete"))
break;
else
Thread.sleep(2000);
i++;
}
}
//try to click 5 times
public void clickAndWait(String xpathToClicked, String xpathAppear) throws InterruptedException {
//click on the element-xpathToClicked
//it will wait -presence and visibilty of next element
//if not visible it will wait for 2 seconds and it will again click on the element-xpathToClicked
//throw failure-if after 5 clicks the element is not appearing
for(int i=1;i<=5;i++) {
driver.findElement(By.xpath("//*[@id='app']/div/div[2]/div[2]/div[3]/div[4]/div")).click();
boolean b=driver.findElement(By.xpath("//img[@alt='close']")).isDisplayed();
if(b==false)
Thread.sleep(2000);
else
break;
}
}
}
Instructor
Ashish Thakur Replied on 18/02/2021
Do you get any error?
Yes I am getting no such element exception. I have attached the screenshot of error already.
Instructor
Ashish Thakur Replied on 21/02/2021
Please try and use this xpath for login link
//div[text()='Log In/Sign Up']
Thanks