i have written a basic test using selenium. The test is failing as the selenium unable to select the item from drop down list.
My select statement is not getting executed. Also i have include this select statement within iframe as this falls under it.
Can you please let me know what is the issue with my select statement.
Here is my code for southwest website:
package Default;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;
//import org.openqa.selenium.firefox.FirefoxDriver;
public class FirstWDWithoutRecording {
#Test
public void SouthWestSignUp() throws InterruptedException
{
//Open the FF/Chrome browser
//FirefoxDriver oBrw = new FirefoxDriver();
ChromeDriver oBrw = new ChromeDriver();
//Maximize Browser
oBrw.manage().window().maximize();
//Open/Launch www.southwest.com
System.setProperty("webdriver.chrome.driver", "E://chromedriver.exe");
oBrw.get("http://www.southwest.com/");
//Click on Sign up and Save
//Recognising
oBrw.findElement(By.linkText("Sign up")).click();
//oBrw.get("http://www.southwest.com/html/email/click_n_save_signup.html?clk=GFOOTER-CNS-ENROLL");
Thread.sleep(5000);
//Enter First Name
oBrw.switchTo().frame(0); //'0' as it is the only iframe on the page, the value is the index of all iframes on the page
//do your login actions
oBrw.findElement(By.xpath("//input[#id='FIRST_NAME']")).clear();
oBrw.findElement(By.xpath("//input[#id='FIRST_NAME']")).sendKeys("abc");
//Enter Last Name
oBrw.findElement(By.id("LAST_NAME")).clear();
oBrw.findElement(By.id("LAST_NAME")).sendKeys("Kish123");
//Enter Email ID
oBrw.findElement(By.id("EMAIL")).clear();
oBrw.findElement(By.id("EMAIL")).sendKeys("abc#Kish123.com");
//Selecting Home Airport
Select uiHomeAp = new Select(oBrw.findElement(By.id("HOME_AIRPORT")));
uiHomeAp.deselectByVisibleText("Atlanta, GA - ATL");
//Accepting Conditions
oBrw.findElement(By.id("IAN")).click();
//Click Submit
oBrw.findElement(By.id("submit")).click();
//after return
oBrw.switchTo().defaultContent();
}
}
As #SiKing had stated, your issue is with your deselectByVisibleText(). You want selectByVisibleText().
uiHomeAp.selectByVisibleText("Atlanta, GA - ATL");
Also wanted to point out that it might be easier if you are just getting started, to check out the getting started with selenium framework. If you're using maven, you could do:
<dependency>
<groupId>io.ddavison</groupId>
<artifactId>getting-started-with-selenium-framework</artifactId>
<version>1.2</version>
</dependency>
then your test would look like this:
#Config(browser = Browser.CHROME, url="http://www.southwest.com/")
public class FirstWDWithoutRecording extends AutomationTest {
public void southWestSignUp() {
click(By.linkText("Sign up"))
.switchToFrame(0)
.setText("input#FIRST_NAME", "abc")
.setText("input#LAST_NAME", "Kish123")
.setText("input#EMAIL", "abc#Kish123.com")
.selectOptionByText("select#HOME_AIRPORT", "Atlanta, GA - ATL")
.check("#IAN") // check the terms and conditions
.click("#submit") // form submitted
.switchToDefaultContent()
;
}
}
Related
As I am new to Selenium and Java I got stuck while testing a login page. Below is my code which I am trying to test to always it is returning Test case failed even if I am giving the correct username and password. I wondering where it is going wrong. URL is also correct while checking the equal condition. The URL I have taken post login into the site.
package Seleniumtesting;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class Selenium {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver","C:\\Users\\hp\\Desktop\\Selenium\\chromedriver_win32\\chromedriver.exe");
WebDriver d = new ChromeDriver();
d.get("https://onelogin.adityabirlacapital.com/login");
d.findElement(By.id("login-id")).sendKeys("my_username");
d.findElement(By.id("password")).sendKeys("my_password");
//d.findElement(By.xpath("//*[#id=\"password-login\"]")).click();
d.findElement(By.id("password-login")).click();
String u = d.getCurrentUrl();
if(u.equalsIgnoreCase("https://onelogin.adityabirlacapital.com/my-dashboard"))
{
System.out.println("Test case passed");
}
else
{
System.out.println("Test case failed");
}
d.close();
}
}
You can try adding delay before executing d.getCurrentUrl(). Something like TimeUnit.SECONDS.sleep(2); into your code to make sure that everything is loaded retrieving the value of the current URL after clicking login.
Other solution might be, after clicking login button via Selenium, find any element in the homepage that possibly indicates a user is successfully logged in, such as welcome messages or if there's already a logout button.
The website I am trying to automate is betting website and I have a scenario to automate a horse betting.
I am using selenium 3.0 with Java
From the site I am able to travel to horse race but unable to select Tomorrow and select the race. I tried using xpath, class and other methods but unable to click on these button.
website is
https://www.williamhill.com.au/
1 step. go to the above url
2. Select horse racing from top left corner or navigate to url (https://www.williamhill.com.au/racing?event=horseracing)
3.Click on Tomorrow I am unable to do this
4. Select on particular race from the table (unable to this too)
package automationFramework;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class horseRacing {
public static void main(String[] args) throws Exception {
String exePath = "D:\\chromedriver.exe";
System.setProperty("webdriver.chrome.driver", exePath);
WebDriver driver = new ChromeDriver();
//Launch the Online Store Website
driver.get("https://www.williamhill.com.au/");
Thread.sleep(5);
driver.manage().window().maximize();
String Title = driver.getTitle();
System.out.println(Title);
driver.findElement(By.className("MenuItem_text_N8V")).click();
Thread.sleep(25);
// driver.findElement(By.className("RaceGrid_raceTile_imG RaceGrid_raceDisabled_Q0m")).click();
driver.findElement(By.xpath("//*[#id='app']/div/div[4]/div/div/div/div[2]/div[1]/div/div[2]/div[2]/div[2]")).click();
// Print a Log In message to the screen
System.out.println("Successfully opened the website www.Store.Demoqa.com");
//Wait for 5 Sec
Thread.sleep(5);
// Close the driver
// driver.quit();
}
}
I tried to click "TOMORROW" and succeeded.
package test;
import org.openqa.selenium.By;
import org.junit.*;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class NavigateToAUrl {
#Test
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.get("https://www.williamhill.com.au/racing?event=horseracing");
driver.findElement(By.xpath(".//*[#id='app']/div/div[4]/div/div/div/div[2]/div[1]/div/div[2]/div[2]/div[2]")).click();
}
}
Dot(.) is missing in your code driver.findElement(By.xpath("//*[#id='app']/div/div[4]/div/div/div/div[2]/div[1]/div/div[2]/div[2]/div[2]")).click();
(Before //*[#id~)
I have created two sets of code contained in two seperate classes
Task Required: need to click on the 'Cash' button on the PH payment page.
Class one = simple class, simple code = code works and can click on the cash option.
Class two = setup contains page objects, framework uses different structure = code is unable to click on the cash option when reaching the payment page = 'org.openqa.selenium.StaleElementReferenceException: Element not found in the cache'
I have used the same locators within both classes but when uses the correct locator in '2' its unable to click on the 'radio' button; as listed above i get the listed error; i have tried to create bespoke methods; using loops etc and different locators but nothing works.
Working code and class:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Test;
public class TestClass {
#Test
public void test() throws InterruptedException {
//System.setProperty("webdriver.chrome.driver", "C:\\Users\\GBruno\\Desktop\\masteringSelenium\\Framework\\src\\test\\resources\\chromedriver.exe");
// WebDriver driver = new ChromeDriver();
WebDriver driver = new FirefoxDriver();
driver.get("http://www.pizzahut.co.uk");
driver.manage().window().maximize();
//click pizza button
driver.findElement(By.cssSelector("div[id='page'] [href='/menu/pizza']")).click();
//select any pizza to start order
driver.findElement(By.cssSelector("div[class='col-xxs-8 col-xs-6 col-sm-8 col-md-7 col-lg-6'] *> button")).click();
//enter postcode and find hut
Thread.sleep(2000);
driver.findElement(By.cssSelector("#ajax-postcode-txt")).sendKeys("TS1 4AG");
driver.findElement(By.cssSelector(" #get-store-btn")).click();
//click start order button
Thread.sleep(3000);
driver.findElement(By.xpath(".//*[#id='store-collection-section']/div[2]/div[4]/div[4]/div/a")).click();
//add pizza
Thread.sleep(5000);
driver.findElement(By.xpath(".//*[#id='pizza-product-list']/div/div[1]/div/div[2]/div[2]/div[3]/div/form/button")).click();
//click mini basket
driver.findElement(By.xpath("html/body/nav/div/div/div[3]/div/div[1]/div[2]/span[3]")).click();
Thread.sleep(2000);
//click checkout
driver.findElement(By.xpath(".//*[#id='divBasket']/div[1]/div/div[2]/div[2]/a")).click();
Thread.sleep(2000);
//checkout guest & enter details
driver.findElement(By.xpath(".//*[#id='frmCheckout']/div[2]/div/div[1]/a")).click();
driver.findElement(By.xpath(".//*[#id='ddlTitleSelectBoxIt']")).click();
driver.findElement(By.linkText("Mr")).click();
driver.findElement(By.xpath(".//*[#id='FirstName']")).sendKeys("Tom");
driver.findElement(By.xpath(".//*[#id='LastName']")).sendKeys("Hanks");
driver.findElement(By.xpath(".//*[#id='EmailAddress']")).sendKeys("tom_hanks12344566#mail.com");
driver.findElement(By.xpath(".//*[#id='ConfirmEmailAddress']")).sendKeys("tom_hanks12344566#mail.com");
driver.findElement(By.xpath(".//*[#id='PhoneNumber']")).sendKeys("01234 5647890");
driver.findElement(By.xpath(".//*[#id='btnFindAddress']")).click();
Thread.sleep(3000);
driver.findElement(By.xpath(".//*[#id='ddlAddressesToChooseSelectBoxItArrowContainer']")).click();
driver.findElement(By.linkText("K F C 189-191 Linthorpe Road Middlesbrough TS14AG")).click();
driver.findElement(By.xpath(".//*[#id='btnContinue']")).click();
driver.findElement(By.xpath(".//*[#id='payment-methods']/div[1]/div/label/input")).click();
}
}
Code dosnt work:
public void selectPaymentTypeAndPayForOrder() throws Exception {
Thread.sleep(3000);
driver.findElement(By.xpath(".//*[#id='payment-methods']/div[1]/div/label/input")).click();
driver.findElement(By.cssSelector(" form[id='CheckoutForm'] input[data-paymentname='Cash']")).click();
The following code resolved the issue:
List<WebElement> iframes = driver.findElements(By.tagName("iframe"));
if(iframes.size() == 0) {
Assert.fail();
} else {
// Frames present
Assert.assertTrue(true);
}
i want to save the background image of "http://yooz.ir/" to my Disk by a java code. this image changes every few days. it has a download link blue arrow, but i don't find image location to save it by code. how i can find the location of this image in url content by jsoup, htmlunit or etc?
You have the wrong url. Here is the right one: http://imgs.yooz.ir/yooz/walls/yooz-950602-2.jpg
I see this element is populated asynchronously
so you need to use some webdriver automation tool, the most common is selenium
/*
import selenium, you can do it via maven;
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-server</artifactId>
<version>2.45.0</version>
</dependency>
*/
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.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;
public class SaveImageFromUrl {
public static void main(String[] args) throws Exception {
// download chrome driver http://chromedriver.storage.googleapis.com/index.html
// or use firefox, w/e u like
System.setProperty("webdriver.chrome.driver", chromeDriverLocation);
WebDriver driver = new ChromeDriver(); // opens browsers
driver.get("http://yooz.ir/"); // redirect to site
// wait until element which contains the download link exist in page
new WebDriverWait(driver, 5).until(new ExpectedCondition<WebElement>() {
#Override
public WebElement apply(WebDriver d) {
return d.findElement(By.className("image-day__download"));
}
});
// get the link inside the element via queryselector
// https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelector
String img2download = driver.findElement(By.cssSelector(".image-day__download a")).getAttribute("href");
System.out.println("img2download = " + img2download);
//TODO download..
driver.close();
}
}
I'm trying to follow the Selenium Webdrive Basic Tutorial in the case of using HTML tables here
http://www.toolsqa.com/selenium-webdriver/handling-tables-selenium-webdriver/
The code of "Practice Exercise 1" in that page doesn't work: the problem seems to be about the xpath filter here
String sCellValue = driver.findElement(By.xpath(".//*[#id='post-1715']/div/div/div/table/tbody/tr[1]/td[2]")).getText();
and here
driver.findElement(By.xpath(".//*[#id='post-1715']/div/div/div/table/tbody/tr[1]/td[6]/a")).click();
The page used in the sample code is this one
http://www.toolsqa.com/automation-practice-table/
I've tried to change the code extracting the xpath directly form the page using Firebug and my new code is the following
package practiceTestCases;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class PracticeTables_00 {
private static WebDriver driver = null;
public static void main(String[] args) {
driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("http://www.toolsqa.com/automation-practice-table");
//Here we are storing the value from the cell in to the string variable
String sCellValue = driver.findElement(By.xpath("/html/body/div[1]/div[3]/div[2]/div/div/table/tbody/tr[1]/td[2]")).getText();
System.out.println(sCellValue);
// Here we are clicking on the link of first row and the last column
driver.findElement(By.xpath("/html/body/div[1]/div[3]/div[2]/div/div/table/tbody/tr[1]/td[6]/a")).click();
System.out.println("Link has been clicked otherwise an exception would have thrown");
driver.close();
}
}
Trying to execute the error is still
Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"xpath","selector":"/html/body/div[1]/div[3]/div[2]/div/div/table/tbody/tr[1]/td[2]"}
I'm using Eclipse Luna on Windows 7
Any suggestions? Thank you in advance ...
Cesare
Your xpath is wrong. As I see you try to get the content from the second row/first column (if you doesn't count the headers). Try the following code on http://www.toolsqa.com/automation-practice-table/ page:
/html/body/div[2]/div[3]/div[2]/div/div/table/tbody/tr[2]/td[1]
or
//*[#id='content']/table/tbody/tr[2]/td[1]
If you run getText() it will return the following value: Saudi Arabia
As they given in Practice Test scenario, that xpath has been changed. And ID or className in the xpath might be changed. so change the xpath as per updated HTML code.
Here i have changed everything:
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class PracticeTables_00 {
private static WebDriver driver = null;
public static void main(String[] args) {
driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("http://www.toolsqa.com/automation-practice-table");
// Here we are storing the value from the cell in to the string variable
String sCellValue = driver
.findElement(
By.xpath(".//*[#id='content']/table/tbody/tr[1]/td[2]"))
.getText();
System.out.println(sCellValue);
// Here we are clicking on the link of first row and the last column
driver.findElement(
By.xpath(".//*[#id='content']/table/tbody/tr[1]/td[6]/a"))
.click();
System.out
.println("Link has been clicked otherwise an exception would have thrown");
driver.close();
}
}