selenium webdriver slider error - selenium

This is not working. I want the slider to move. All the buttons of the slider have the same xpath. I have need to move only the departure slider option, how to do it. I have tried the below code in two ways but nothing works.
public class task {
public static void main(String[] args) {
System.setProperty("webdriver.gecko.driver", "g://geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("https://www.cheapoair.com/");
driver.manage().deleteAllCookies();
driver.findElement(By.xpath(".//*[#id='ember746']")).sendKeys("DFW");
driver.findElement(By.xpath(".//*[#id='ember751']")).sendKeys("JFK");
driver.findElement(By.xpath(".//*[#id='owFlight']")).click();
driver.findElement(By.xpath(".//*[#id='departCalendar_0']")).click();
driver.findElement(By.xpath(".//*[#id='calendarCompId']/section/div/div[1]/ol/div[26]/li")).click();
driver.findElement(By.xpath(".//*[#id='ember751']")).sendKeys("JFK");
driver.findElement(By.xpath(".//*[#id='owFlight']")).click();
driver.findElement(By.xpath(".//*[#id='ember730']/section/form/input")).click();
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
Actions a = new Actions(driver);
//WebElement target = driver.findElement(By.xpath(".//*[#id='DivDepart']/div/div/div[1]/div[2]"));
//WebElement source = driver.findElement(By.xpath(".//*[#id='DivDepart']/div/div/div[1]/div[1]"));
//a.dragAndDrop(source, target).build().perform();
WebElement slider = driver.findElement(By.cssSelector("div[class='slider-handle round']"));
a.clickAndHold(slider).moveByOffset(30, 0).release(slider).build().perform();
}
}

Related

Drag and drop in Selenium

In https://www.globalsqa.com/demo-site/draganddrop/ I need to drag and drop picture with the text "High Tatras" into Trash section.
#Test
void task1() {
System.setProperty("webdriver.chrome.driver", "C:\\Users\\DELL\\Desktop\\New folder\\chromedriver.exe");
WebDriver webDriver = new ChromeDriver();
try {
webDriver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
webDriver.get("https://www.globalsqa.com/demo-site/draganddrop/");
WebElement from = webDriver.findElement(By.xpath("//*[#id=\"gallery\"]/li[1]"));
WebElement to = webDriver.findElement(By.xpath("//*[#id=\"trash\"]"));
Actions actions = new Actions(webDriver);
actions.dragAndDrop(from, to).build().perform();
} finally {
webDriver.close();
}
}
Both the from and to elements are inside the iframe.
So, to access these elements you need to switch into that iframe first.
Your code will look as following:
#Test
void task1() {
System.setProperty("webdriver.chrome.driver", "C:\\Users\\DELL\\Desktop\\New folder\\chromedriver.exe");
WebDriver webDriver = new ChromeDriver();
try {
webDriver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
webDriver.get("https://www.globalsqa.com/demo-site/draganddrop/");
driver.switchTo().frame(driver.findElement(By.xpath("//div[#rel-title='Photo Manager']//iframe")));
WebElement from = webDriver.findElement(By.xpath("//*[#id='gallery']/li[1]"));
WebElement to = webDriver.findElement(By.xpath("//*[#id='trash']"));
Actions actions = new Actions(webDriver);
actions.dragAndDrop(from, to).build().perform();
} finally {
webDriver.close();
}
}

Unable to select drop-down list item on Google's Advance Search page

Issue :
Unable to select the language from the drop-down list.
Please help!!
Test Steps:
Go to https://www.google.co.in/
Click 'Settings' present at bottom right.
Now click 'Advance Search'
Advance search page will be opened.
Click a drop-down whose name is 'any language'.
Drop-down list of languages will be opened.
Select any language from drop-down list say Hindi.
Code is as follows:
public class DropDown_Settings {
static String baseURL = "https://google.com";
static WebDriver driver;
public static void main(String[] args) throws InterruptedException
{
driver= new FirefoxDriver();
driver.get(baseURL);
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(2000, TimeUnit.SECONDS);
driver.findElement(By.xpath(".//*[#id='fsettl']")).click();
driver.findElement(By.xpath(".//*[#id='advsl']/a[text()='Advanced
search']")).click();
driver.findElement(By.cssSelector("#lr_button")).click();
WebElement dropDown=driver.findElement(By.id("lr_menu"));
List<WebElement> options=dropDown.findElements(By.tagName("li"));
System.out.println(options.size());
for(WebElement value:options){
if(value.getText().equals("Hindi")){
System.out.println(value.getText());
value.click();
break;
}
}
}
}
Your code works fine in Chrome, Try this code it is working in both Firefox and chrome
Try this code in Firefox and Chrome:
System.setProperty("webdriver.chrome.driver","E:/software and tools/chromedriver_win32/chromedriver.exe");
WebDriver driver=new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://www.google.co.in/");
driver.manage().timeouts().implicitlyWait(2000, TimeUnit.SECONDS);
driver.findElement(By.xpath(".//*[#id='fsettl']")).click();
driver.findElement(By.xpath(".//*[#id='advsl']/a[text()='Advanced search']")).click();
driver.findElement(By.cssSelector("#lr_button")).click();
WebElement dropDown=driver.findElement(By.id("lr_menu"));
List<WebElement> options=dropDown.findElements(By.tagName("li"));
System.out.println(options.size());
for(int i=0;i<options.size();i++){
String langName=options.get(i).getText();
if(langName.contains("Hindi"))
{
options.get(i).click();
break;
}
}
System.setProperty("webdriver.chrome.driver", "./drivers/chromedriver.exe");
WebDriver driver= new ChromeDriver();
driver.get("https://www.google.co.in/");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(2000, TimeUnit.SECONDS);
driver.findElement(By.xpath("//a[#id='fsettl']")).click();
Thread.sleep(3000);
driver.findElement(By.xpath("//span[#id='advsl']//a")).click();
Thread.sleep(10000);
driver.findElement(By.xpath("//div[#id='lr_button']")).click();
driver.manage().timeouts().implicitlyWait(2000, TimeUnit.SECONDS);
driver.findElement(By.xpath("//ul[#id='lr_menu']//li[23]")).click();
}

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) {
}
}
}

How to handle the pop-up as webpage and alert both simultaneously?

Please help me to fill this form and then going on to the website along with handling the alert.
Here is my code which is not working:
public class FirstCry {
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "D:\\Selenium\\CP-SAT\\Chromedriver\\chromedriver.exe");
WebDriver a = new ChromeDriver();
a.get("http://www.firstcry.com/");
Thread.sleep(5000L);
a.manage().window().maximize();
String k = a.getPageSource();
System.out.println(k);
WebDriverWait Wait = new WebDriverWait(a, 30);
Wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(".//*[#id='amt']/div[2]/div[1]/div[1]/div[3]/div")));
WebElement b = a.findElement(By.xpath(".//*[#id='amt']/div[2]/div[1]/div[1]/div[3]/div"));
b.click();
}
}
In your website popup present inside an iframe with id iframe_Login, you need to switch that iframe before finding the close button of the popup as below :-
//Create this prefs to handle notification popup
Map<String, Object> prefs = new HashMap<String, Object>();
prefs.put("profile.default_content_setting_values.notifications", 2);
//Initialize chrome option to add prefs
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("prefs", prefs);
//Now initialize chrome driver with chrome option to handle notification alert
WebDriver a = new ChromeDriver(options)
a.get("http://www.firstcry.com/");
a.manage().window().maximize();
WebDriverWait wait = new WebDriverWait(a, 30)
//Now find iframe and switch to it
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt("iframe_Login"));
//Now find the popup close button
WebElement b = wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("div[class = '_pop_close _pop_reg_bg']")));
b.click();
//Now switch back from frame to default content for further steps
a.switchTo().defaultContent();
//Now do your further stuff
Hope it helps..:)

Unable to control slider captcha jquery using Selenium Webdrive?

I want to record slider captcha given on our client site.
We have get this concept from other site named as http://www.fmylife.com/signup
This have slider captcha for registration
I have try to use selenium webdriver action builder
public class TestFmylife {
WebDriver driver;
Selenium selenium;
#BeforeMethod
public void startSelenium() {
driver = new FirefoxDriver();
selenium = new WebDriverBackedSelenium(driver, "http://www.fmylife.com/");
driver.manage().window().maximize();
}
#AfterMethod
public void stopSelenium() {
driver.close();
}
#Test
public void testFmylife() {
selenium.open("/");
selenium.click("link=Sign up");
selenium.waitForPageToLoad("30000");
selenium.type("name=login", "testfmylife");
selenium.type("name=pass", "123#fmylife");
selenium.type("name=passc", "123#fmylife");
selenium.type("name=mail", "testfmylife#gmail.com");
Point MyPoint= driver.findElement(By.xpath("//*[#id='bgSlider']")).getLocation();
WebElement someElement = driver.findElement(By.xpath("//*[#id='bgSlider']"));
System.out.println(MyPoint.x+"--------"+MyPoint.y);
Actions builder = new Actions(driver);
Action dragAndDrop = builder.clickAndHold(someElement).moveByOffset(MyPoint.x,(MyPoint.y + 100)).release().build();
dragAndDrop.perform();
selenium.click("css=div.form > div.ok > input[type=\"submit\"]");
}
}
But I can't move slider using this code
Help me to sort this out
I used the dragAndDropBy method of the Actions class (java.lang.Object
org.openqa.selenium.interactions.Actions) and moved the slider by 200 points horizontally . Please give the following code a try:
WebDriver driver = new FirefoxDriver();
driver.get("http://www.fmylife.com/signup");
WebElement slider = driver.findElement(By.xpath(".//*[#id='Slider']"));
Actions builder = new Actions (driver);
builder.dragAndDropBy(slider, 200, 0).build().perform();
Actions builder = new Actions(driver);
Action dragAndDrop = builder.clickAndHold(someElement)
.moveToElement(otherElement)
.release(otherElement)
.build();
dragAndDrop.perform();
more can be found at - http://code.google.com/p/selenium/wiki/AdvancedUserInteractions
You can use locator as follows -
String xto=Integer.toString(LocatorTo.getLocation().x);
String yto=Integer.toString(LocatorTo.getLocation().y);
Working code-
WebDriver driver = new InternetExplorerDriver();
driver.get("http://jqueryui.com/demos/slider/");
//Identify WebElement
WebElement slider = driver.findElement(By.xpath("//div[#id='slider']/a"));
//Using Action Class
Actions move = new Actions(driver);
Action action = move.dragAndDropBy(slider, 30, 0).build();
action.perform();
driver.quit();
Source - https://gist.github.com/2497551
If your slider is like mine
with a "slider handle" (an <a/> tag as the box with the value "5ft 5") within a "slider track" (a <div> tag as the long black bar) then the following code will in C# will work to move the slider handle a percentage along the slider track.
public void SetSliderPercentage(string sliderHandleXpath, string sliderTrackXpath, int percentage)
{
var sliderHandle = driver.FindElement(By.XPath(sliderHandleXpath));
var sliderTrack = driver.FindElement(By.XPath(sliderTrackXpath));
var width = int.Parse(sliderTrack.GetCssValue("width").Replace("px", ""));
var dx = (int)(percentage / 100.0 * width);
new Actions(driver)
.DragAndDropToOffset(sliderHandle, dx, 0)
.Build()
.Perform();
}