I made a loop for an iframe locator with java code
but its not working. Can anyone see the problem?
invoking the class with:
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
WebElement searchButton = IFrameLocator.switchToIFrameWithElement(driver,driver.findElement(By.cssSelector("[href*='Search.mvc'][class*='magnify']")));
and use this after:
searchButton.click();
public class IFrameLocator {
public static WebElement switchToIFrameWithElement(WebDriver driver, WebElement element) {
try {
driver.switchTo().defaultContent();
element.isDisplayed();
} catch (Exception continueFlow) {
WebDriverWait wait = new WebDriverWait(driver, 20);
List<WebElement> frames = driver.findElements(By.cssSelector("iframe"));
for (WebElement frame : frames) {
driver.switchTo().defaultContent();
try {
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(frame));
if (element.isDisplayed()) {
break;
}
} catch (NoSuchElementException | StaleElementReferenceException | ElementNotInteractableException ignored) {
}
}
} return element;
}
}
Because i was passing the whole Webelement By cssSelector it was not even going in the loop. So i pass the element like this:
WebElement searchButton = IFrameLocator.switchToIFrameWithElement(driver, By.cssSelector("[href*='Search.mvc'][class*='magnify']"));
So i changed the code and removed the By.cssSelector in 2 spots and all working fine now:
public class IFrameLocator {
public static WebElement switchToIFrameWithElement(WebDriver driver, By element) {
driver.switchTo().defaultContent();
try {
if (driver.findElement(element).isDisplayed()) ;
{
System.out.println("Element is displayed on main page");
}
} catch (Exception continueFlow) {
List<WebElement> frames = driver.findElements(By.cssSelector("iframe"));
for (WebElement frame : frames) {
driver.switchTo().defaultContent();
System.out.println("going back to main page");
try {
driver.switchTo().frame(frame);
System.out.println("switched to next frame: " + frame);
if (driver.findElement(element).isDisplayed()) {
System.out.println("element is found in frame: " + frame);
break;
}
} catch (NoSuchElementException | StaleElementReferenceException | ElementNotInteractableException ignored) {
}
}
} System.out.println("returned element succesfully");
return driver.findElement(element);
}
}
Related
I'm trying to create a function that loops through open windows and switches to a specific one based on the window title. This works for the first few windows, but the site I'm automating utilizes several popup windows that self close on actions such as a "next" button, and those handles seem to continue to be present after the window is closed.
So, when I try to switch to a window to check its title, the script times out because the WebDriver can't switch to a nonexistent window. It doesn't throw a NoSuchWindowException though, which is weird.
Below is my method for traversing windows and switching by title
public String switchByTitle(String title)
{
boolean isPage = false;
String neededHandle = null;
while(isPage == false)
{
for (String winHandle : driver.getWindowHandles())
{
try
{
//vvv timeout occurs here vvv
driver.switchTo().window(winHandle);
if (driver.switchTo().window(winHandle).getTitle().equals(title))
{
isPage = true;
neededHandle = winHandle;
break;
}
}
catch (NoSuchWindowException e)
{
continue;
}
}
}
return neededHandle;
}
UPDATE
Not Ideal But I found a Workaround
public String switchByTitle(String title)
{
boolean isPage = false;
String neededHandle = null;
WebDriverWait wait = new WebDriverWait(driver, 5);
while(isPage == false)
{
for (String winHandle : driver.getWindowHandles())
{
try
{
//shortened timeout to 5 seconds
driver.manage().timeouts().pageLoadTimeout(5, TimeUnit.SECONDS);
driver.switchTo().window(winHandle);
if (driver.switchTo().window(winHandle).getTitle().equals(title))
{
isPage = true;
neededHandle = winHandle;
break;
}
}
catch (NoSuchWindowException e)
{
continue;
}
//added timeout exception
catch (org.openqa.selenium.TimeoutException e)
{
continue;
}
}
}
//reset timeout
driver.manage().timeouts().pageLoadTimeout(1800, TimeUnit.SECONDS);
return neededHandle;
}
Used this as workaround:
public String switchByTitle(String title)
{
boolean isPage = false;
String neededHandle = null;
WebDriverWait wait = new WebDriverWait(driver, 5);
while(isPage == false)
{
for (String winHandle : driver.getWindowHandles())
{
try
{
//shortened timeout to 5 seconds
driver.manage().timeouts().pageLoadTimeout(5, TimeUnit.SECONDS);
driver.switchTo().window(winHandle);
if (driver.switchTo().window(winHandle).getTitle().equals(title))
{
isPage = true;
neededHandle = winHandle;
break;
}
}
catch (NoSuchWindowException e)
{
continue;
}
//added timeout exception
catch (org.openqa.selenium.TimeoutException e)
{
continue;
}
}
}
//reset timeout
driver.manage().timeouts().pageLoadTimeout(1800, TimeUnit.SECONDS);
return neededHandle;
}
The following code is not working. The reportDataFields displays a list of items(for ex abc, abd, abe) and I want to select abc and drop into the target. It does not fisplay any error message either.
Actions action = new Actions(driver);
List<WebElement> reportFields = driver.findElements(By.className("reportDataFields"));
WebElement target = driver.findElement(By.id("rptDataSections"));
for (int i = 0; i < reportFields.size(); i++) {
if (reportFields.get(i).getText().equals(Section)) {
action.dragAndDrop(reportFields.get(i), target).release().build().perform();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
I think you should create a new instance of Actions interface every time you use it.
Try below code with my personalized drag and drop functionality:
List<WebElement> reportFields = driver.findElements(By.className("reportDataFields"));
WebElement target = driver.findElement(By.id("rptDataSections"));
for (int i = 0; i < reportFields.size(); i++) {
if (reportFields.get(i).getText().equals(Section)) {
WebElement draggedFrom = reportFields.get(i);
new Actions(driver)
.moveToElement(draggedFrom)
.pause(Duration.ofSeconds(1))
.clickAndHold(draggedFrom)
.pause(Duration.ofSeconds(1))
.moveByOffset(1, 0)
.moveToElement(target)
.moveByOffset(1, 0)
.pause(Duration.ofSeconds(1))
.release().perform();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Correct wait to wait for an element to disappear?
I have an ajax loader which loads after for example clicking on a button, is my method correct inorder to wait for a particular load bar which takes the full width and height of a screen to disappear?
public void waitUntilAjaxLoaderDisapears() {
// Wait up to 2minutes for the element to disappear
WebDriverWait ajaxWait = new WebDriverWait(this.driver, 60);
ajaxWait.pollingEvery(100, TimeUnit.SECONDS);
try {
//tempWait.until(ExpectedConditions.invisibilityOfElementLocated(By.cssSelector(".modal-body")));
ajaxWait.until(ExpectedConditions.invisibilityOfElementLocated(By.xpath("//*[contains(#class, 'ajax_loader')]")));
} catch (UnhandledAlertException e) {
Alert alert = driver.switchTo().alert();
alert.accept();
}catch (NoAlertPresentException e) {
//
}catch (StaleElementReferenceException e) {
// do nothing
} catch (NullPointerException e) {
// do nothing
} catch (Exception e) {
// do nothing
}
}
I think i got the issue. The polling time should be less than the overall wait time. So it can be,
WebDriverWait ajaxWait = new WebDriverWait(this.driver, 60);
ajaxWait.pollingEvery(**5**, TimeUnit.SECONDS);
Instead of
WebDriverWait ajaxWait = new WebDriverWait(this.driver, 60);
ajaxWait.pollingEvery(100, TimeUnit.SECONDS);
Hope this helps you. Thanks.
How to list all the checkboxes available in a webpage by displaying their Visible Text in selenium using Java?
For that website this should do
List<WebElement> checkboxes = driver.findElements(By.cssSelector("input[type=checkbox]"));
JavascriptExecutor js = (JavascriptExecutor) driver;
if (checkboxes.isEmpty()) {
System.out.println("No Checkbox present in the page");
} else {
for (WebElement checkbox : checkboxes) {
if (checkbox.isDisplayed()) {
String text=(String) js.executeScript("return arguments[0].nextSibling.textContent.trim();", checkbox);
System.out.println(text);
}
}
}
I have a class Automator that can automate a user. I am specifically having problems setting the system clipboard in windows. The Automator class makes use of the ClipSetThread class, which is a thread that sets the system clipboard. A instance of ClipSetThread takes as input a thread, that if null, it joins with (waits for it to complete).
I feel that I am not calling ClipSetThread right because I still have the errors I have had before in its reliability; prior to the ClipSetThread. This code does not throw any errors when it runs, it works about 2/3 of the time though. Other times it will print 1134, _234, or etc. It seems that the threads are not joining (waiting for) each other, or get skipped.
Code:
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import org.jnativehook.GlobalScreen;
import org.jnativehook.NativeHookException;
import org.jnativehook.mouse.NativeMouseEvent;
import org.jnativehook.mouse.NativeMouseInputListener;
public class Automator extends Thread implements NativeMouseInputListener
{
Robot rob = null;
TheAppClass theApp = null;
ClipSetThread lastClipSet = null;
boolean doit = false;
boolean settingClip = false;
public void run()
{
try // to make the Global hook
{
GlobalScreen.registerNativeHook();
}
catch (NativeHookException ex){theApp.updateOutput("No Global Keyboard or Mouse Hook");return;}
try // to create a robot (can simulate user input such as mouse and keyboard input)
{
rob = new Robot();
}
catch (AWTException e1) {theApp.updateOutput("The Robot could not be created");return;}
while(true) {}
}
public void setApp(TheAppClass app)
{
theApp = app;
theApp.updateOutput("Succesfully started automator");
}
public void setClip(String arg)
{
ClipSetThread set = new ClipSetThread(theApp, lastClipSet);
lastClipSet = set;
set.setClip(arg);
}
public void DOit()
{
theApp.updateOutput("Starting");
pasteAtCursorLocation("1");
tab(1);
pasteAtCursorLocation("2");
tab(1);
pasteAtCursorLocation("3");
tab(1);
pasteAtCursorLocation("4");
tab(1);
theApp.updateOutput("Complete");
}
public void nativeMouseReleased(NativeMouseEvent e)
{
//System.out.println("Mouse Released: " + e.getButton());
if(doit)
{
DOit();
doit = false;
}
}
public void pasteAtCursorLocation(String text)
{
setClip(text);
rob.keyPress(KeyEvent.VK_CONTROL);
rob.keyPress(KeyEvent.VK_V);
rob.keyRelease(KeyEvent.VK_V);
rob.keyRelease(KeyEvent.VK_CONTROL);
theApp.updateOutput("Simulated Paste");
}
public void tab(int numTimes)
{
while(numTimes > 0)
{
rob.keyPress(KeyEvent.VK_TAB);
rob.keyRelease(KeyEvent.VK_TAB);
numTimes--;
theApp.updateOutput("Simulated Tab");
}
}
// Unimplemented
public void nativeMouseClicked(NativeMouseEvent arg0) {}
public void nativeMousePressed(NativeMouseEvent arg0) {}
public void nativeMouseDragged(NativeMouseEvent arg0) {}
public void nativeMouseMoved(NativeMouseEvent arg0) {}
}
ClipSetThread:
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
public class ClipSetThread extends Thread
{
Clipboard sysClip = null;
TheAppClass theApp = null;
public ClipSetThread(TheAppClass app, Thread waitFor)
{
theApp = app;
sysClip = Toolkit.getDefaultToolkit().getSystemClipboard();
if(waitFor != null)
{try {waitFor.join();}catch (InterruptedException e) {}}
}
public void setClip(String arg)
{
// Two strings that will hopefully never be on the clipboard
String checkStr1 = "9999999999999";
String checkStr2 = "99999999999999";
// When we read in the clipboard we want to see if we change these strings from the ones they
// will never be, if they do change we read the clipboard successfully
String clipBoardTextBefore = checkStr1;
String clipBoardTextAfter = checkStr2;
// First get a copy of the current system clipboard text
while(true)
{
try
{
Transferable contents = sysClip.getContents(null);
clipBoardTextBefore = (String)contents.getTransferData(DataFlavor.stringFlavor);
}
catch(Exception e)
{
try {Thread.sleep(20);} catch (InterruptedException e1) {}
continue;
}
break;
}
// If we failed to change the string it means we failed to read the text
if(clipBoardTextBefore.equals(checkStr1))
theApp.updateOutput("Could NOT get sysClip text");
else
{
// If we didn't failed to get the current text try to change it
while(true)
{
try{sysClip.setContents(new StringSelection(arg), null);}
catch(Exception e)
{
try {Thread.sleep(20);} catch (InterruptedException e1) {}
continue;
}
break;
}
// Now again check to see the clipboard text
while(true)
{
try
{
Transferable contents = sysClip.getContents(null);
clipBoardTextAfter = (String)contents.getTransferData(DataFlavor.stringFlavor);
}
catch(Exception e)
{
try {Thread.sleep(20);} catch (InterruptedException e1) {}
continue;
}
break;
}
// If we failed to read the clipboard text
if(clipBoardTextAfter.equals(checkStr2))
theApp.updateOutput("Could NOT check if sysClip update was successful");
else
{ // We re-read the clipboard text, see if it changed from the original clipboard text
if(clipBoardTextAfter.equals(checkStr1))
theApp.updateOutput("Could NOT successfully set clipboard text");
else
theApp.updateOutput("Set Clipboard Text:" + arg + "\n");
}
}
}
}
So, firstly, you never call start on the ClipSetThread. You should also check to see if the thread is still alive before joining it.
public class ClipSetThread extends Thread {
Clipboard sysClip = null;
TheAppClass theApp = null;
private String toClipboard;
public ClipSetThread(TheAppClass app, Thread waitFor, String toClipBoard) {
theApp = app;
sysClip = Toolkit.getDefaultToolkit().getSystemClipboard();
this.toClipboard = toClipBoard;
// !! Check to see if the thread is also alive before trying to join with it...
if (waitFor != null && waitFor.isAlive()) {
try {
waitFor.join();
} catch (InterruptedException e) {
}
}
}
// You should really put your logic into the `run` method in order to allow
// the code to actually run in a separate thread...otherwise there is no
// point in using a thread....
#Override
public void run() {
// Two strings that will hopefully never be on the clipboard
String checkStr1 = "9999999999999";
String checkStr2 = "99999999999999";
// When we read in the clipboard we want to see if we change these strings from the ones they
// will never be, if they do change we read the clipboard successfully
String clipBoardTextBefore = checkStr1;
String clipBoardTextAfter = checkStr2;
// First get a copy of the current system clipboard text
while (true) {
try {
Transferable contents = sysClip.getContents(null);
clipBoardTextBefore = (String) contents.getTransferData(DataFlavor.stringFlavor);
} catch (Exception e) {
try {
Thread.sleep(20);
} catch (InterruptedException e1) {
}
continue;
}
break;
}
// If we failed to change the string it means we failed to read the text
if (clipBoardTextBefore.equals(checkStr1)) {
theApp.updateOutput("Could NOT get sysClip text");
} else {
// If we didn't failed to get the current text try to change it
while (true) {
try {
sysClip.setContents(new StringSelection(toClipboard), null);
} catch (Exception e) {
try {
Thread.sleep(20);
} catch (InterruptedException e1) {
}
continue;
}
break;
}
// Now again check to see the clipboard text
while (true) {
try {
Transferable contents = sysClip.getContents(null);
clipBoardTextAfter = (String) contents.getTransferData(DataFlavor.stringFlavor);
} catch (Exception e) {
try {
Thread.sleep(20);
} catch (InterruptedException e1) {
}
continue;
}
break;
}
// If we failed to read the clipboard text
if (clipBoardTextAfter.equals(checkStr2)) {
theApp.updateOutput("Could NOT check if sysClip update was successful");
} else { // We re-read the clipboard text, see if it changed from the original clipboard text
if (clipBoardTextAfter.equals(checkStr1)) {
theApp.updateOutput("Could NOT successfully set clipboard text");
} else {
theApp.updateOutput("Set Clipboard Text:" + toClipboard + "\n");
}
}
}
}
}
As per our previous converstaion, it's dangerous to use while (true) {}, it's also wasteful, as it will consume CPU cycles unnecessarily...
public class Automator extends Thread implements NativeMouseInputListener {
// A "locking" object...
private static final Object WAIT_LOCK = new Object();
Robot rob = null;
TheAppClass theApp = null;
ClipSetThread lastClipSet = null;
boolean doit = false;
boolean settingClip = false;
public void run() {
try // to make the Global hook
{
GlobalScreen.registerNativeHook();
} catch (NativeHookException ex) {
theApp.updateOutput("No Global Keyboard or Mouse Hook");
return;
}
try // to create a robot (can simulate user input such as mouse and keyboard input)
{
rob = new Robot();
} catch (AWTException e1) {
theApp.updateOutput("The Robot could not be created");
return;
}
// This is wasteful...
// while (true) {
// }
// Locks do not consume CPU cycles while in the wait state...
synchronized (WAIT_LOCK) {
try {
WAIT_LOCK.wait();
} catch (Exception exp) {
}
}
}
public void dispose() {
// Tell the thread it can terminate...
synchronized (WAIT_LOCK) {
WAIT_LOCK.notify();
}
// This will STOP the current thread (which called this method)
// while the lastClipSet finishes...
if (lastClipSet != null && lastClipSet.isAlive()) {
lastClipSet.join();
}
}
public void setClip(String arg) {
ClipSetThread set = new ClipSetThread(theApp, lastClipSet, arg);
lastClipSet = set;
// You MUST START the thread...
set.start();
}
/*...*/
}
Updated
This code could produce a infinite loop. What happens if the clipboard does not contain a String value??
while(true)
{
try
{
Transferable contents = sysClip.getContents(null);
clipBoardTextBefore = (String)contents.getTransferData(DataFlavor.stringFlavor);
}
catch(Exception e)
{
try {Thread.sleep(20);} catch (InterruptedException e1) {}
continue;
}
break;
}
You tend to do this a lot. I might suggest that you provide some kind of "escape" mechanism to allow it to fail after a number of retries...
boolean successful = false;
int retries = 0;
while (!successful && retries < 20) {
{
try
{
Transferable contents = sysClip.getContents(null);
clipBoardTextBefore = (String)contents.getTransferData(DataFlavor.stringFlavor);
successful = true;
}
catch(Exception e)
{
retries++;
try {Thread.sleep(20);} catch (InterruptedException e1) {}
}
}
Updated with working example
Okay, that was fun. I've put together a (simple) working example. You will want to open a text editor of some kind. When you run the program, you have 5 seconds to make it active ;)
The only basic change I've made is I set added a auto delay between events of 250 milliseconds (see rob.setAutoDelay(250).
Now, you could also place a delay between each key event as well, using Robot#delay, but that's up to you
public class Engine extends Thread {
private Robot rob = null;
private PasteThread lastClipSet = null;
public void setClip(String arg) {
if (lastClipSet != null && lastClipSet.isAlive()) {
try {
lastClipSet.join();
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
PasteThread set = new PasteThread(arg);
lastClipSet = set;
lastClipSet.start();
}
public void pasteAtCursorLocation(String text) {
System.out.println("Paste " + text);
setClip(text);
rob.keyPress(KeyEvent.VK_CONTROL);
rob.keyPress(KeyEvent.VK_V);
rob.keyRelease(KeyEvent.VK_V);
rob.keyRelease(KeyEvent.VK_CONTROL);
}
public Engine() throws AWTException {
rob = new Robot();
rob.setAutoDelay(250);
try {
Thread.sleep(5000);
} catch (InterruptedException ex) {
}
pasteAtCursorLocation("This is a simple test, thanks for watching!");
}
public static void main(String[] args) {
try {
new Engine();
} catch (AWTException ex) {
Logger.getLogger(Engine.class.getName()).log(Level.SEVERE, null, ex);
}
}
public class PasteThread extends Thread {
private String toPaste;
public PasteThread(String value) {
toPaste = value;
}
#Override
public void run() {
Clipboard sysClip = Toolkit.getDefaultToolkit().getSystemClipboard();
System.out.println("Current clipboard contents = " + getClipboardContents(sysClip));
sysClip.setContents(new StringSelection(toPaste), null);
System.out.println("New clipboard contents = " + getClipboardContents(sysClip));
}
public String getClipboardContents(Clipboard clipboard) {
String value = null;
boolean successful = false;
int retries = 0;
while (!successful && retries < 20) {
Transferable contents = clipboard.getContents(null);
if (contents.isDataFlavorSupported(DataFlavor.stringFlavor)) {
try {
value = (String) contents.getTransferData(DataFlavor.stringFlavor);
successful = true;
} catch (Exception exp) {
retries++;
exp.printStackTrace();
}
} else {
retries++;
}
}
System.out.println(successful + "/" + retries);
return value;
}
}
}
Could you please try to repeat the Paste action with a sleep 1 second in between
public void pasteAtCursorLocation(String text)
{
setClip(text);
rob.keyPress(KeyEvent.VK_CONTROL);
rob.keyPress(KeyEvent.VK_V);
rob.keyRelease(KeyEvent.VK_V);
rob.keyRelease(KeyEvent.VK_CONTROL);
theApp.updateOutput("Simulated Paste");
// put in a sleep 1 second here
rob.keyPress(KeyEvent.VK_CONTROL);
rob.keyPress(KeyEvent.VK_V);
rob.keyRelease(KeyEvent.VK_V);
rob.keyRelease(KeyEvent.VK_CONTROL);
theApp.updateOutput("Simulated Paste");
}
It could be that pasting 2x is giving different results. The reason for this strange behavior could the way Windows manages the clipboard. If pasting 2x the clipboard is giving different result then you know that the root cause for this strange behavior is not to find in your code but how Java and Windows work together.