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..:)
Related
After clicking a button it redirect to another webpage associated with it. when i write code to select object in the new page i am not able select field in the new page
public class trail {
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver","C:\\Users\\Toshiba\\Desktop\\selenium\\chromedriver_win32 (2)\\chromedriver.exe");
ChromeDriver driver=new ChromeDriver();
driver.get("https://orgd8994c71.crm8.dynamics.com/main.aspx?appid=e474bdc4-6835-ed11-9db1-002248d5d2d5&pagetype=entityrecord&etn=opportunity&id=818c1b86-0565-4021-9b82-cd67fac340a9");
Thread.sleep(3000);
//maximize window screen
driver.manage().window().maximize();
//user id
WebElement username = driver.findElement(By.xpath("//input[#type='email']"));
username.sendKeys("gokul87141#gmail.com");
//user id button
WebElement next = driver.findElement(By.xpath("//input[#type='submit']")); next.click();
Thread.sleep(2000);
//password
WebElement password = driver.findElement(By.xpath("//input[#id='i0118']"));
password.sendKeys("Youaregreat!##");
Thread.sleep(2000);
//password button
WebElement next1 = driver.findElement(By.xpath("//input[#type='submit']")); next1.click();
Thread.sleep(2000);
//next button
WebElement next2 = driver.findElement(By.xpath("//input[#id='idSIButton9']")); next2.click();
Thread.sleep(22000);
//click button
WebElement next21 = driver.findElement(By.xpath("//*[#id=\"opportunity|NoRelationship|Form|new.opportunity.Command0.Command10-button\"]/span/span[2]")); next21.click();
Thread.sleep(6000);
//switch to option
ArrayList<String> tabs = new ArrayList<String> (driver.getWindowHandles());
driver.switchTo().window(tabs.get(tabs.size()-1));
//second click button
WebElement next22 = driver.findElement(By.xpath("/html/body/button")); next22.click();
Thread.sleep(6000);
}
}
After clicking the element opening a new tab you need to switch the driver to that tab. Selenium will not do it automatically.
This can be done with the following code:
ArrayList<String> tabs = new ArrayList<String> (driver.getWindowHandles());
driver.switchTo().window(tabs.get(tabs.size()-1));
After that you will be able to continue on the new tab with your test flow.
#Test
public void openWeb() throws IOException, InterruptedException {
fis = new FileInputStream(System.getProperty("user.dir")+"\\resources\\config.properties");
property = new Properties();
property.load(fis);
WebElement UsernameField = driver.findElement(By.name(property.getProperty("username_fieldName")));
WebElement PasswordField = driver.findElement(By.name(property.getProperty("password_fieldName")));
WebElement SubmitButton = driver.findElement(By.xpath(property.getProperty("submit_ButtonXpath")));
UsernameField.click();
UsernameField.sendKeys("test");
PasswordField.click();
PasswordField.sendKeys("!TEST");
SubmitButton.click();
//Testcase TC_1.01 Verify the login of "Berater" with valid credentials
WebElement Einladen = driver.findElement(By.className(property.getProperty("HomeScreen_EinladenClass")));
boolean Homescreen = Einladen.isDisplayed();
extentTest = extent.startTest("TC_1.01 - Verify the login of \"Berater\" with valid credentials");
if(Homescreen == true) {
extentTest.log(LogStatus.PASS, "Login successful for the Berater");
}
else {
extentTest.log(LogStatus.FAIL, "Login Failed for the Berater, Please refer the Screenshot");
}
Thread.sleep(1000);
Einladen.click();
driver.findElement(By.xpath(property.getProperty("Einladung_LinkXpath"))).click();
Thread.sleep(1000);
driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL + "t");
Thread.sleep(2000);
Actions actions = new Actions(driver);
actions.sendKeys(Keys.chord(Keys.LEFT_CONTROL, "v")).build().perform();
Thread.sleep(2000);
actions.sendKeys(Keys.ENTER);
Thread.sleep(2000);
}
First of all i call here chrome driver and open a link in chrome, now after performing some actions in chrome driver, i need to open firefox and open a separate link, and check the syncronization between the two. But i dont know how to open two different browsers and switch between them.
Please help.
I am unable to open google in new tab please review this code :
public static void main (String [] args) throws InterruptedException, AWTException
{
System.setProperty("webdriver.chrome.driver","C://Users//vbisht//Downloads//chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.navigate().to("https://www.google.co.in/");
Robot r = new Robot();
r.keyPress(KeyEvent.VK_CONTROL);
r.keyPress(KeyEvent.VK_T);
//Thread.sleep(10000);
//driver.navigate().refresh();*/
ArrayList<String> tabs = new ArrayList<String> (driver.getWindowHandles());
driver.switchTo().window(tabs.get(1)); //switches to new tab
driver.navigate().to("https://www.google.co.in/");
String parentWindowHandler=driver.getWindowHandle();// Store your parent window
String subWindowHandler = null;
Set<String> handles = driver.getWindowHandles(); // get all window handles
Iterator<String> iterator = handles.iterator();
while (iterator.hasNext()){
subWindowHandler = iterator.next();
}
driver.switchTo().window(subWindowHandler); // switch to popup window
Hope it will help you.
Try the below code,
The code below will open the link in new Tab.
String selectLinkOpeninNewTab = Keys.chord(Keys.CONTROL,Keys.RETURN);
driver.findElement(By.linkText("urlLink")).sendKeys(selectLinkOpeninNewTab);
The code below will open empty new Tab.
String selectLinkOpeninNewTab = Keys.chord(Keys.CONTROL,"t");
driver.findElement(By.linkText("urlLink")).sendKeys(selectLinkOpeninNewTab);
Instead of using Robot Class, You can simply open it with Javascript Executor
public static void main (String [] args) throws InterruptedException, AWTException { System.setProperty("webdriver.chrome.driver","C://Users//vbisht//Downloads//chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.navigate().to("https://www.google.co.in/");
((JavascriptExecutor) driver).executeScript("window.open()");
ArrayList<String> tabs = new ArrayList<String> (driver.getWindowHandles());
driver.switchTo().window(tabs.get(1)); //switches to new tab
driver.navigate().to("https://www.google.co.in/");
}
To open two different urls in two different TABs, once you initiate opening a new tab/window you have to induce WebDriverWait and then collect the window handles and finally iterate through the window handles and then switchTo().window(newly_opened) as per the following example:
Sample Code:
System.setProperty("webdriver.chrome.driver", "C:\\Utility\\BrowserDrivers\\chromedriver.exe.exe");
WebDriver driver = new InternetExplorerDriver();
driver.get("http://www.google.com");
String first_tab = driver.getWindowHandle();
System.out.println("Working on Google");
((JavascriptExecutor) driver).executeScript("window.open('http://facebook.com/');");
WebDriverWait wait = new WebDriverWait(driver,5);
wait.until(ExpectedConditions.numberOfWindowsToBe(2));
Set<String> s1 = driver.getWindowHandles();
Iterator<String> i1 = s1.iterator();
while(i1.hasNext())
{
String next_tab = i1.next();
if (!first_tab.equalsIgnoreCase(next_tab))
{
driver.switchTo().window(next_tab);
System.out.println("Working on Facebook");
}
}
Console Output:
Working on Google
Working on Facebook
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();
}
}
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();
}