1

I have shared the below code please let me know the correction

import org.openqa.selenium.By;
import org.openqa.selenium.ie.InternetExplorerDriver;

public class MyClass {

    public static void main(String[] args) throws InterruptedException {
        // TODO Auto-generated method stub
        /*System.setProperty("webdriver.ie.driver","D:\\Backup\\Documents\\Automation\\drivers\\IEDriverServer.exe");
        InternetExplorerDriver driver = new InternetExplorerDriver();
        driver.get("https://www.google.com");

        WebDriverWait driverWait = new WebDriverWait(driver,50);

        driverWait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@id='lst-ib']"))).click();
        driver.findElementByXPath("//*[@id='lst-ib']").sendKeys("Make My Trip");
        driver.findElementById("_fZl").click();
        driverWait.until(ExpectedConditions.elementToBeClickable(By.linkText("MakeMyTrip, India's No 1 Travel Site | Book Hotels, Flights, Holiday ..."))).click();
        Screenshot S1 = new Screenshot();
        S1.Takescreen();*/
        String username = null;
        String password = null;
        MyClass C1 = new MyClass();
        C1.URLs(username, password);
    }

    public void URLs  (String username, String password) throws InterruptedException
    {

        System.setProperty("webdriver.ie.driver","D:\\Backup\\Documents\\Automation\\drivers\\IEDriverServer.exe");
        InternetExplorerDriver driver = new InternetExplorerDriver();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.get("https://google.com");
        driver.findElement(By.className("lst lst-tbb sbibps")).sendKeys("irctc");
        driver.findElement(By.id("_fZl")).click();
        Thread.sleep(10000);
        driver.findElement(By.linkText("IRCTC Next Generation eTicketing System")).click();
        username = driver.findElement(By.id("usernameId")).getTagName();
        password = driver.findElement(By.className("loginPassword")).getTagName();
        System.out.println(username);
        System.out.println(password);           
    }
}

Error :

Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to find element with class name == lst lst-tbb sbibps (WARNING: The server did not provide any stacktrace information) Command duration or timeout: 10.45 seconds

Guy
  • 46,488
  • 10
  • 44
  • 88
GSelenium
  • 19
  • 3

7 Answers7

2

as @Guy pointed out, you try to access 3 different classes and thus cannot use the By.className method with all classes.

I think Guy had the right approach, but it works only if your use-case is exactly like Guy understood it to be

What is your use-case?

  1. Do you want to access all elements that are either class lst, lst-tbb or sbibps OR
  2. Do you want to access all elements that are of all 3 classes lst, lst-tbb or sbibps OR
  3. Do you want to access all elements that are of class sbibps, which is a subclass of lst-tbb, which is a subclass of lst (i.e. .lst.lst-tbb.sbibps)

Solutions

  1. for the first use-case this should suffice(see Selenium Webdriver w/Java: locating elements with multiple class names with one command)

driver.findElements(By.cssSelector(".lst,.lst-tbb,.sbibps");

  1. for the second use-case I found this(see Find div element by multiple class names?)

driver.findElements(By.xpath("//*[@class='lst lst-tbb sbibps]"));

or this, if you don't know if it has more classes

driver.findElements(By.xpath("//*[contains(@class, 'lst lst-tbb sbibps')]"));

  1. for the last use-case this should work(credit to @Guy)

driver.findElements(By.cssSelector(".lst.lst-tbb.sbibps"));

Attention! I used the findElements method and not the findElement method, it results in a List and doesn't throw an exception, rather the list is just empty if the condition is not met.

Also: see Need to find element in selenium by css for reference

Community
  • 1
  • 1
Japu_D_Cret
  • 632
  • 5
  • 18
1

lst lst-tbb sbibps are actually 3 different classes. To use all of them you can use cssSelector

driver.findElement(By.cssSelector(".lst.lst-tbb.sbibps")).sendKeys("irctc");

With className you can use only one class, although none of them looks unique

driver.findElement(By.className("lst")).sendKeys("irctc");
// or
driver.findElement(By.className("lst-tbb")).sendKeys("irctc");
// or
river.findElement(By.className("sbibps")).sendKeys("irctc");
Guy
  • 46,488
  • 10
  • 44
  • 88
  • Thanks for the response, I tried with cssselector still not able to find element Exception and i also tried with finding element by "Id" and classname as you mentioned nothing helped me to find element – GSelenium Mar 30 '17 at 04:20
  • @GSelenium are you trying to type to the search box? – Guy Mar 30 '17 at 04:26
0

In Google.com , there is normally three input tags available for search , However 2 of them have property of hidden =true

<div id="gs_sc0" class="gsfi" style="background: transparent none repeat scroll 0% 0%; color: transparent; padding: 0px; position: absolute; z-index: 2; white-space: pre; visibility: hidden;"/>

<input id="gs_taif0" class="gsfi" disabled="" autocomplete="off" aria-**hidden="true"** style="border: medium none; padding: 0px; margin: 0px; height: auto; width: 100%; position: absolute; z-index: 1; background-color: transparent; color: silver; left: 0px; visibility: hidden;" dir="ltr"/>

<input id="gs_htif0" class="gsfi" disabled="" autocomplete="off" aria-hidden="true" style="border: medium none; padding: 0px; margin: 0px; height: auto; width: 100%; position: absolute; z-index: 1; background-color: transparent; color: silver; transition: all 0.218s ease 0s; opacity: 0; text-align: left; left: 0px;" dir="ltr"/>

So we have to use the input which is not hidden , in this case use

driver.findElement(By.id("lst-ib")).sendKeys("irctc");
Veena Devi
  • 26
  • 8
0

Try this below code.

driver.get("http://google.com");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);

driver.findElement(By.id("lst-ib")).sendKeys("irctc"); 

WebDriverWait wait = new WebDriverWait(driver, 5);   //use explicit wait method for 5 seconds until element not found.
wait.until(ExpectedConditions.elementToBeClickable(driver.findElement(By.xpath("//a[text()='IRCTC Next Generation eTicketing System']"))));
driver.findElement(By.xpath("//a[text()='IRCTC Next Generation eTicketing System']")).click();

WebDriverWait homepage = new WebDriverWait(driver, 5);   //use explicit wait method for 5 seconds until element not found. Because when click on link button page is getting loading so wait for few seconds.
homepage.until(ExpectedConditions.elementToBeClickable(driver.findElement(By.id("usernameId"))));

driver.findElement(By.id("usernameId")).sendKeys("Username");
driver.findElement(By.xpath("//input[@class='loginPassword'][@type='password']")).sendKeys("XXXXX");

Note:- Instead of using Thread.sleep(), use Explicit wait method.

Jainish Kapadia
  • 2,603
  • 5
  • 18
  • 29
0

Use following:

driver.findElement(By.name("q")).sendKeys("irctc");
Kushal Bhalaik
  • 3,349
  • 5
  • 23
  • 46
0

Please use following code, hope it works.

driver.findElement(By.className("gsfi")).sendKeys("irctc");
MamathaMacherla
  • 1,106
  • 7
  • 8
0

Try below code it will work in your case(I tested it in my machine).

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.ie.InternetExplorerDriver;

public class MyClass {

public static void main(String[] args) throws InterruptedException {
    String username = null;
    String password = null;
    MyClass C1 = new MyClass();
    C1.URLs(username, password);
}

public void URLs  (String username, String password) throws InterruptedException
{
    System.setProperty("webdriver.ie.driver","D:\\Backup\\Documents\\Automation\\drivers\\IEDriverServer.exe");
    InternetExplorerDriver driver = new InternetExplorerDriver();
    InternetExplorerDriver driver = new InternetExplorerDriver();
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    driver.get("https://google.com");
    driver.findElement(By.id("lst-ib")).sendKeys("irctc");
    driver.findElement(By.id("_fZl")).click();
    Thread.sleep(10000);
    driver.findElement(By.linkText("IRCTC Next Generation eTicketing System")).click();
    username = driver.findElement(By.id("usernameId")).getTagName();
    password = driver.findElement(By.className("loginPassword")).getTagName();
    System.out.println(username);
    System.out.println(password);           
}
}

Let me know if it works for you.

Akarsh
  • 967
  • 5
  • 9