I am a newbie trying to learn automation using the tool Selenium
. I am trying to automate this website - http://newtours.demoaut.com/
where I login and try to access this radio button (one way, round way )for flight finder. But i am getting the error Unable to locate the element
.
Tried the following.
Xpath
obtained from firebug
. Used the following Xpath composed from the html
code to locate the radio button
//*[@type='radio']//*[@value='oneway'] //*[contains(@type,'radio')] //*[contains(text(),'oneway'] //input[@type='radio' AND @value='oneway']
Also tried CSS selector
to locate the element.
driver.findElement(By.cssSelector("input[type=radio][value=oneway]"))
Tried adding wait times using implicit wait
and thread.sleep
The HTML
script for the radio button as obtained from firebug
is -
input type="radio" checked="" value="roundtrip" name="tripType"
Round Trip
input type="radio" value="oneway" name="tripType"
One Way
Given below is my code -
package gurutrial2;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Assert;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class gurutrial2
{
public static WebDriver driver;
@BeforeTest
public final void preTest() {
System.setProperty("webdriver.firefox.marionette", "C:/Users/serajendran/Downloads/geckodriver-0.10.0 (1)");
DesiredCapabilities capabilities = DesiredCapabilities.firefox();
capabilities.setCapability("marionette", true);
driver = new FirefoxDriver(capabilities);
driver.get("http://newtours.demoaut.com/");
driver.manage().window().maximize();
System.out.println(driver.getTitle());
Assert.assertEquals("Welcome: Mercury Tours", driver.getTitle());
}
@Test
public final void login() {
driver.findElement(By.name("userName")).sendKeys("invalidUN");
driver.findElement(By.name("password")).sendKeys("invalidPW");
driver.findElement(By.name("login")).click();
System.out.println("login in progress");
}
@Test
public final void flightFinder() {
WebDriverWait wait = new WebDriverWait(driver, 30);
WebElement oneWayRadioButton = wait.until(ExpectedConditions.elementToBeClickable(By.linkText("oneway")));
oneWayRadioButton.click();
System.out.println("Clicked One Way");
}
}
Any help would be deeply appreciated.
//*[@type='radio']//*[@value='oneway']
- you're looking for an element of type radio and value oneway.. this xpath look for an element of type radio that has a child element with value oneway.
//*[contains(@type,'radio')]
- you'll get multiple results for this
//*[contains(text(),'oneway']
- the text is not oneway, only the value attribute is oneway, the text contains 'One Way'
//input[@type='radio' AND @value='oneway']
- this should work if you change 'AND' to 'and'
以下解决方案在newtour网站上为我工作-
driver.findElement(By.cssSelector("input[value='oneway']")).click();
Actually the problem is in your test Methods
In TestNG
the execution of @Test
methods is in alphabetic order by default. So in your code flightFinder()
method executing before login()
So even you are using right locator to click on radio button, It will show the exception.
Solution:
Maintains your method name in alphabetic order
Use priority under @Test
annotation for the methods eg - @Test(priority = 1)
Create dependency test eg -
@Test() public final void login() { //code } @Test(dependsOnMethods={"login"}) public final void flightFinder() { //code }
Update your code as below and try -
@Test
public final void doLogin() {
driver.findElement(By.name("userName")).sendKeys("invalidUN");
driver.findElement(By.name("password")).sendKeys("invalidPW");
driver.findElement(By.name("login")).click();
System.out.println("login in progress");
}
@Test()
public final void flightFinder() {
driver.findElement(By.xpath("//input[@type='radio' and @value='oneway']")).click();
System.out.println("Clicked One Way");
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.