I cannot select from city in MakeMyTrip wesite with Selenium WebDriver - selenium

I cannot select from city in MakeMyTrip wesbite with Selenium WebDriver. It does not select the specified city. It should click on the specified city and display the city in the "from city" field.
Here is my code:
import java.util.List;
import java.util.concurrent.TimeUnit;
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.interactions.Actions;
import org.openqa.selenium.support.ui.Select;
public class Makemytrip {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "D:\\Selenium\\chromedriver_win32\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("https://www.makemytrip.com/");
String actualTitle=" ";
actualTitle=driver.getTitle();
String url=driver.getCurrentUrl();
System.out.println(url);
String expectedTitle=actualTitle;
if(actualTitle.contentEquals(expectedTitle)){
System.out.println("Test pass");
}
else{
System.out.println("Test fail");
}
WebElement roundtrip=driver.findElement(By.xpath(".//label[#class='label_text flight-trip-type']"));
roundtrip.click();
System.out.println("Select one way option");
//driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.findElement(By.xpath(".//div/section/div/div/input[#id='hp-widget__sfrom']")).click();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
List<WebElement> dd_menu=driver.findElements(By.xpath(".//ul[#id='ui-id-1']/li/div/p/span"));
for(int i=0;i<dd_menu.size();i++){
WebElement element=dd_menu.get(i);
String innerhtml=element.getAttribute("innerHTML");
if(innerhtml.contentEquals("Hyderabad, India HYD" )){
element.click();
break;
}
System.out.println("Values in list " +innerhtml);
}
}
}

Input field is kind of autosuggestive dropdown here...you should use sendKeys here...one more thing use explicit wait here...I've used Thread.sleep() here...you can use WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.id("someid"))); Modify your code with following snippet..e.g.
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "D:\\Selenium\\chromedriver_win32\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("https://www.makemytrip.com/");
String actualTitle=" ";
actualTitle=driver.getTitle();
String url=driver.getCurrentUrl();
System.out.println(url);
String expectedTitle=actualTitle;
if(actualTitle.contentEquals(expectedTitle)){
System.out.println("Test pass");
}
else{
System.out.println("Test fail");
}
WebElement roundtrip=driver.findElement(By.xpath(".//label[#class='label_text flight-trip-type']"));
roundtrip.click();
System.out.println("Select one way option");
WebElement we = driver.findElement(By.xpath(".//div/section/div/div/input[#id='hp-widget__sfrom']"));
we.clear();
Thread.sleep(3000);
we.sendKeys("hyde");
Thread.sleep(3000);
we.sendKeys(Keys.RETURN);
}

I was facing the same issue during testing. Whenever you try to open "makemytrip.com" site.
Click here to check whether you are getting the same window
Because of this menu, it block the entire WebElement to get access. To resolve this problem, follow the steps given below: -
Press Ctrl+Shift+i
Click on Inspect Pointer from the left top window of inspect tools.
Drag the pointer on window screen.
Select and copy the selected tagName's Xpath available on the inspect tool.
Here is the image of intermediate help for new learner
Paste it on your respective working IDE as shown below. Here is the working code to resolved the issue
Hope this will help you.

Please use the below code to solve the issue.
String driverPath = "C:\\geckodriver.exe";
System.setProperty("webdriver.gecko.driver", driverPath);
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.get("https://www.makemytrip.com/flights/");
Thread.sleep(2000);
Actions act = new Actions(driver);
WebElement ele= driver.findElement(By.xpath("//label[#for=\"fromCity\"]"));
act.doubleClick(ele).perform();

Related

Its showing some error at By.xpath i.e., the method xpath is undefined for the method BY

public class LaunchBrowser_TestNG {
#Test
public void LaunchBrowser() throws InterruptedException{
DesiredCapabilities caps = new DesiredCapabilities();
caps.setJavascriptEnabled(true);
caps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, "/projects/challenge/phantomjs-2.1.1-linux-x86_64/bin/phantomjs");
WebDriver driver = new PhantomJSDriver(caps);
System.out.println("PhantomJS Headless Driver launched");
// Write your script here
driver.get("https://google.com");
Thread.sleep(5000);
System.out.println ("Launch Browser is successful");
System.out.println("Page Title : " + driver.getTitle());
//Searching for "Fresco Play" in Google search
driver.findElement(By.xpath("//input[#class='gLFyf gsfi']")).sendKeys("Fresco Play");
driver.findElement(By.xpath("//input[#class='gLFyf gsfi']")).sendKeys(Keys.ENTER);
Thread.sleep(5000);
System.out.println("Page Title : " + driver.getTitle());
}
}
There is some error at By.xpath. It says -
the method xpath is undefined for the method BY.
Possibly a required import is missing. You need to add:
import org.openqa.selenium.By;
First, you need to import import org.openqa.selenium.By on top of your test.
and then like:
driver.findElement(By.xpath,value= "//input[#class='gLFyf gsfi']")).sendKeys("Fresco Play");
Try this if the above code is not working:
driver.find_element(By.XPATH,"//input[#class='gLFyf gsfi']")

Selenium Drop Down Selection In https://www.goibibo.com/hotels/ website

https://www.goibibo.com/hotels/ Image
I want to select one city using selenium, but its not working
I have tried with xpath, css selector, id, className.\
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class Goibibo {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "C:\\Selenium\\chromedriver_2\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
//Goto Url Goibibo.com
driver.get("https://www.goibibo.com/hotels/");
System.out.println("WWW.GOIBIBO.COM");
//Select Country India
driver.findElement(By.xpath("//input[#name='CountryType']")).click();
System.out.println("Selected Country India");
// Click on Search Bar
driver.findElement(By.id("downshift-1-input")).click();
System.out.println("Clicked On Search Bar");
//Select Mysore City
driver.findElement(By.xpath("//*[#id=\"downshift-1-menu\"]/div/ul/li[2]/img")).click();;
}
}
You can try with the below xpath :
//p[text()='Mysore']
or
//img[contains(#alt,'Mysore')]/following-sibling::div
in code :
System.out.println("Clicked On Search Bar");
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//p[text()='Mysore']"))).click();
System.out.println("Task has been done !");

how to get tooltip text from canvas using selenium webdriver

It will be a great help if someone help me out with below problem.
Here I am trying to get tooltip text associated with mouse hover on the first gray box in the first row. But still no luck. Please let me know in case of additional details.
Web Link :-https://www.redbus.in/search?fromCityName=Bangalore&fromCityId=122&toCityName=Goa&toCityId=210&onward=25-Aug-2017&opId=0&busType=Any#
public class RedBus {
WebDriver driver;
WebDriverWait wait;
#BeforeTest
public void run() {
System.setProperty("webdriver.gecko.driver", "C:\\geckodriver.exe");
String baseUrl="https://www.redbus.in/";
driver=new FirefoxDriver();
driver.get(baseUrl);
wait=new WebDriverWait(driver,5);
}
#Test
public void testFunctionality() {
try {
driver.findElement(By.id("src")).clear();
driver.findElement(By.id("src")).sendKeys("Bangalore");
wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("li.selected")));
driver.findElement(By.cssSelector("li.selected")).click();
driver.findElement(By.id("dest")).clear();
driver.findElement(By.id("dest")).sendKeys("Goa");
wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("li.selected")));
driver.findElement(By.cssSelector("li.selected")).click();
driver.findElement(By.xpath("//div[#id='rb-calendar_onward_cal']/table/tbody/tr[4]/td[3]")).click();
driver.findElement(By.id("search_btn")).click();
String Busname=driver.findElement(By.xpath("//ul//li[2]//div[#class='service-name']")).getText();
String departTime=driver.findElement(By.xpath("//ul//li[2]//div[#class='fl depart']")).getText();
String arrivalTime=driver.findElement(By.xpath("//ul//li[2]//div[#class='fl arrive']")).getText();
String totalSeats=driver.findElement(By.xpath("//ul//li[2]//div[#class='fl seats']//span[#class='tot-seats']")).getText();
String fare=driver.findElement(By.xpath("//ul//li[2]//div[#class='fare']")).getText();
System.out.println(Busname+" "+departTime+" "+arrivalTime+" "+totalSeats+" "+fare );
driver.findElement(By.xpath("//ul//li[2]//button[#class='view-seats button to-be-hidden']")).click();
Actions builder=new Actions(driver);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[#class='upper-canvas canvas-wrapper']//canvas")));
WebElement Upper=driver.findElement(By.xpath("//div[#class='upper-canvas canvas-wrapper']//canvas"));
Action movetoUpper=builder.moveToElement(Upper).build();
movetoUpper.perform();
System.out.println("tooltip text"+ Upper.getAttribute("title"));
System.out.println("SEAT NO:"+ Upper.getText());
}
catch(NoSuchElementException ex) {
}
}
}

Selenium Ebay Script

I have logged into Ebay and want to click the hyperlink of 'My collections' which is under "G'day [username]". The issue is now I can not find the element of 'My collections'. The error message is "Unable to locate element: (//li[#id='gh-ucol']/a)
Please refer to the below steps how I replicate:
Open Ebay via Firefox
Click Log in hyperlink
Enter the user name password then click log in button
Click "G'day [username]"
Select 'My Collection' from the droplist
This is my Selenium Java Script:
import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class KevinTest {
public static void main(String[] args) throws InterruptedException{
WebDriver driver = new FirefoxDriver();
driver.get("http://www.ebay.com.au");
driver.manage().window().maximize();
Thread.sleep(20);
driver.findElement(By.linkText("Sign in")).click();
Thread.sleep(100);
driver.findElement(By.xpath("(//input[#placeholder='Email or username'])[2]")).sendKeys("my#username");
driver.findElement(By.xpath("(//input[#placeholder='Password'])[1]")).sendKeys("mypassword");
driver.findElement(By.id("sgnBt")).click();
boolean tf;
try {
driver.findElement(By.id("errf")).getText();
tf = true;
}catch(NoSuchElementException e) {
tf = false;
}
if (tf == true) {
System.out.println("Incorrect Password");
driver.close();
}else {
System.out.println("Log in successfully");
}
driver.findElement(By.id("gh-eb-u")).click();
Thread.sleep(100);
driver.findElement(By.xpath("(//*[#id='gh-ucol']/a)")).click();
}}
Its a good practice to use implicit wait after initiating the driver.
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
Use the below id for the 'Click "G'day [username]'.
driver.findElement(By.id("gh-ug")).click();
Use the below xpath for collection.
driver.findElement(By.id("//*[#id="gh-ucol"]/a")).click();
Since we have used implicit wait you can remove all other thread.sleeps. Hope this helps. Thanks.

Stale Element reference error on finding the element -> when navigates back to previous page

My WebDriver script is simply finding elements on the page 1(Product display page) and clicking on 1st element to see if its working, then navigates back to Product display page.
It throws Stale Element Reference error and does not click the second element on the page, says element is not attached to the page.
Code is :
public class EcommerceSearchResult {
public static WebDriver driver ;
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "C:\\chromedriver_win32\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("http://store.demoqa.com/");
WebElement searchBox = driver.findElement(By.xpath("//input[contains(#class,'search')]"));
searchBox.sendKeys("iphone"+"\n");
List<WebElement> gridrow
= driver.findElements
(By.xpath(".//*[#id='grid_view_products_page_container']/div/div"));
int count = gridrow.size();
System.out.print(count);
for(int i = 0 ; i<count ; i++)
{
List<WebElement> listingelementinloop = driver.findElements(By.xpath(".//*[#id='grid_view_products_page_container']/div/div"));
System.out.println(gridrow.get(i).getText());
listingelementinloop.get(i).click();
driver.navigate().back();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
}
}
In the loop you need to replace following line:
System.out.println(gridrow.get(i).getText());
With
System.out.println(listingelementinloop.get(i).getText());
As, elements in the gridrow list will become stale once you click on 'Back' button of browser.
Alternatively, following is another way of performing the same task:
public static WebDriver driver ;
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "C:\\chromedriver_win32\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("http://store.demoqa.com/");
WebElement searchBox = driver.findElement(By.xpath("//input[contains(#class,'search')]"));
searchBox.sendKeys("iphone"+"\n");
List<WebElement> gridrow
= driver.findElements
(By.xpath(".//*[#id='grid_view_products_page_container']/div/div"));
int count = gridrow.size();
System.out.print(count);
for(int i = 1 ; i<=count ; i++)
{
WebElement element =
driver.findElement(By.xpath(".//*[#id='grid_view_products_page_container']/div/div[" + i + "]"));
System.out.println(element.getText());
element.click();
driver.navigate().back();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
}
}
Let me know, if you have any further queries.
The problem is that you are looping through a collection of elements that you collected from the page. Once you leave that page or reload it, the element references you were storing become stale, thus the error. One way to get around this is to use an index into the collection and loop over that. I've rewritten your code to make use of functions to take care of repeated actions. I've tested this code and it works.
Part of main
driver.get("http://store.demoqa.com/");
Search("iphone");
for (int i = 0; i < GetProductCount(); i++)
{
ClickProduct(i);
driver.navigate().back();
}
and supporting functions
public void Search(String searchTerm)
{
driver.findElement(By.cssSelector("input[value='Search Products']")).sendKeys(searchTerm + "\n");
}
public void ClickProduct(int index)
{
driver.findElements(By.cssSelector("h2 > a")).get(index).click();
}
public int GetProductCount()
{
return driver.findElements(By.cssSelector("h2 > a")).size();
}
Below is the solution code for your problem.Tested it in my machine and attached console screenshot.
import java.util.List;
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.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class EcommerceSearchResult {
public static WebDriver driver ;
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "C:\\chromedriver_win32\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("http://store.demoqa.com/");
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement searchBox = driver.findElement(By.xpath("//input[contains(#class,'search')]"));
searchBox.sendKeys("iphone"+"\n");
List<WebElement> gridrow = driver.findElements(By.xpath(".//*[#id='grid_view_products_page_container']/div/div"));
int count = gridrow.size();
System.out.print(count+"\n");
for(int i = 0 ; i<count ; i++)
{
WebElement listingelementinloop = driver.findElement(By.xpath(".//*[#id='grid_view_products_page_container']/div/div["+(i+1)+"]"));
System.out.println(listingelementinloop.getText());
listingelementinloop.findElement(By.xpath(".//div/a")).click();
wait.until(ExpectedConditions.urlContains("products-page/product-category"));
driver.navigate().back();
wait.until(ExpectedConditions.titleIs("iphone | Search Results | ONLINE STORE"));
}
}
}