Reputation: 1
I'm trying to text Selenium Webdriver on Mac. I was trying to have it automatically fill the search blank of Google and search. The html for the search box is following:
<input class="gLFyf" maxlength="2048" name="q" type="text" jsaction="paste:puy29d" aria-autocomplete="both" aria-haspopup="false" autocapitalize="off" autocomplete="off" autocorrect="off" role="combobox" spellcheck="false" title="Search" value="" aria-label="Search">
So, I wanted to test if I can single out the above element, which corresponds to the search box. So I had it print the following to see if it did get the element:
driver.findElements(By.className("gLFyf")).toString
However, instead of printing the actual html of the above, it printed
[[[ChromeDriver: chrome on MAC (a8470f41df7943e813ac6f77266ed33c)] -> class name: gLFyf]]
Can anyone explain to me why am I not getting the element?
Upvotes: 0
Views: 513
Reputation: 128
I didn't understand what you want to do? Do you want to check that you've found element you need?
So..
Webelement element = driver.findElement...
will throw an exception if element hasn't been found.
You can try to check the count of element found by your locator:
driver.findElements(By.className("gLFyf")).count
This will return 0 if nothing found, You can also compare it with 1 to be sure that there are no any other elements with the same classname..
if(driver.findElements(By.className("gLFyf")).count > 1){
//more than one lement found
}
Upvotes: 0
Reputation: 193058
As per your question to print the html of the Google Home Page Search Box element you can use the following solution:
Code Block:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class findElement_html {
public static void main(String[] args) {
System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("https://www.google.com/");
WebElement myElement = driver.findElement(By.name("q"));
System.out.println(myElement.getAttribute("outerHTML"));
}
}
Console Output:
<input class="gsfi lst-d-f" id="lst-ib" maxlength="2048" name="q" autocomplete="off" title="Search" value="" aria-label="Search" aria-haspopup="false" role="combobox" aria-autocomplete="list" style="border: medium none; padding: 0px; margin: 0px; height: auto; width: 100%; background: transparent url("data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw%3D%3D") repeat scroll 0% 0%; position: absolute; z-index: 6; left: 0px; outline: currentcolor none medium;" dir="ltr" spellcheck="false" type="text">
Upvotes: 1