vaish
vaish

Reputation: 5

How to locate webelement?

I am not able to select departure city on spice jet website to check flight status.

Steps followed:

login to http://www.spicejet.com
click on flight status link
select departure city as goa from drop down>>
=====

Code

package TestProject;    
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.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.MarionetteDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.Wait;
import org.openqa.selenium.support.ui.WebDriverWait;

public class TestFirefox {

    public static void main(String[] args) throws InterruptedException {
        WebDriver driver;
        //driver = new FirefoxDriver();
        System.setProperty("webdriver.chrome.driver",
                "C://Selenium Jars//Chrome//chromedriver_2.31//chromedriver.exe ");
         driver = new ChromeDriver();
        driver.get("http://www.spicejet.com/?utm_source=Brand%20Campaign%20_&utm_medium=cpc_google&utm_term=O%26D&utm_campaign=Spicejet%20all%20route%20campaign&gclid=CjwKCAiAtorUBRBnEiwAfcp_Y7LCureobkUcdJZY7dqfgx0nlxyYPQeMp6w4MKkaxBEF8QzpovceDBoCXKMQAvD_BwE");
    driver.findElement(By.xpath("//*[@id=\'buttons\']/div/div/ul/li[4]/a/span[2]")).click();
    Thread.sleep(3000);
    WebDriverWait wait =new WebDriverWait (driver, 15);

     WebElement test = wait.until(ExpectedConditions.elementToBeClickable(By.id("FlifoSearchInputCompactFlightStatus_originStation")));
        //WebElement test=driver.findElement(By.id("FlifoSearchInputCompactFlightStatus_originStation"));
    test.click();   
    //test.sendKeys("GOA(GOI)");
    Select s1=new Select(test);
    s1.selectByVisibleText("Agartala (IXA)");

    }

}

================ error

Starting ChromeDriver 2.31.488763 (092de99f48a300323ecf8c2a4e2e7cab51de5ba8) on port 45746
Only local connections are allowed.
Exception in thread "main" org.openqa.selenium.TimeoutException: Timed out after 15 seconds waiting for element to be clickable: By.id: FlifoSearchInputCompactFlightStatus_originStation
Build info: version: '2.53.0', revision: '35ae25b', time: '2016-03-15 17:00:58'
System info: host: 'NPF0JVX9B', ip: '10.254.101.104', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_131'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities [{applicationCacheEnabled=false, rotatable=false, mobileEmulationEnabled=false, networkConnectionEnabled=false, chrome={chromedriverVersion=2.31.488763 (092de99f48a300323ecf8c2a4e2e7cab51de5ba8), userDataDir=C:\Users\DAHIBH~1\AppData\Local\Temp\scoped_dir11036_19151}, takesHeapSnapshot=true, pageLoadStrategy=normal, databaseEnabled=false, handlesAlerts=true, hasTouchScreen=false, version=63.0.3239.84, platform=XP, browserConnectionEnabled=false, nativeEvents=true, acceptSslCerts=true, locationContextEnabled=true, webStorageEnabled=true, browserName=chrome, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true, setWindowRect=true, unexpectedAlertBehaviour=}]
Session ID: fcc55c734bff34b0f8d58fec53bd9c02
    at org.openqa.selenium.support.ui.WebDriverWait.timeoutException(WebDriverWait.java:80)
    at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:261)
    at TestProject.TestFirefox.main(TestFirefox.java:27)
Caused by: org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"id","selector":"FlifoSearchInputCompactFlightStatus_originStation"}
  (Session info: chrome=63.0.3239.84)
  (Driver info: chromedriver=2.31.488763 (092de99f48a300323ecf8c2a4e2e7cab51de5ba8),platform=Windows NT 10.0.14393 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 286 milliseconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '2.53.0', revision: '35ae25b', time: '2016-03-15 17:00:58'
System info: host: 'NPF0JVX9B', ip: '10.254.101.104', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_131'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities [{applicationCacheEnabled=false, rotatable=false, mobileEmulationEnabled=false, networkConnectionEnabled=false, chrome={chromedriverVersion=2.31.488763 (092de99f48a300323ecf8c2a4e2e7cab51de5ba8), userDataDir=C:\Users\DAHIBH~1\AppData\Local\Temp\scoped_dir11036_19151}, takesHeapSnapshot=true, pageLoadStrategy=normal, databaseEnabled=false, handlesAlerts=true, hasTouchScreen=false, version=63.0.3239.84, platform=XP, browserConnectionEnabled=false, nativeEvents=true, acceptSslCerts=true, locationContextEnabled=true, webStorageEnabled=true, browserName=chrome, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true, setWindowRect=true, unexpectedAlertBehaviour=}]
Session ID: fcc55c734bff34b0f8d58fec53bd9c02
*** Element info: {Using=id, value=FlifoSearchInputCompactFlightStatus_originStation}
    at sun.reflect.GeneratedConstructorAccessor9.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:206)
    at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:158)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:678)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:363)
    at org.openqa.selenium.remote.RemoteWebDriver.findElementById(RemoteWebDriver.java:413)
    at org.openqa.selenium.By$ById.findElement(By.java:218)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:355)
    at org.openqa.selenium.support.ui.ExpectedConditions.findElement(ExpectedConditions.java:899)
    at org.openqa.selenium.support.ui.ExpectedConditions.access$0(ExpectedConditions.java:897)
    at org.openqa.selenium.support.ui.ExpectedConditions$7.apply(ExpectedConditions.java:205)
    at org.openqa.selenium.support.ui.ExpectedConditions$7.apply(ExpectedConditions.java:1)
    at org.openqa.selenium.support.ui.ExpectedConditions$22.apply(ExpectedConditions.java:653)
    at org.openqa.selenium.support.ui.ExpectedConditions$22.apply(ExpectedConditions.java:1)
    at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:238)
    ... 1 more

Upvotes: 0

Views: 207

Answers (3)

kripindas
kripindas

Reputation: 480

For selecting from a dropdown you don't have to click on it. Removetest.click();. And try the following , it should work.

WebElement element = driver.findElement(By.id("FlifoSearchInputCompactFlightStatus_originStation")));

Select departureCity = new Select(element);
departureCity.selectByVisibleText("Goa (GOI)");

or you can use

departureCity.selectByValue("GOI");

If exception thrown, please try putting a Thread.sleep(2000) before the WebElement declaration or use the wait methods.

Upvotes: 0

Sodium
Sodium

Reputation: 1066

Click on flight status -> opens in iframe..so you have to switchTo iframe first before locating further webelements. Modify your code like this, see I've added a line before wait...

    driver.switchTo().frame(1); //this line switchto flight-status iframe

Upvotes: 1

Naveen Kumar R B
Naveen Kumar R B

Reputation: 6398

The following code worked for me:

driver.get("http://www.spicejet.com/");
driver.findElement(By.cssSelector("a[title='Flights']>span:nth-child(2)")).click();
WebDriverWait wait = new WebDriverWait(driver, 15);
WebElement dropdown = wait.until(ExpectedConditions.elementToBeClickable(By.id("ctl00_mainContent_ddl_originStation1_CTXT")));
dropdown.sendKeys("Goa (GOI)");

Some notes on identifying elements:

  1. Start locating the elements with the HTML code the element itself instead of starting from parent HTML. This will reduce a lot of noise in locator. There are various ways of identifying element: id, name, linkText, css locator, xpath.
  2. Prefer CSS locator over Xpath.
  3. Avoid absolute xpaths, always use relative xpaths. and try to minimize the relativity i.e., as said in first point, start with the element HTML itself.

Upvotes: 0

Related Questions