Swirch back to iframe selenium - java

I am facing a problem and I don't know where I am going wrong:
Thread.sleep(10000);
String mainWindow = driver.getWindowHandle();
while(true) {
try {
wc.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[#class='iff-campaign-container']/a")));
driver.findElement(By.xpath("//*[#class='iff-campaign-container']/a")).click();
//Here on click , it open new window in new browser.
for (String winHandle : driver.getWindowHandles()) {
driver.switchTo().window(winHandle);
}
driver.close();
driver.switchTo().window(mainWindow);
driver.switchTo().frame(0);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
break;
}
}
My wait condition fail after first loop run .
I am doing this , xpath is in iframe , and used iframe because , after every click new webelement is added.

In the line driver.switchTo().frame(0); you are changing the TargetLocator for the driver to the iframe, which doesn't have the specified xpath so the wait condition fails.
If you don't do anything within the iframe just remove it. If you do use it at the end switch back
driver.switchTo().frame(0);
// do stuff on the iframe
driver.switchTo().defaultContent();

Related

Selenium: Open jstree Nodes using Node name as String

I'm trying to automatize some test in order to open Jstree Nodes, using the Node name
I would like to make it clicking the arrow at the left of the node, instead of double-clicking the node itself.
Here is the code:
public void abrirSistema(String node) {
Boolean isPresent = driver.findElements(By.xpath("//*[contains(text(),'" + node + "')]")).size() > 0;
if (isPresent) {
System.out.println("System está abierto");
} else {
System.out.println("Hay que abrir el sistema");
WebElement system = driver.findElement(By.xpath("//*[contains(text(),'" + node + "')]"));
WebElement parent = system.findElement(By.xpath(".."));
String parentId = parent.getAttribute("id");
WebElement flecha = driver.findElement(By.xpath("//*[#id=\"" + parentId + "\"]/i"));
// WebElement arrow = parent.findElement(By.className("jstree-icon"));
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
js.executeScript("arguments[0].scrollIntoView(true);", arrow);
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
wait.until(ExpectedConditions.visibilityOf(flecha));
wait.until(ExpectedConditions.elementToBeClickable(flecha)).click();
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
So basically I locate the Node by the name, cause elements on the tree changes dynamically, then pick the arrow, and click it.
The thing is that sometimes the code works, sometimes it doesn't, and I cannot figure it out why.
I make sure that page is full loaded before trying to run this, and when I run the Scenario, the step is always green although the Node is not opened
Also I would like to let you know that this code runs always after opening the root node, which is working properly. Just in case
Hope someone could help me.
Thanks in advance

Interrupt loading of site by condition in Selenium WebDriver

There are multiple files which should be uploaded and waiting for output. It opens up an AJAX like window during processing. If processing takes too much time, Close button should be clicked on this window and file should be submitted again.
I try to use code below, but doesn't click Close button in 10 seconds.
public void clickOnSendButton() throws InterruptedException {
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement webElement;
try {
driver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);
driver.findElement(sendButton).click();
log.info("Processing in progress!");
webElement = wait.until(ExpectedConditions.presenceOfElementLocated(By.className("button-download")));
} catch (TimeoutException ex) {
webElement = null;
} finally {
driver.manage().timeouts().implicitlyWait(90, TimeUnit.SECONDS);
}
if (webElement == null) {
driver.findElement(popUpClose).click();
TimeUnit.SECONDS.sleep(1);
driver.findElement(sendButton).click();
}
}
Try using 'visibilityOfElementLocated' condition instead of 'presenceOfElementLocated' as below:
webElement = wait.until(ExpectedConditions.visibilityOfElementLocated(By.className("button-download")));
Using the loop:
try {
driver.findElement(By.id("button-submit")).click();
Thread.sleep(3000);//3 seconds
log.info("Processing in progress!");
for(int i=0; i<10;i++){
try{
webElement = driver.findElement(By.className("button-download"));
} catch (Exception e){e.printStackTrace();}
if(webElement.isDisplayed())
break;
else
Thread.sleep(1000);
}
} catch (TimeoutException ex) {ex.printStackTrace();} finally {
driver.manage().timeouts().implicitlyWait(90, TimeUnit.SECONDS);
}
if (!webElement.isDisplayed() ) {
driver.findElement(By.xpath("/html/body/div[4]/div[1]/button/span[1]")).click();
Thread.sleep(2000);
driver.findElement(By.id("button-submit")).click();
}

Click webelement until hidden

I have a web application where I press submit button until data available on the table.When in my no data are available then submit button hidden.So we can get logic until submitting button hides we will click.And when button, not available we show on success message and load next browser Url.
for (k=0;k>30;k++) {
try {
driver.findElement(By.xpath(".//*[#id='content']/input")).click();
driver.switchTo().alert();
driver.switchTo().alert().accept();
Thread.sleep(20000);
} catch (org.openqa.selenium.NoSuchElementException e){
System.out.println(""+location+" Done");
}
}
Here driver.findElement(By.xpath(".//*[#id='content']/input")).click(); this line click my submit button.And after submit one browser alert shows thats why i accept this. In this loop for (k=0;k>30;k++) Blindly i take 30..Is there any logic or any sugggestion how can i manage this...
You can use existence of the element in a way something like this:-
By by = By.xpath(".//*[#id='content']/input");
List<WebElement> buttons = driver.findElement(by);
while(buttons !=null && !buttons.isEmpty()) {
WebElement yourButton = buttons.get(0); // assuming only and the first element in the list
yourButton.click();
driver.switchTo().alert();
driver.switchTo().alert().accept(); // ideally this should be sufficient
Thread.sleep(20000);
buttons = driver.findElement(by);
}
Below code may help you.
try {
while(driver.findElement(By.xpath(".//*[#id='content']/input")).isDisplayed()){
driver.findElement(By.xpath(".//*[#id='content']/input")).click();
driver.switchTo().alert();
driver.switchTo().alert().accept();
Thread.sleep(20000);
}
}
catch (org.openqa.selenium.NoSuchElementException e){
System.out.println(""+location+" Done");
}
another solution with findElements,
while(driver.findElements(By.xpath(".//*[#id='content']/input")).size()>0)
{
try {
driver.findElement(By.xpath(".//*[#id='content']/input")).click();
driver.switchTo().alert();
driver.switchTo().alert().accept();
Thread.sleep(20000);
}
catch (org.openqa.selenium.NoSuchElementException e){
System.out.println(""+location+" Done");
}
}
Implement below Logic :
public void test() throws InterruptedException
{
WebElement button = driver.findElement(By.xpath(".//*[#id='content']/input"));
while(checkButton(button))
{
button.click();
driver.switchTo().alert().accept();
Thread.sleep(1000);
}
}
public static boolean checkButton(WebElement element)
{
if(element.isDisplayed())
return true;
else
return false;
}
It will click until your element get invisible once it your can perform your action further. Hope this will help.
Simply checked with isDisplayed() to check whether element is displayed or not
WebElement ele=driver.findElement(By.xpath(".//*[#id='content']/input"));
//check element is present or not
try {
if(ele.size()>0){
if(ele.isDisplayed()){
ele.click();
}
}
//switch to alert and perform operation
driver.switchTo().alert();
driver.switchTo().alert().accept();
Thread.sleep(20000);
}
catch (Exception e){
System.out.println(""+location+" Done");
}
Following way I solve my Problem..Hope Its will help to next visitor
do
{
try {
driver.findElement(By.xpath(".//*[#name='yt1']")).click();
driver.switchTo().alert();
driver.switchTo().alert().accept();
Thread.sleep(20000);
driver.switchTo().alert();
driver.switchTo().alert().accept();
Thread.sleep(2000);
driver.navigate().refresh();
}
catch (org.openqa.selenium.NoSuchElementException e){
System.out.println(""+location+" Done");
}
}
while ((driver.findElements(By.xpath(".//*[#class='google_data_save']")).size()>0));

Capturing full screen shot of webpage content using selenium 3 webdriver

i am trying to take a full content screen shot of web page . but i am only getting view based screen shot with following code. browser is firefox i am using SELENIUM 3 web driver.
File scrFile5 = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
try {
FileUtils.copyFile(scrFile5, new File("C:\\test.jpg"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
how i can achieve the same.
You may want to make use of RemoteWebDriver interface. Are you working with Firefox or IE? See below
File screenshotFile = null;
try {
// This checks to make sure we're not running an
// incompatible driver to take a screenshot (e.g.
// screenshots are not supported by the HtmlUnitDriver)
//the following line will throw a ClassCastException if the current driver is not a browser typed driver
RemoteWebDriver compatibleDriver = (RemoteWebDriver) driver;
screenshotFile = ((TakesScreenshot) compatibleDriver).getScreenshotAs(OutputType.FILE);
} catch (ClassCastException e) {
//this driver does not support screenshots.
// this should get logged as a warning -- this only means the driver cannot be of type RemoteWebDriver
} finally {
if (screenshotFile == null) {
System.out.println("This WebDriver does not support screenshots");
return; //get us outa here
}
}
try {
String pathString = "some path of your choice"
File path = new File(pathString);
if (!path.exists())
path.mkdirs();
stringPath.concat("/someFileName.png");
File newFileLocation = new File(pathString);
System.out.println("Full path to screenshot: " + newFileLocation.getAbsolutePath());
FileUtils.copyFile(screenshotFile, newFileLocation);
} catch (IOException e) {
e.printStackTrace();
}
It's an old problem with Selenium browser drivers.
Try this utility: aShot
You can find it at https://github.com/yandex-qatools/ashot

Unable to move to next page after clicking on button using Selenium Webdriver in IE11

This is my first post as I am stuck on this problem for many days. My script is unable to move to next page after a click on button. I checked that element is present and then clicked on it. It displays next page but script keeps running and never comes back. It never executes my information message given below.
System.out.println("continue button clicked");
Copying my Selenium and Java Code. Please help...
HTML:
<a tabindex="76" href="javascript:formSubmit()" oncontextmenu="return false">
<img src="/RetailCustomerManagement/resources/graphics/continue_orange_en.gif" style="border:none;"/>
</a>
Java Code:(using a frame work)
boolean isElementpresent = PerformAction.isElementsPresentNameCheck(continuebutton).size()!=0;
System.out.println("Is Element present? " + isElementpresent);
PerformAction.waitForElementToBeClickable(continuebutton);
PerformAction.FuncSubmit(continuebutton);
//PerformAction.FuncClick(continuebutton);
System.out.println("continue button clicked");
}
catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

Categories