I tried the below code, now the month is selected perfectly but no date is selected.
Can someone help me to understand what I am missing.
driver.findElement(By.id("BE_flight_origin_date")).click() ;
Thread.sleep(2000);
WebElement element = driver.findElement(By.xpath("//div[#class='month-title']"));
JavascriptExecutor js = (JavascriptExecutor) driver;
while(!element.getText().contains(" July' 21 "))
js.executeScript("arguments[0].scrollIntoView(true);", element);
Thread.sleep(500);
List<WebElement> getCalendardays = driver.findElements(By.xpath("//tbody[#class='BE_flight_origin_date']//tr//td"));
for(int i=0;i<getCalendardays.size();i++)
{
String sdays = getCalendardays.get(i).getText();
System.out.println(sdays);
if(sdays.contentEquals(" 14 "))
{
getCalendardays.get(i).click();
System.out.println("The date has been clicked");
break;
}
}
It's actually much easier than you think. Each of the dates can be identified by an id, that is actually the date in the dd/MM/YYYY format. Create a method, like this, and call it everytime.
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;
public class AppTest {
public WebDriver driver;
#Test
public void testYatra() throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "C:\\drivers\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("https://www.yatra.com/");
// Launch Website
driver.findElement(By.id("BE_flight_origin_date")).click();
Thread.sleep(2000);
clickOnDate("14/07/2021");
}
public void clickOnDate(String id) {
WebElement date = driver.findElement(By.id(id));
JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript("arguments[0].click();", date);
}
}
Related
I am trying to click on a line under the text but its not clicking on it and also not showing error.
website name voylla.com
package newpackage;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.interactions.Actions;
public class seven {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "E:\\h\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
String baseUrl = "http://www.voylla.com";
driver.get(baseUrl);
WebElement menu = driver.findElement(By.className("dropdown"));
//WebElement submenu = driver.findElement(By.cssSelector("#main-div > div.mdl-tabs.mdl-js-tabs.mdl-js-ripple-effect.mdl-js-ripple-effect--ignore-events.is-upgraded > div > div > div > div:nth-child(7)"));
WebElement sub = driver.findElement(By.xpath("//*[#id=\"main-div\"]/div[2]/div/div/div/div[7]/a"));
Hover(driver, menu);
HoverAndClick(driver, sub, sub);
}
public static void Hover(WebDriver driver, WebElement element) {
Actions action = new Actions(driver);
action.moveToElement(element).perform();
}
public static void HoverAndClick(WebDriver driver, WebElement elementToHover, WebElement elementToClick) {
Actions action = new Actions(driver);
action.moveToElement(elementToHover).click(elementToClick).build().perform();
}
}
I have added hardcode wait thread.sleep() in my below code. How to use explicit wait. I want to wait till "username" WebElement appear. My program is working perfectly. I have already written testcases.
package com.pol.zoho.PageObjects;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.ui.WebDriverWait;
public class ZohoLoginPage {
WebDriver driver;
public ZohoLoginPage(WebDriver driver)
{
PageFactory.initElements(driver, this);
}
#FindBy(xpath=".//*[#id='lid']")
public WebElement email;
#FindBy(xpath=".//*[#id='pwd']")
public WebElement password;
#FindBy(xpath="//*[#id='signin_submit']")
public WebElement signin;
public void doLogin(String username,String userpassword)
{
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
email.sendKeys(username);
password.sendKeys(userpassword);
signin.click();
}
}
When using PageFactory in PageObjectModel if you expect the element to be loaded through some JavaScript and it might not be present on the page already you can use the Explicit Wait support with a normal locator factory as follows:
Code Block:
package com.pol.zoho.PageObjects;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class ZohoLoginPage {
WebDriver driver;
public ZohoLoginPage(WebDriver driver)
{
PageFactory.initElements(driver, this);
}
#FindBy(xpath=".//*[#id='lid']")
public WebElement email;
#FindBy(xpath=".//*[#id='pwd']")
public WebElement password;
#FindBy(xpath="//*[#id='signin_submit']")
public WebElement signin;
public void doLogin(String username,String userpassword)
{
WebElement element = new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(ZohoLoginPage.getWebElement()));
email.sendKeys(username);
password.sendKeys(userpassword);
signin.click();
}
public WebElement getWebElement()
{
return email;
}
}
You can find a detailed discussion in How to use explicit waits with PageFactory fields and the PageObject pattern
You have two options:
1- You can use implicity wait while initializing the driver.
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
2- Use explicty wait for the username field only:
WebDriverWait wait = new WebDriverWait(driver,30);
WebElement element = wait.until(
ExpectedConditions.visibilityOf(By.id(identifier)));
I try to test this website https://classpad.net/classpad/use-as-guest using Selenium. I do not know how to make the form to select visible. Please see pictures below for more detail.
[Before I click on blank area]
[After I click]
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
//import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class DriverScript {
public static WebDriver driver;
public static WebElement element;
public static void main(String[] args) {
driver = new FirefoxDriver();
driver.navigate().to("http://34.201.210.27/classpad");
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.manage().window().maximize();
driver.findElement(By.xpath("/html/body/div/div[2]/form/a[2]")).click();
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.presenceOfElementLocated(By.className("scratchpaper-detail")));
JavascriptExecutor js=(JavascriptExecutor)driver;
js.executeScript("$('.scratchpaper-detail .stickyContainer .inputForm').show()");
String element = driver.getTitle();
System.out.println(element);
}
}
I got this exception or error when I rum my script:
"Unable to locate element: *[name='password']"
I have tried with different locators but every time I get the same error.
Here is my script
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
//import org.openqa.selenium.chrome.ChromeDriver;
public class TestGmail {
public static void main(String[] args){
System.setProperty("webdriver.gecko.driver", "E:\\geckodriver-v0.16.0-win32\\geckodriver.exe");
WebDriver driver=new FirefoxDriver();
//System.setProperty("webdriver.chrome.driver", "C:\\Program Files\\chromedriver_win32\\chromedriver.exe");
//WebDriver driver=new ChromeDriver();
driver.get("https://accounts.google.com/");
driver.findElement(By.id("identifierId")).sendKeys("myAddress");
driver.findElement(By.cssSelector("span.RveJvd.snByac")).click();
driver.findElement(By.name("password")).sendKeys("myPassword");
driver.findElement(By.className("RveJvd snByac")).click();
driver.close();
}
}
Here is the code block to locate the password field and send text into the password field on the url https://accounts.google.com/
package demo;
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;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class GMAIL_LOGIN_FIREFOX_CSS
{
public static void main(String[] args)
{
System.setProperty("webdriver.gecko.driver","C:\\Utility\\BrowserDrivers\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
String url = "https://accounts.google.com/signin";
driver.get(url);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.findElement(By.cssSelector("#identifierId")).sendKeys("your_email");
driver.findElement(By.cssSelector(".ZFr60d.CeoRYc")).click();
WebElement password = driver.findElement(By.cssSelector("input[class='whsOnd zHQkBf'][type='password']"));
WebDriverWait wait = new WebDriverWait(driver, 5);
wait.until(ExpectedConditions.elementToBeClickable(password));
password.sendKeys("your_password");
}
}
Try this script it is workig fine:
WebDriver driver = new FirefoxDriver();
driver.get("https://accounts.google.com/");
driver.findElement(By.id("identifierId")).sendKeys("myAddress");
driver.findElement(By.cssSelector("span.RveJvd.snByac")).click();
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
driver.findElement(By.name("password")).sendKeys("myPassword");
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
driver.findElement(By.xpath("//span[#class='RveJvd snByac']")).click();
driver.close();
I would suggest you to use Explicit wait. Using implicit wait is a bad practice.
You can use below code something like below-
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("someid")));
Above code is in Java.
Use this code line for finding the "password" element and entering the password
Code is as follows:
driver.findElement(By.Xpath("html/body/div/div/div[2]/div[2]/form/div[2]/div/div/div[1]/div[1]/div/div[1]/div/div[1]/input")).sendKeys("Your password ")
I am new to selenium. I trying to check whether button is enable or not through isEnabled(). But when I am running this program it generating a error as "Unable to locate element" of button.
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class test
{
static WebDriver driver;
public static void main(String[] args)
{
System.setProperty("webdriver.chrome.driver", "D:\\rakesh\\software\\selenium browser\\New folder\\chromedriver.exe");
driver=new ChromeDriver();
driver.get("https://app.crossover.com/");
driver.manage().window().maximize();
JavascriptExecutor js= (JavascriptExecutor) driver;
js.executeScript("window.scrollBy(0,5500)", "");
driver.findElement(By.linkText("Available Jobs")).click();
Boolean search_btn_ele = driver.findElement(By.xpath(".//*[#id='available-jobs']/div[2]/form/div/div[3]/button")).isEnabled();
if(search_btn_ele.FALSE)
{
System.out.println("Button is disable before giving search keys");
}
else
{
System.out.println("Button is enable before giving search keys");
}
WebElement search_txtfield_ele= driver.findElement(By.xpath(".//*[#id='available-jobs']/div[2]/form/div/div[1]/div/input"));
search_txtfield_ele.sendKeys("Chief");
}
}
Use WebDriverWait to wait for the element to be present:
new WebDriverWait(driver, TimeSpan.FromSeconds(45)).Until(ExpectedConditions.ElementIsVisible((By.Id("ctl00_ContentPlaceHolder1_drp85"))));