Unable to control slider captcha jquery using Selenium Webdrive? - selenium

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();
}

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();
}
}

Selenium: How to drag and drop in Chrome using C#

The below code works in Firefox but not in Chrome. From what I could find online it seems that Actions.DragAndDrop does not work with Chrome. I am trying to move SVG elements.
Is there an alternative?
var action = new Actions(driver);
action
.DragAndDropToOffset(middle, 100, 100)
.Build()
.Perform();
Selenium.Webdriver: v3.141.0
Selenium.Webdriver.ChromeDriver: v76.0.3809.68
Selenium.Firefox.Webdriver: v0.24.0
Chrome: Version 76.0.3809.100 (Official Build) (64-bit)
This is what I use in Chrome. Remember when using drag and drop you need a starting element to click, and a second element where you are going to drop it.
Call:
var ele1 = Driver.FindElement(By.Xpath("//button[#class='cz2__images__image-content cz2__images--draggable']"));
var ele2 = Driver.FindElement(By.Xpath("//button[#class='Destination']"));
DragAndDrop(ele1, ele2);
Method:
public static void DragAndDrop(IWebElement element1, IWebElement element2)
{
WaitForElementEnabled(element1);
WaitForElementEnabled(element2);
var builder = new Actions(Driver);
var dragAndDrop = builder.ClickAndHold(element1).MoveToElement(element2).Release(element1).Build();
dragAndDrop.Perform();
}
or....
public static void test ()
{
var test1 = _webDriver.FindElement(By.Id("myid"));
var test2 = _webDriver.FindElement(By.Id("myid2"));
Actions builder1 = new Actions(_webDriver);
IAction dragAndDrop1 = builder1.ClickAndHold(test1).MoveToElement(test2).Release(test1).Build();
dragAndDrop1.Perform();
}
public void DragAndDropItem(IWebElement from, IWebElement to)
{
Actions action = new Actions(_driver);
action.DragAndDrop(from, to).Build().Perform();
}

selenium webdriver slider error

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();
}
}

Selenium xpath getting changed during run time

Scenario - Valid login to www.homeshop18.com and then from the Digital Menu select "Samsung".
The results are displayed and now I need to choose another brand - Micromax from the Brand section (displayed at the left side of the page)
which requires scrolling and selecting Micromax.
Issue:
Though the xpath of Micromax is correct which is //*[#id='filter_1Option_12']//div[#class='ez-checkbox'] but I see during run time of the script - some other brand is getting selected instead of micromax.
Kindly advise.
//Class for valid login to www.homeshop18.com
public class HomeShop_Login_Test
{
#FindBy(xpath="//a[#id='signInHeaderLink']") WebElement SignIn_Link;
#FindBy(xpath=".//input[#id='emailId']") WebElement Email;
#FindBy(xpath=".//input[#id='existing_user_radio']") WebElement Existing_User_Radio;
#FindBy(xpath=".//input[#id='new_user_radio']") WebElement New_User_Radio;
#FindBy(xpath=".//input[#id='password']") WebElement Password;
#FindBy(xpath=".//a[#id='signin']") WebElement SignIn_Button;
#FindBy(xpath="//a[#title='Close']") WebElement Close_Home;
public void Login_Valid()
{
WebDriverWait wait = new WebDriverWait(driver, 30);
WebElement SignIn_Link = wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//a[#id='signInHeaderLink']")));
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("arguments[0].click()", SignIn_Link);
Email.sendKeys("xxx#gmail.com");
boolean selected;
selected = New_User_Radio.isSelected();
if(selected)
{
Existing_User_Radio.click();
}
Password.sendKeys("xxx");
SignIn_Button.click();
}
//Class to choose Samsung from Digital menu
public class Browse_Samsung_Mobile
{
#FindBy(xpath="//*[#id='CategoryMenu1']//a[text()='Digital']") WebElement Digital_Menu;
#FindBy(xpath="//*[#id='CategoryMenu1']//a[#title='Samsung']") WebElement Samsung_SubMenu;
#FindBy(xpath="//*[#id='filter_1Option_19']//span[#class='selected_filter_img']") WebElement Micromax;
public void Browse_Samsung()
{
WebDriverWait wait = new WebDriverWait(driver, 30);
Actions act = new Actions(driver);
act.moveToElement(Digital_Menu).perform();
act.click(wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[#id='CategoryMenu1']//a[#title='Samsung']")))).build().perform();
//WebElement Micromax = wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[#id='filter_1Option_12']//span[#class='selected_filter_img']")));
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("arguments[0].click()", Micromax);
}
}
//class to call above two classes
public class Validate_Browse_Samsung_Mobile
{
WebDriver driver;
#Test
public void Validate_Browse()
{
driver = BrowserFactory.getBrowser("Firefox");
driver.get(DataProviderFactory.getConfig().getURL());
HomeShop_Login_Test login = PageFactory.initElements(driver, HomeShop_Login_Test.class);
login.Login_Valid();
Browse_Samsung_Mobile browse = PageFactory.initElements(driver, Browse_Samsung_Mobile.class);
browse.Browse_Samsung();
}
}
You should try with their name using title attribute as below :-
WebElement micromax = wait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("a[title ~= "Micromax"] input")));
You should use following XPath to choose proper check-box:
//a[#title="GSM Mobile Phones - Micromax"]/div/input

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..:)