The method implicitlyWait(long, TimeUnit) from the type WebDriver.Timeouts is deprecated what is that Mean really? - selenium

[enter image description here][1]
please resolve issue , please update the same
WebDriver driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(());
driver.get("https://accounts.google.com/signin");
driver.manage().window().maximize();

You can use like this :
WebDriver driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.get("https://accounts.google.com/signin");
driver.manage().window().maximize();
If you are using selenium 4 try this:
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
driver.manage().timeouts().scriptTimeout(Duration.ofSeconds(30));
driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(60));

Deprecated
It means the method implicitlyWait(long, TimeUnit) is no more supported and you should use an updated method to serve the same purpose.
You can change your code to
long time = 2000;
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(time));
Instead of arguments (long, TimeUnit), this method now takes arguments as Duration.ofSeconds(time)

Related

The following returns a NoSuchElementException, can you help me identify the mistake in the xpath syntax?

I can't find the error with my xpath element locator
Login using automated testing tool Selenium webdriver
driver.findElement(By.className("btn-primary")).click();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.findElement(By.xpath("//input[#aria-labelledby='vaadin-text-field-label-19']")).click();
driver.findElement(By.xpath("//input[#aria-labelledby='vaadin-text-field-label-19']")).sendKeys("xxx#gmail.com");
driver.findElement(By.xpath("//input[#type='password']")).click();
driver.findElement(By.xpath("//input[#type='password']")).sendKeys("12345678");
driver.findElement(By.className("btn-block")).click();
Try the following xpath to enter details.
public static void main(String[] args) {
// TODO Auto-generated method stub
System.setProperty("webdriver.chrome.driver", "D:\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("http://jbm4u.com/jupiter/#/signin");
driver.findElement(By.className("btn-primary")).click();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.findElement(By.xpath("//div[#class='card']//vaadin-text-field[#placeholder='Enter email address']")).click();
driver.findElement(By.xpath("//div[#class='card']//vaadin-text-field[#placeholder='Enter email address']")).sendKeys("xxx#gmail.com");
driver.findElement(By.xpath("//div[#class='card']//vaadin-password-field[#placeholder='Enter Password']")).click();
driver.findElement(By.xpath("//div[#class='card']//vaadin-password-field[#placeholder='Enter Password']")).sendKeys("12345678");
}
Browser Snapshot
Can you please try with this xpath
driver.findElement(By.xpath("//input[#placeholder='Enter email address']")).click();
driver.findElement(By.xpath("//input[#placeholder='Enter email address']")).sendKeys("xxx#gmail.com");
Please avoid referring to attributes which has dynamic indexing in your example(vaadin-text-field-label-21) 21 is dynamically generated which tend to change for every time when you refresh or for a new session.
If you're wanting to fill in the email address you need this selector:
driver.findElement(By.cssSelector("vaadin-text-field[placeholder='Enter email address']")).sendKeys("test#email.com")
The password field is:
driver.findElement(By.cssSelector("vaadin-password-field[placeholder='Enter Password']")).sendKeys("dwedewdw")
And the submit button:
driver.findElement(By.cssSelector("button[class*='btn-block']")).click()
Try and avoid Xpath whenever possible, it's too flakey. I've tested the above selectors myself.

getWindowHandles in selenium is always returning size as 1 even though multiple instances of chrome are open

I have multiple chrome windows open, but getWindowHandles in selenium is always returning size as 1 even though multiple instances of chrome are open.
here is the code:
System.setProperty("webdriver.chrome.driver", "C:/Downloads/New folder/chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://www.google.com/");
driver = new ChromeDriver();
driver.get("https://www.rediffmail.com/");
driver = new ChromeDriver();
driver.get("https://www.facebook.com/");
Set<String> ids = driver.getWindowHandles();
System.out.println(ids.size());
System.out.println(ids);
and here is the output:
1
[CDwindow-DBDAD89956F46A4DE6DC3F16E70D79A6]
any idea as to why this is happening?
or perhaps maybe im doing something incorrectly.
Im using Selenium-Java "selenium-java-3.13.0"
JDK "10.0.2"
Chrome "Version 68.0.3440.75 (Official Build) (64-bit)"
getWindowHandles() method will return the set of window handles of the current WebDriver instance.
Here you are creating different instance of each url and trying to get the WindowHandles. So, it is returning the size as 1.
Instance 1:
WebDriver driver = new ChromeDriver();
driver.get("https://www.google.com/")
Instance 2:
driver = new ChromeDriver();
driver.get("https://www.rediffmail.com/");
Instance 3:
driver = new ChromeDriver();
driver.get("https://www.facebook.com/");
you are finding the windowhandles after creating the instance 3. So, the driver object will hold the instance 3 and hence driver.getWindowHandles(); is returning as 1 .
You can try by opening multiple different tab using the same driver object and then check the size of the WindowHandles.
For the understanding purpose, Please add the below steps after the instance 3 and you can observe the getWindowHandles size.
//Latest driver instance is passed
JavascriptExecutor js=(JavascriptExecutor)driver;
String url1="https://www.rediffmail.com/";
String url2="https://www.google.com/";
js.executeScript("window.open(arguments[0])",url1); //New Tab will be opened
js.executeScript("window.open(arguments[0])",url2); //New Tab will be opened
Set<String> ids = driver.getWindowHandles();
System.out.println(ids.size());
System.out.println(ids);
Output:
3
First of all we required to find how many windows are open for this required to store getWindowHandles in String as below and hope below code will helps.
Try below code
Set<String> allWindowHandles = driver.getWindowHandles();
int count = allWindowHandles.size();
System.out.println("Number of browser windows opened on the system is : "+ count);
for (String windowHandle : allWindowHandles) {
//switch to each browser window
driver.switchTo().window(windowHandle);
String title = driver.getTitle();
//print the window handle id of each browser window
System.out.println("Window handle id of page -->"+ title +" --> is : "+windowHandle);
}

When I execute selenium script, following it browser is being closed Automatically?

I am writing selenium script and It's working perfect but when code is running then my browser is automatically closed?
public static void main(String args[])
{
System.setProperty("webdriver.chrome.driver",
"./chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("URL");
String email = "EMAil";
String password = "123";
int ELEMENT_WAIT_TIME_SEC = 60;
WebDriverWait explicitWait = new WebDriverWait(driver, ELEMENT_WAIT_TIME_SEC);
String locator = "//input[#type='email'][#aria-label='Email']";
By findBy = By.xpath(locator);
WebElement Login = explicitWait.until(ExpectedConditions.elementToBeClickable(findBy));
Login.click();
JavascriptExecutor jse = (JavascriptExecutor) driver;
jse.executeScript("arguments[0].setAttribute('aria-invalid',true);", Login);
Login.sendKeys(email);
String plocator = "//input[#type='password'][#aria-label='Password']";
By findByp = By.xpath(plocator);
WebElement Password = explicitWait.until(ExpectedConditions.elementToBeClickable(findByp));
Password.click();
jse.executeScript("arguments[0].setAttribute('aria-invalid',true);", Password);
Password.sendKeys(password);
}
Check your code. May be you used driver.close() method to close the browser.
Just comment or delete that code and the browser will not close automatically.
I got over this error by using the Code Runner extension of VS Code instead of manually running node file-path.
Works well.
This is a case of browser version compatibility issue with chrome. so update your chromedriver.exe in sync with current version of Chrome browser.

Dropdown not able to select

I am new to Selenium webDriver. Below is the code which i used to select day, Month & Year from dropdown of Facebook.
public class Facebook {
public static void main(String[] args) throws InterruptedException{
System.setProperty("webdriver.gecko.driver", "E:/Selenium/geckodriver-v0.11.1-win64/geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("https://www.facebook.com/");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.findElement(By.id("u_0_1")).sendKeys("selenium");
driver.findElement(By.id("u_0_3")).sendKeys("selenium");
driver.findElement(By.id("u_0_5")).sendKeys("1234567891");
driver.findElement(By.id("u_0_8")).sendKeys("1234567891");
Thread.sleep(500);
new Select(driver.findElement(By.xpath(".//*[#id='day']"))).selectByVisibleText("17");
Thread.sleep(500);
new Select(driver.findElement(By.id("month"))).selectByVisibleText("Aug");
new Select(driver.findElement(By.id("year"))).selectByVisibleText("1988");
driver.findElement(By.id("u_0_i")).click();
driver.findElement(By.id("u_0_e"));
Not able to select dropdown list. Please help me in this.
Thanks in advance. :)
I can only provide a lead since you have not captured all necessary details in code snippet.
WebElement selElem=driver.findElement(By.xpath(".//*[#id='day']")));
Select sel=new Select(selElem);
sel.selectByVisibleText("Aug");
You can use this code and it will work for you:
System.setProperty("webdriver.gecko.driver", "E:/Selenium/geckodriver-v0.11.1-win64/geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("https://www.facebook.com/");
driver.findElement(By.id("u_0_1")).sendKeys("selenium");
driver.findElement(By.id("u_0_3")).sendKeys("selenium");
driver.findElement(By.id("u_0_5")).sendKeys("1234567891");
driver.findElement(By.id("u_0_8")).sendKeys("1234567891");
new Select(driver.findElement(By.id("day"))).selectByVisibleText("17");
new Select(driver.findElement(By.id("month"))).selectByVisibleText("Aug");
new Select(driver.findElement(By.id("year"))).selectByVisibleText("1988");
driver.findElement(By.id("u_0_i")).click();
You have done a mistake in your code i.e. if you are using an implicit wait then you have to write it before your url launch. In your case you wrote it after url launch. Now copy and paste this code and it will definitely work.
Try this below code.
I have simply increase the wait after selecting the each value from dropdown selection.
Note:- If id is present inside the element, then use id locator, instead of using xpath locator.
new Select(driver.findElement(By.id("day"))).selectByVisibleText("17");
Thread.sleep(1000);
new Select(driver.findElement(By.id("month"))).selectByVisibleText("Aug");
Thread.sleep(1000);
new Select(driver.findElement(By.id("year"))).selectByVisibleText("1988");

Selenium remote webdriver doesn't accept desiredCapabilities

I am trying to launch a operaDriver using remoteWebdriver, and my code is:
static private IWebDriver driver;
DesiredCapabilities capability = DesiredCapabilities.Opera();
driver = new RemoteWebDriver("http://localhost:9515", capability);
however this gives me error of RemoteDriver has invalid arguments, and it specifies RemoteDriver takes OpenQA.Selenium.ICapabilities type as argument, which is violated in my code.
I am confused here. Could anyone please share some lights?
Thank you very much!
I think you can try this
It is as in java. You can try a URL class in C#
grid_url = new URL("http://localhost:9515/wd/hub");
driver = new RemoteWebDriver(grid_url, capability);