Thread.sleep not running [duplicate] - java

This question already has answers here:
How do I make a delay in Java?
(8 answers)
Closed 4 years ago.
I have some code with the following structure in Eclipse:
package automationFramework;
import java.util.List;
import org.openqa.selenium.support.ui.Select;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class FirefoxDropDown {
public static void main(String[] args) throws InterruptedException {
// Create a new instance of the Firefox driver
System.setProperty("webdriver.gecko.driver", "/home/gradulescu/Documents/Eclipse project/geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
// Storing the Application URL in the String variable
String url= "http://toolsqa.wpengine.com/automation-practice-form/";
driver.get(url);
//Launch the Online Store Website
Select oSdropDown = new Select((WebElement)driver.findElement(By.id("continents")));
oSdropDown.selectByIndex(1);
Thread.sleep(100);
oSdropDown.selectByVisibleText("Africa");
Thread.sleep(100);
List<WebElement> oSize = oSdropDown.getOptions();
int size = oSize.size();
for(int i=0;i<size;i++)
{
String sValue = oSdropDown.getOptions().get(i).getText();
System.out.println(sValue);
}
driver.quit();
}
}
My expectation would be that after the first code ran, 10 seconds to be waited and then the second code and some other 10 seconds. But actually the compiler runs command after command without waiting the 10 seconds I have set.
Is there any mandatory condition for it to work?
Thank you!

I don't think you're waiting long enough, try: Thread.sleep(10000);
You can also use: Thread.sleep(TimeUnit.SECONDS.toMillis(10));

Related

For loop only runs once to delete emails

I'm attempting a for loop using java/selenium to run numerous times to delete emails, but it only runs once. I'm still a beginner at this and can not figure out what it is that I'm doing wrong. Can someone please provide me with some assistance? Your help is greatly appreciated. Below is the code that I'm running:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class deleteYahooEmail {
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.gecko.driver", "/Users/lena/WebDrivers/geckodriver.exe");
WebDriver driver = new FirefoxDriver(); //launches the browser
driver.get("https://login.yahoo.com/");
Thread.sleep(2000);
driver.findElement(By.id("login-username")).sendKeys("someemailhere#yahoo.com");
Thread.sleep(2000);
driver.findElement(By.id("login-signin")).click();
Thread.sleep(2000);
driver.findElement(By.id("login-passwd")).sendKeys("passwrdhere");
Thread.sleep(2000);
driver.findElement(By.id("login-signin")).click();
Thread.sleep(2000);
driver.findElement(By.id("header-mail-button")).click();
WebDriverWait wait = new WebDriverWait(driver, 30);
for (int i = 0; i >10; i = i + 1) {
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//button[#type='button' and #data-test-id='checkbox']"))).click();
driver.findElement(By.xpath("//button[#type='button' and #data-test-id='toolbar-delete']")).click();
}
}
}
i ++ and i = i + 1 are the same.
The issue with your for-loop is that your condition statement is always false as a result the loop will never execute.
Your basically saying, whilst i is more than 10 execute the for-loop body, can you see why this will never execute? It will never run as your are initialising i with the value of 0. See my diagram.
For more info on loops see: https://www.w3schools.com/java/java_for_loop.asp

Having problems in Selenium example code java

First Error Screen
Second Error Screen
I am running the selenium example code:
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.WebDriverWait;
import static org.openqa.selenium.support.ui.ExpectedConditions.presenceOfElementLocated;
import java.time.Duration;
public class HelloSelenium {
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
try {
driver.get("https://google.com/ncr");
driver.findElement(By.name("q")).sendKeys("cheese" + Keys.ENTER);
WebElement firstResult = wait.until(presenceOfElementLocated(By.cssSelector("h3>div")));
System.out.println(firstResult.getAttribute("textContent"));
} finally {
driver.quit();
}
}
}
And getting the errors as shown in the screenshots above.
Note that the action is being performed but the last statement in the try block isn't printing the attribute of the firstElement. I understand the problem is not very easy to read but solving should be interesting.
Also I am using the geckodriver (for Firefox) in Manjaro.
And I am using gradle.
It states the error on the debug window.
WebElement firstResult = wait.until(presenceOfElementLocated(By.cssSelector("h3>div")));
An exception is thrown because there is a timeout on the "wait.until" function.
It doesn't find the element you are searching for.
Your css selector is invalid.

Selenium: Unable to upload files using for loop in Selenium

I want to upload images on a website, I am using XPath to do it. Using for loop I have tried, the loop is executed but no action is performed.
Please find my code below:
WebDriver driver=new FirefoxDriver( );
driver.get("https://www.netmeds.com/");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.findElement(By.xpath(".//*[#id='carousel-header']/div[1]/div/div[1]/div[3]/div/div[3]/button")).click();
for (int i=1;i<5;i++) {
driver.findElement(By.xpath(".//*[#id='lbl"+i+"']")).sendKeys("C:\\Users\\sys\\Downloads\\1.png");
System.out.println("upload");
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
}
Point 1 - Actually you are doing sendkeys() to wrong element i.e. a label. It should be an <input> tag with type=file then only you will be able to upload file using sendkeys() method
Point 2 - No need to mention ImplicitWait more then one place. If you have mentioned it at one place e.g. after get(URL) then it is applicable for throughout the script. Still If you required some wait then use ExplicitWait
Anyway, Use the below code for doing the same what you required :
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class Test {
public static WebDriver driver;
public static void main(String[] args) throws InterruptedException
{
System.setProperty("webdriver.chrome.driver","Resources/chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://www.netmeds.com");
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.findElement(By.xpath("//button[#title='Upload Prescription']")).click();
for(int i=1;i<5;i++)
{
driver.findElement(By.xpath("//input[#id='FileUpload"+i+"']")).sendKeys("C:\\LICENCE.jpg");
new WebDriverWait(driver, 60).until(ExpectedConditions.invisibilityOf(driver.findElement(By.id("loaderContainer"))));
System.out.println("File Upload "+ i + "Done");
}
}
}
I've tested at my end and I'm able to upload all 4 images. Let me know if you have any issue with this :)
Try this code in for loop:
for(int i = 1; i < 5; i++){
driver.findElement(By.xpath("//input[#id='FileUpload" + i + "']")).sendKeys("C:\\a.jpg");
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
System.out.println("File Upload "+ i + "successfully");
}
And it works for me:)
second I can use the webDriverwait

Unable to select the value in the drop down in selenium Web Driver for the website Goibibo

This is the code which I am using. I want to select different values from the "class" drop down. I am getting the count of the no. of drop downs as correct but the values are not getting selected under the dropdown.
package selectclasspackage;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
import org.junit.Test;
//import org.junit.Before;
import static javax.swing.JOptionPane.showMessageDialog;
import java.util.List;
import org.openqa.selenium.WebElement;
public class selectclass {
#Test
public void Sample () throws InterruptedException {
System.setProperty("webdriver.gecko.driver", "C:\\\\Users\\Rajiv\\Downloads\\geckodriver-v0.14.0-win64\\geckodriver.exe");
WebDriver driver = new FirefoxDriver ();
String URL = "https://www.goibibo.com/";
driver.get(URL);
driver.manage().window().maximize();
Thread.sleep(3000);
Select seatingclass = new Select (driver.findElement(By.id("gi_class")));
List <WebElement> elementCount = seatingclass.getOptions();
System.out.println(elementCount.size());
seatingclass.selectByVisibleText ("Premium Economy");
System.out.println("Premium Economy selected");
Thread.sleep(3000);
for (int i = 0; i < elementCount.size(); i++)
{
new Select(driver.findElement(By.id("gi_class"))).selectByIndex(i);
}
seatingclass.selectByIndex(2);
System.out.println("First Class selected");
Thread.sleep(3000);
seatingclass.selectByValue("B");
System.out.println("Business Class selected");
Thread.sleep(3000);
}
}
Why are you using for loop? selectByValue works fine as the number of options in #selnoOfAdults are fixed. The below code worked fine. It selected 10 in the #selnoOfAdults.
Select seatingclass = new Select(driver.findElement(By.id("selnoOfAdults")));
seatingclass.selectByValue("10");

Unable to click on few items on website

The website I am trying to automate is betting website and I have a scenario to automate a horse betting.
I am using selenium 3.0 with Java
From the site I am able to travel to horse race but unable to select Tomorrow and select the race. I tried using xpath, class and other methods but unable to click on these button.
website is
https://www.williamhill.com.au/
1 step. go to the above url
2. Select horse racing from top left corner or navigate to url (https://www.williamhill.com.au/racing?event=horseracing)
3.Click on Tomorrow I am unable to do this
4. Select on particular race from the table (unable to this too)
package automationFramework;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class horseRacing {
public static void main(String[] args) throws Exception {
String exePath = "D:\\chromedriver.exe";
System.setProperty("webdriver.chrome.driver", exePath);
WebDriver driver = new ChromeDriver();
//Launch the Online Store Website
driver.get("https://www.williamhill.com.au/");
Thread.sleep(5);
driver.manage().window().maximize();
String Title = driver.getTitle();
System.out.println(Title);
driver.findElement(By.className("MenuItem_text_N8V")).click();
Thread.sleep(25);
// driver.findElement(By.className("RaceGrid_raceTile_imG RaceGrid_raceDisabled_Q0m")).click();
driver.findElement(By.xpath("//*[#id='app']/div/div[4]/div/div/div/div[2]/div[1]/div/div[2]/div[2]/div[2]")).click();
// Print a Log In message to the screen
System.out.println("Successfully opened the website www.Store.Demoqa.com");
//Wait for 5 Sec
Thread.sleep(5);
// Close the driver
// driver.quit();
}
}
I tried to click "TOMORROW" and succeeded.
package test;
import org.openqa.selenium.By;
import org.junit.*;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class NavigateToAUrl {
#Test
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.get("https://www.williamhill.com.au/racing?event=horseracing");
driver.findElement(By.xpath(".//*[#id='app']/div/div[4]/div/div/div/div[2]/div[1]/div/div[2]/div[2]/div[2]")).click();
}
}
Dot(.) is missing in your code driver.findElement(By.xpath("//*[#id='app']/div/div[4]/div/div/div/div[2]/div[1]/div/div[2]/div[2]/div[2]")).click();
(Before //*[#id~)

Categories