I think the issue is that the dropdown does not have the input type as select. Please suggest the correction in the code.
Code-
package Practice_Exercises;
import java.util.concurrent.TimeUnit;
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.support.ui.Select;
public class GoogleSearch {
public static void main(String[] args) {
WebDriver driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.get("https://www.google.com/");
//driver.findElement(By.name("q")).sendKeys("Hello");;
WebElement e= driver.findElement(By.name("q"));
e.sendKeys("Hello");
System.out.println("-------");
Select s = new Select(e);
s.selectByVisibleText("hello google");
System.out.println("-------");
driver.findElement(By.name("btnK")).click();
}
}
Console-
Starting ChromeDriver 73.0.3683.68 (47787ec04b6e38e22703e856e101e840b65afe72) on port 7637
Only local connections are allowed.
Please protect ports used by ChromeDriver and related test frameworks to prevent access by malicious code.
Apr 11, 2019 11:44:07 AM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS
-------
Exception in thread "main" org.openqa.selenium.support.ui.UnexpectedTagNameException: Element should have been "select" but was "input"
Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:25:48'
System info: host: 'IDCMBP067.local', ip: 'fe80:0:0:0:1c71:6af9:8b3c:f41c%en0', os.name: 'Mac OS X', os.arch: 'x86_64', os.version: '10.14.4', java.version: '1.8.0_101'
Driver info: driver.version: unknown
at org.openqa.selenium.support.ui.Select.<init>(Select.java:48)
at Practice_Exercises.GoogleSearch.main(GoogleSearch.java:22)
Thanks
Neha
Instructor
Ashish Thakur Replied on 12/04/2019
You cannot use select here
Every element in droplist will have a unique xpath
How will I come to know that select can be used for which type of dropdown?
try the below code.it will work fine.
driver.get("https://www.google.com/");
WebElement e1= driver.findElement(By.name("q"));
e1.sendKeys("Hello");
System.out.println("-------");
String e="hello google";
Thread.sleep(2000);
List<WebElement> search=driver.findElements(By.xpath("//*[@id='tsf']/div[2]/div/div[2]/div[2]/ul/li/div/div[1]/div/span"));
for(int i=0;i<search.size();i++) {
System.out.println(search.get(i).getText());
if(e.equals(search.get(i).getText())) {
Thread.sleep(2000);
search.get(i).click();
System.out.println("-------");
}
}
Instructor
Ashish Thakur Replied on 12/04/2019
Please let us know if Avinash's suggestion doesn't work
The code suggested by Avinash worked fine. But I want to know that how can we find out select can be used for which type of dropdown.
Instructor
Ashish Thakur Replied on 12/04/2019
Neha, you need to analyze the HTML code. How the data is being represented. Sometimes it can be handled directly, sometimes it can be handled using Select statement..
For example, take the current application, analyze the HTML structure of the element you were trying to access.
I googled a lot and tried also.for select you need input type as select and tag name starts as Select.
Thanks for the information.