Selenium webdriver click google search - java

I'm searching text "Cheese!" on google homepage and unsure how can I can click on the searched links after pressing the search button. For example I wanted to click the third link from top on search page then how can I find identify the link and click on it. My code so far:
package mypackage;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.WebDriverWait;
public class myclass {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "C:\\selenium-java-2.35.0\\chromedriver_win32_2.2\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("http://www.google.com");
WebElement element = driver.findElement(By.name("q"));
element.sendKeys("Cheese!");
element.submit();
//driver.close();
}
}

Google shrinks their css classes etc., so it is not easy to identify everything.
Also you have the problem that you have to "wait" until the site shows the result.
I would do it like this:
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com");
WebElement element = driver.findElement(By.name("q"));
element.sendKeys("Cheese!\n"); // send also a "\n"
element.submit();
// wait until the google page shows the result
WebElement myDynamicElement = (new WebDriverWait(driver, 10))
.until(ExpectedConditions.presenceOfElementLocated(By.id("resultStats")));
List<WebElement> findElements = driver.findElements(By.xpath("//*[#id='rso']//h3/a"));
// this are all the links you like to visit
for (WebElement webElement : findElements)
{
System.out.println(webElement.getAttribute("href"));
}
}
This will print you:
http://de.wikipedia.org/wiki/Cheese
http://en.wikipedia.org/wiki/Cheese
http://www.dict.cc/englisch-deutsch/cheese.html
http://www.cheese.com/
http://projects.gnome.org/cheese/
http://wiki.ubuntuusers.de/Cheese
http://www.ilovecheese.com/
http://cheese.slowfood.it/
http://cheese.slowfood.it/en/
http://www.slowfood.de/termine/termine_international/cheese_2013/

#Test
public void google_Search()
{
WebDriver driver;
driver = new FirefoxDriver();
driver.get("http://www.google.com");
driver.manage().window().maximize();
WebElement element = driver.findElement(By.name("q"));
element.sendKeys("Cheese!\n");
element.submit();
//Wait until the google page shows the result
WebElement myDynamicElement = (new WebDriverWait(driver, 10)).until(ExpectedConditions.presenceOfElementLocated(By.id("resultStats")));
List<WebElement> findElements = driver.findElements(By.xpath("//*[#id='rso']//h3/a"));
//Get the url of third link and navigate to it
String third_link = findElements.get(2).getAttribute("href");
driver.navigate().to(third_link);
}

There would be multiple ways to find an element (in your case the third Google Search result).
One of the ways would be using Xpath
#For the 3rd Link
driver.findElement(By.xpath(".//*[#id='rso']/li[3]/div/h3/a")).click();
#For the 1st Link
driver.findElement(By.xpath(".//*[#id='rso']/li[2]/div/h3/a")).click();
#For the 2nd Link
driver.findElement(By.xpath(".//*[#id='rso']/li[1]/div/h3/a")).click();
The other options are
By.ByClassName
By.ByCssSelector
By.ById
By.ByLinkText
By.ByName
By.ByPartialLinkText
By.ByTagName
To better understand each one of them, you should try learning Selenium on something simpler than the Google Search Result page.
Example - http://www.google.com/intl/gu/contact/
To Interact with the Text input field with the placeholder "How can we help? Ask here." You could do it this way -
# By.ByClassName
driver.findElement(By.ClassName("searchbox")).sendKeys("Hey!");
# By.ByCssSelector
driver.findElement(By.CssSelector(".searchbox")).sendKeys("Hey!");
# By.ById
driver.findElement(By.Id("query")).sendKeys("Hey!");
# By.ByName
driver.findElement(By.Name("query")).sendKeys("Hey!");
# By.ByXpath
driver.findElement(By.xpath(".//*[#id='query']")).sendKeys("Hey!");

Based on quick inspection of google web, this would be CSS path to links in page list
ol[id="rso"] h3[class="r"] a
So you should do something like
String path = "ol[id='rso'] h3[class='r'] a";
driver.findElements(By.cssSelector(path)).get(2).click();
However you could also use xpath which is not really recommended as a best practice and also JQuery locators but I am not sure if you can use them aynywhere else except inArquillian Graphene

Simple Xpath for locating Google search box is:
Xpath=//span[text()='Google Search']

public class GoogleSearch {
public static void main(String[] args) {
WebDriver driver=new FirefoxDriver();
driver.get("http://www.google.com");
driver.findElement(By.xpath("//input[#type='text']")).sendKeys("Cheese");
driver.findElement(By.xpath("//button[#name='btnG']")).click();
driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
driver.findElement(By.xpath("(//h3[#class='r']/a)[3]")).click();
driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
}
}

Most of the answers on this page are outdated.
Here's an updated python version to search google and get all results href's:
import urllib.parse
import re
from selenium import webdriver
driver.get("https://google.com/")
q = driver.find_element_by_name('q')
q.send_keys("always look on the bright side of life monty python")
q.submit();
sleep(1)
links= driver.find_elements_by_xpath("//h3[#class='r']//a")
for link in links:
url = urllib.parse.unquote(webElement.get_attribute("href")) # decode the url
url = re.sub("^.*?(?:url\?q=)(.*?)&sa.*", r"\1", url, 0, re.IGNORECASE) # get the clean url
Please note that the element id/name/class (#class='r') ** will change depending on the user agent**.
The above code used PhantomJS default user agent.

Related

Selenium Java - unnable to click on Google Search

I saw a lot of threads with this issue but none is working for me as I tried almost all possible methods and still get error "Exception in thread "main" org.openqa.selenium.ElementClickInterceptedException: element click intercepted"
System.setProperty("webdriver.chrome.driver", "C://Driver//chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://www.google.com");
driver.findElement(By.xpath("//body/div[1]/div[3]/form[1]/div[2]/div[1]/div[1]/div[1]/div[2]/input[1]")).sendKeys("568567546754");
driver.findElement(By.xpath("//form[#role='search']/div[2]/div[1]/div[3]//center/input[1]")).click();
//driver.findElement(By.xpath("//div[#class='RNNXgb']/div/div[2]/input")).sendKeys("3462355452354");
//Parent-child relationship xpath - Define xpath for parent
//body/div[1]/div[3]/form[1]/div[2]/div[1]/div[1]/div[1]/div[2]/input[1]
//driver.findElement(By.xpath("//body/div[1]/div[3]/form[1]/div[2]/div[1]/div[1]/div[1]/div[2]/input[1]"));
//driver.findElement(By.xpath("//div[#class='FPdoLc tfB0Bf']/center/input[1]")).click();
//driver.findElement(By.xpath("//iframe[contains(#src, 'consent.google.com')]")).click();;
//Thread.sleep(2000);
//driver.findElement(By.xpath("//*[#id='introAgreeButton']/span/span")).click();
//driver.findElement(By.linkText("Google Search")).click();
//driver.findElement(By.id("lst-ib")).sendKeys();
//driver.findElement(By.id("lst-ib")).sendKeys("Selenium");
//driver.findElement(By.name("btnK")).click();
//driver.manage().window().maximize();
Please try this, it will work. As Enter will do the same thing as clicking on the Search button.
driver.findElement(By.name("q")).sendKeys("568567546754" + Keys.ENTER);
Here I'm not using the xpath for search text field as I've found name attribute.
You can send the search text and Enter in one shot.
You can use Action class here.
driver.get("https://www.google.com/");
driver.findElement(By.xpath("//input[#title='Search']")).sendKeys("568567546754");
Robot ab = new Robot();
ab.keyPress(KeyEvent.VK_ENTER);
You can use this reduced Xpath : //input[#name='q'] or driver.findElement(By.name("q")).sendKeys("568567546754");
or you can try this
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("\$x("//input[#name='q']")[0].value="Car"\");
check out this code snippet
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.support.ui.ExpectedConditions;
public class SearchAction{
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver","./chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
driver.get("https://www.google.com/");
// identify element
WebElement p=driver.findElement(By.name("q"));
//enter text with sendKeys() then apply submit()
p.sendKeys("Selenium Java");
// Explicit wait condition for search results
WebDriverWait w = new WebDriverWait(driver, 5);
w.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//ul")));
p.submit();
driver.close();
}
}

Unable to enter text in dropdown

I tried the following code to enter the value BLR in a auto suggestive dropdown however although its clicking it, its now entering the text.
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class testcase2 {
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
System.setProperty("webdriver.chrome.driver", "//Users//suva//Downloads//chromedriver");
WebDriver driver = new ChromeDriver();
driver.get("https://www.makemytrip.com/");
WebElement source = driver.findElement(By.id("fromCity"));
source.click();
System.out.println(source.isEnabled());
Thread.sleep(2000);
source.sendKeys("BLR");
//source.sendKeys(Keys.ARROW_DOWN);
}
}
After you click on the default 'from' selection, there is an dropdown with another input to type.
Try like this:
driver.get("https://www.makemytrip.com/");
WebElement triggerFromDropdown = driver.findElement(By.id("fromCity"));
triggerFromDropdown.click();
WebElement fromInput = driver.findElement(By.css(".autoSuggestPlugin input[placeholder='From']"));
fromInput.sendkeys('Dubai');
There can be many reasons why it is not working. It would ave been beneficial if you could provide the DOM element as well..
However a solution would be to enter text through JavaScript Executor.
The code will be something like :-
WebElement webelement = driver.FindElement(By.id("fromCity"));
JavaScriptExecutor executor = (JavaScriptExecutor)driver;
executor.ExecuteScript("arguments[0].value='" + "BLR" + "';", webelement);
For better authenticity of the above code, I need DOM. It should work though.

Not able to click an element and redirect to new tab in selenium

I am trying to learn selenium by testing them on different websites. In this process, I am trying to work with Flipkart website. In this, I would like to give puma is search bar and trying to click one of the resultant items. But I am not able to do that using below-mentioned code. Could anyone help in solving it?
Secondly, If we click on any item, it is redirected to new-tab. How to access the new-tab elements using the same script?
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class AutomationTesting {
public static void main(String[] args) {
System.setProperty("webdriver.gecko.driver","/Users/xxxx/eclipse-workspace/seleniumTesting/lib/geckoDriver/geckodriver");
WebDriver driver = new FirefoxDriver();
driver.get("https://www.google.de");
driver.findElement(By.id("lst-ib")).sendKeys("flipkart");
driver.findElement(By.id("lst-ib")).sendKeys(Keys.ENTER);
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.partialLinkText("Flipkart")));
driver.findElement(By.partialLinkText("Flipkart")).click();
driver.findElement(By.cssSelector("._3Njdz7 [class = '_2AkmmA _29YdH8']")).click();
driver.findElement(By.xpath("//input[#class = 'LM6RPg']")).click();
driver.findElement(By.xpath("//input[#class = 'LM6RPg']")).sendKeys("Puma");
driver.findElement(By.xpath("//button[#class = 'vh79eN']")).click();
driver.findElement(By.xpath("//a[#title='Puma Men Black Wallet' and #class= '_1Nyybr _30XEf0']")).click();
}
}
You need to use the window switchTo feature.
String mainWindowHandle = driver.getWindowHandle();
ArrayList<String> wins = driver.getWindowHandles();
// You can use a for loop here, or get the assumed second window directly
driver.switchTo().window(wins.get(1));
// Test some things, then switch back
driver.close();
driver.switchTo().window(mainWindowHandle);
See http://www.seleniumhq.org/docs/03_webdriver.jsp#moving-between-windows-and-frames
You will have to inspect that entire frame. I would suggest instead of customising xpaths on your own try firebug and firepath add on of Firefox for getting xpath of entire elements. Inspect the entire frame within which the results are getting displayed then save it in some variable like below one:
List<WebElements> searchResults;
searchResults=driver.findElements(By.xpath("Your xpath"));
Then access elements of this list using index and then you can perform .click() action on the same. More on this link for capturing xpath using firebug and firepath
Question 1: During the execution of the last line in the above code I am getting the following error. Unable to locate element: //a[#title='Puma Men Black Wallet' and #class= '_1Nyybr _30XEf0']
--> Due to Lazy loading webelement not present in dom at the time when you are hiting click event on it. So to over come this you need to make the webElement on view and then hit the click event.
Please refer below code:-
driver.get("https://www.google.de");
driver.findElement(By.id("lst-ib")).sendKeys("flipkart");
driver.findElement(By.id("lst-ib")).sendKeys(Keys.ENTER);
WebDriverWait wait = new WebDriverWait(driver,60);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.partialLinkText("Flipkart")));
driver.findElement(By.partialLinkText("Flipkart")).click();
try{
driver.findElement(By.cssSelector("._3Njdz7 [class = '_2AkmmA _29YdH8']")).click();
}catch(Exception e){
System.out.println("No division");
}
driver.findElement(By.xpath("//input[#class = 'LM6RPg']")).click();
driver.findElement(By.xpath("//input[#class = 'LM6RPg']")).sendKeys("Puma");
driver.findElement(By.xpath("//button[#class = 'vh79eN']")).click();
// Thread.sleep(3000);
wait.until(ExpectedConditions.visibilityOf( driver.findElement(By.xpath("//a[#title='Puma Men Black Wallet']"))));
// getting element into view
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView();", driver.findElement(By.xpath("//*[#alt='Puma Men Black Wallet']")));
Thread.sleep(2000);
driver.findElement(By.xpath("//*[#alt='Puma Men Black Wallet']")).click();
Question 2: If that last command works then it is redirected to the new tab. So how to access the elements from the new-tab?
--> As suggested be #Damian Jansen add that code after last click event.
String mainWindowHandle = driver.getWindowHandle();
ArrayList<String> wins = driver.getWindowHandles();
for(String win : wins ){
driver.switchTo().window(win);
// other operation
System.out.println(driver.getTitle());
}
// back to old window
driver.switchTo().window(mainWindowHandle);
System.out.println(driver.getTitle());
Hope this help you :)
Use this code to reach upto puma and then select the option use below code
public static void main(String[] args) throws InterruptedException, AWTException {
System.setProperty("webdriver.chrome.driver","G:\\java programme\\SendkeysExample\\lib\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
driver.get("https://www.google.com");
driver.findElement(By.id("lst-ib")).sendKeys("flipkart");
driver.findElement(By.id("lst-ib")).sendKeys(Keys.ENTER);
driver.findElement(By.linkText("Flipkart")).click();
driver.findElement(By.className("LM6RPg")).sendKeys("Puma");
Robot rb = new Robot();
rb.keyPress(KeyEvent.VK_DOWN);
rb.keyPress(KeyEvent.VK_DOWN);
rb.keyPress(KeyEvent.VK_ENTER);
rb.keyRelease(KeyEvent.VK_DOWN);
rb.keyRelease(KeyEvent.VK_DOWN);
rb.keyRelease(KeyEvent.VK_ENTER);
Thread.sleep(2000);
/*driver.findElement(By.xpath(".//*[#id='container']/div/header/div[1]/div/div/div/div[1]/form/ul/li[2]/a"));
driver.findElement(By.className("icon-add-circle"))*/;
driver.close();
}
}

In Selenium Webdriver, click button once the text is entered in text box

I am trying to build a Selenium Automation framework for Gmail.
I have installed below tools:
JDK, Eclipse, Selenium Jars, Gradle, TestNG
I am trying to login to gmail. But i cam getting below error by the time i enter my username.
Its trying to click "Next" button before username is entered.
Can I use wait where ever required while developing framework?
Do I need to maintain any standards while calling wait.
Write any user defined wait methods.
Error:
FAILED: gmailLoginShouldBeSuccessful
org.openqa.selenium.ElementNotVisibleException: Cannot click on element (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 207 milliseconds
My Code:
#Test
public void gmailLoginShouldBeSuccessful(){
//1.Go to Gmail website
System.setProperty("webdriver.ie.driver", "C:\\Selenium_Softwares_Docs_Videos\\IEDriverServer_x64_3.1.0\\IEDriverServer.exe");
WebDriver driver = new InternetExplorerDriver();
driver.manage().deleteAllCookies();
driver.manage().window().maximize();
driver.get("http://gmail.com");
//2.Fill in username
WebElement userTextBox = driver.findElement(By.id("Email"));
userTextBox.clear();
userTextBox.sendKeys("xxxx");
//3. click on next button
WebElement nxtBtn = driver.findElement(By.id("next"));
nxtBtn.click();
//4.Fill in password
WebElement pwdTextBox = driver.findElement(By.id("Passwd-hidden"));
userTextBox.clear();
userTextBox.sendKeys("xxxxxxx");
//5.Click sign in
WebElement signBtn = driver.findElement(By.id("signIn"));
signBtn.click();
}
You can use explicit wait to achieve your requirement.
WebDriverWait wait = new WebDriverWait(yourWebDriver, 5);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//xpath_to_element")));
Webdriver will wait for 5 seconds for your element to be able to be clicked.
Use Chrome Driver instead of internet Explorer:
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;
public class TestCommons {
#Test
public void gmailLoginShouldBeSuccessful() throws InterruptedException {
// 1.Go to Gmail website
System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir") + "\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.manage().window().maximize();
driver.get("http://gmail.com");
// 2.Fill in username
driver.findElement(By.id("Email")).clear();
driver.findElement(By.id("Email")).sendKeys("vishala");
// 3. click on next button
driver.findElement(By.id("next")).click();
// 4.Fill in password
driver.findElement(By.id("Passwd")).sendKeys("vishala");
// 5.Click sign in
driver.findElement(By.id("signIn")).click();
driver.quit();
}
}
Hope this will work for you :)
Could you send a RETURN key rather than click the Sign-In button?

Java: How to Scrape Images from Amazon with Selenium?

I'm trying to scrape the 6 images on left side of page from this URL on Amazon using Selenium WebDriver:
http://www.amazon.com/EasyAcc%C2%AE-10000mAh-Brilliant-Smartphone-Bluetooth/dp/B00H9BEC8E
However, whatever I try causes an error. What I've tried so far:
I tried scraping images directly using XPATH and then extracting src using "getAttributes" method. For example, for the 1st image on page the XPATH is:
.//*[#id='a-autoid-2']/span/input
so I tried the following:
String path1 = ".//*[#id='a-autoid-2']/span/input";
String url = "http://www.amazon.com/EasyAcc%C2%AE-10000mAh-Brilliant-Smartphone-Bluetooth/dp/B00H9BEC8E";
WebDriver driver = new FirefoxDriver();
driver.get(url);
WebElement s;
s = driver.findElement(By.xpath(path1));
String src;
src = s.getAttribute("src");
System.out.println(src);
But I'm unable to find source.
Note: This problem occurs only when scraping images from certain types of products. For example, I can easily scrape images from this product using Selenium:
http://www.amazon.com/Ultimate-Unification-Diet-Health-Disease/dp/0615797806/
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class mytest {
public static void main(String[] args) {
// TODO Auto-generated method stub
String path = ".//*[#id='imgThumbs']/div[2]/img";
String url = "http://www.amazon.com/Ultimate-Unification-Diet-Health-Disease/dp/0615797806/";
WebDriver driver = new FirefoxDriver();
driver.get(url);
WebElement s;
s = driver.findElement(By.xpath(path));
String src;
src = s.getAttribute("src");
System.out.println(src);
driver.close();
}
}
This code works flawlessly. It is only when scraping certain products that there seems to be no way around it.
I tried clicking on image which causes an iframe to open but I'm unable to scrape images from this iframe either, even after switching to iframe with:
driver.switchTo().frame(IFRAMEID);
I know I can use the "screenshot" method but I'm wondering if there's a way to scrape the images directly?
Thanks
Try this code
String path = "//div[#id='imageBlock_feature_div']//span/img";
String url = "http://rads.stackoverflow.com/amzn/click/0615797806";
WebDriver driver = new FirefoxDriver();
driver.get(url);
List<WebElement> srcs;
srcs = driver.findElements(By.xpath(path));
for(WebElement src : srcs) {
System.out.println(src.getAttribute("src"));
}
driver.close();
Result
2015-01-23 12:36:14 [main]-[INFO] Opened url: http://rads.stackoverflow.com/amzn/click/B00H9BEC8E
http://ecx.images-amazon.com/images/I/41cOP3mFX3L._SX38_SY50_CR,0,0,38,50_.jpg
http://ecx.images-amazon.com/images/I/51YkMhRXqcL._SX38_SY50_CR,0,0,38,50_.jpg
http://ecx.images-amazon.com/images/I/51nSbXF%2BCTL._SX38_SY50_CR,0,0,38,50_.jpg
http://ecx.images-amazon.com/images/I/31s%2B31F%2BQmL._SX38_SY50_CR,0,0,38,50_.jpg
http://ecx.images-amazon.com/images/I/41FmTOJEOOL._SX38_SY50_CR,0,0,38,50_.jpg
http://ecx.images-amazon.com/images/I/41U6qpLJ07L._SX38_SY50_CR,0,0,38,50_.jpg
However, to get Amazon Images, I suggest you to try Amazon API https://affiliate-program.amazon.com/gp/advertising/api/detail/main.html
It's much better.

Categories