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));
Related
I'm trying to use an element for testing and I want to continue the test if the element even couldn't be found.
I used NoSuchElementException for this part of my codes.
Here is what I've tried before:
try {
WebElement site_width_full_width = wait.until(
ExpectedConditions.visibilityOfElementLocated(
By.cssSelector("label[for=site_width-full_width]")));
site_width_full_width.click();
Thread.sleep(1000);
System.out.println("FullWidth Label Found!");
} catch (NoSuchElementException e) {
System.out.println("FullWidth Label not found!");
System.out.println(e);
}
But when the element isn't available it can't be thrown into the NoSuchElementException and all test breaks and fails.
What's the solution and how could I continue the test when the element isn't available.
Thanks in advance.
You might be getting the exception of a different derived class type. You can catch it using the parent class 'Exception' and then further drill down the exact exception type.
try using;
try
{
WebElement site_width_full_width = wait.until(
ExpectedConditions.visibilityOfElementLocated(
By.cssSelector("label[for=site_width-full_width]")));
site_width_full_width.click();
Thread.sleep(1000);
System.out.println("FullWidth Label Found!");
}
catch (Exception e)
{
if (e instanceof NoSuchElementException)
{
System.out.println("FullWidth Label not found!");
System.out.println(e);
}
else
{
System.out.println("Unexpected exception!");
System.out.println(e);
}
}
Hope this helps.
You can try with its parent classes like Throwable or Exception in catch block. In my case, I am Throwable in catch block which works as expected
You are catching a NoSuchElementException, but an explicit wait throws a TimeoutException if nothing is found. To get what you have working you should modify your code to do the following:
try {
WebElement site_width_full_width = wait.until(
ExpectedConditions.visibilityOfElementLocated(
By.cssSelector("label[for=site_width-full_width]")
));
site_width_full_width.click();
System.out.println("FullWidth Label Found!");
} catch (NoSuchElementException | TimeoutException e) {
System.out.println("FullWidth Label not found!");
System.out.println(e);
}
However using Try/Catch for execution flow is generally a code anti-pattern. You would be much better off doing something like this:
List<WebElement> site_width_full_width =
driver.findElements(By.cssSelector("label[for=site_width-full_width]"));
if (site_width_full_width.size() > 0) {
System.out.println("FullWidth Label Found!");
site_width_full_width.get(0).click();
} else {
System.out.println("FullWidth Label not found!");
}
Webdriver already provide the way to deal this problem in much simpler way. you can use following way
WebDriverWait wait= new WebDriverWait(driver, TimeSpan.FromSeconds(120));
wait.IgnoreExceptionTypes(typeof(NoSuchElementException), typeof(ElementNotVisibleException));
WebElement site_width_full_width = wait.until(
ExpectedConditions.visibilityOfElementLocated(
By.cssSelector("label[for=site_width-full_width]")));
site_width_full_width.click();
Thread.sleep(1000);
System.out.println("FullWidth Label Found!");
Note : You can add all types of exception need to be ignored.
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();
}
OS: Windows 7 32bit
ChromeDriver version: 2.30
Selenium Webdriver version: 3.4.0
Java 8
I've tried a few different ways to clean this code up and not have to repeat the same try/catch blocks. I'm trying to check that various elements are present on a page I'm testing. I can gracefully report to the console and this code does work with no problems.
The issue I'm having is with the ungraceful code. Is there a way to nest these try/catch blocks, or put them inside of an if/else loop?
try {
driver.findElement(By.xpath("/html/head/title"));
System.out.println("Title found...");
Thread.sleep(1000);
} catch (NoSuchElementException ex) {
System.out.println("Title NOT FOUND...");
// ex.printStackTrace();
}
try {
driver.findElement(By.xpath("//*[#id='logindiv']"));
System.out.println("Login form found...");
Thread.sleep(1000);
} catch (NoSuchElementException ex) {
System.out.println("Login form NOT FOUND...");
// ex.printStackTrace();
}
try {
driver.findElement(By.id("username"));
System.out.println("'Username' field found...");
Thread.sleep(1000);
} catch (NoSuchElementException ex) {
System.out.println("'Username' form NOT FOUND...");
// ex.printStackTrace();
}
try {
driver.findElement(By.id("password"));
System.out.println("'Password' field found...");
Thread.sleep(1000);
} catch (NoSuchElementException ex) {
System.out.println("'Password' form NOT FOUND...");
// ex.printStackTrace();
}
try {
driver.findElement(By.id("loginSubmit")).getText();
System.out.println("Login button found...");
Thread.sleep(1000);
} catch (NoSuchElementException ex) {
System.out.println("Login button NOT FOUND...");
// ex.printStackTrace();
}
A few things...
I'm a firm believer that exceptions should be exceptional, meaning exceptions shouldn't be used as flow control. This is further supported by the docs,
findElement should not be used to look for non-present elements, use findElements(By) and assert zero length response instead.
so you should replace .findElement() and try-catch with .findElements() and test for empty. See example in #2.
You really should use some Assert library like JUnit, etc. It makes these validations so much easier/cleaner.
This whole thing
try
{
driver.findElement(By.id("username"));
System.out.println("'Username' field found...");
Thread.sleep(1000);
}
catch (NoSuchElementException ex)
{
System.out.println("'Username' form NOT FOUND...");
// ex.printStackTrace();
}
should/could look like
Assert.assertFalse(driver.findElements(By.id("username")).isEmpty(), "Validate Username field exists");
Arguments could be made that it's faster, so on and so forth but... it hurts me to see people use something complicated like an XPath to do no more than locate an element by ID, e.g. By.xpath("//*[#id='logindiv']"). This is so much simpler and easier to read as By.id("logindiv").
You can do some googling to see all the details but Thread.Sleep() is a bad practice and should be avoided. Instead use WebDriverWait. Explore ExpectedConditions to see all what can be waited for and use it liberally. In the cases you posted in your code, I don't see any reason to wait for any of these so that's several seconds of unnecessary wasted time.
Your last example is pulling .getText() but not using it. Since you are just checking that the button exists, you can safely remove the .getText() from the end.
You can try this:
public void checkElementVisibile()throws InterruptedException {
element = driver.findElement(By.xpath("/html/head/title"));
Thread.sleep(1000);
if(isElementVisibile(element))
System.out.println("Title found...");
else
System.out.println("Title not found...");
element = driver.findElement(By.xpath("//*[#id='logindiv']"));
Thread.sleep(1000);
if(isElementVisibile(element))
System.out.println("Login form found...");
else
System.out.println("Login form not found...");
element = driver.findElement(By.id("username"));
Thread.sleep(1000);
if(isElementVisibile(element))
System.out.println("'Username' field found...");
else
System.out.println("'Username' field not found...");
element = driver.findElement(By.id("password"));
Thread.sleep(1000);
if(isElementVisibile(element))
System.out.println("'Password' field found...");
else
System.out.println("'Password' field not found...");
}
public static boolean isElementVisibile(WebElement element){
try {
return element.isDisplayed();
}catch (NoSuchElementException ex)
{
return false;
}
}
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();
I'm having a problem in my script. I'm using Selenium WebDriver to drive a webpage, but i'm getting ElementNotFound exceptions quite regularly. The page takes a second or two to load.
My code is the following:
Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
.withTimeout(10, TimeUnit.SECONDS)
.pollingEvery(1, TimeUnit.SECONDS)
.ignoring(NoSuchElementException.class);
try
{
WebElement username = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[#class='gwt-TextBox']")));
username.sendKeys(usernameParm);
}
catch (Exception e) {
e.printStackTrace();
}
The exception still gets thrown after a second or so. Then if i test it by running the following:
Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
.withTimeout(10, TimeUnit.SECONDS)
.pollingEvery(1, TimeUnit.SECONDS)
.ignoring(NoSuchElementException.class);
try
{
WebElement username = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[#class='gwt-TextBox1']")));
username.sendKeys(usernameParm);
}
catch (Exception e) {
e.printStackTrace();
}
Knowing that TexBox1 doesn't exist, then it throws the same exception. It doesn't appear to be waiting. In the second instance i would expect it to time out, and not throw ElementNotFoundException.
My implementation is probably wrong.
Checkout my post on this topic: https://iamalittletester.wordpress.com/2016/05/11/selenium-how-to-wait-for-an-element-to-be-displayed-not-displayed/. There are code snippets there. Basically my suggestion is not to use FluentWait, but instead:
WebDriverWait wait = new WebDriverWait(driver, TIMEOUT);
ExpectedCondition elementIsDisplayed = new ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver arg0) {
try {
webElement.isDisplayed();
return true;
}
catch (NoSuchElementException e ) {
return false;
}
catch (StaleElementReferenceException f) {
return false;
}
}
};
wait.until(elementIsDisplayed);
Define TIMEOUT with whatever timeout value seems ok for you (i believe you said 10 seconds in your initial issue).
Yes, i'm answering my own question. Figured out what the problem was.
I had imported java.lang.util.NoSuchElementException
I had told FluentWait to ignore NoSuchElementException
What was actually being thrown was org.openqa.selenium.NoSuchElementException
Once i changed it, it seems to work just fine.
Before i figured this out, i also implemented this:
for (int i = 0; i< 10; i++){
if (B_Username){
break;
} else {
try {
WebElement username = driver.findElement(By.xpath("//*[#class='gwt-TextBox']"));
B_Username = true;
username.sendKeys(usernameParm);
} catch (NoSuchElementException e) {
System.out.println("Caught exception while locating the username textbox");
System.out.println(i);
try {
Thread.sleep(500);
} catch (InterruptedException ee) {
System.out.println("Somthing happened while the thread was sleeping");
}
}
}
}
which worked fine as well.
Maybe this will help someone else another time.
Thanks to everyone who answered.