how to get page content from any site using selenium? - selenium

Here is my code it return null value but is runs well in browser console
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class google {
public static void main(String[] args) throws Exception {
WebDriver driver = new FirefoxDriver();
driver.get("https://in.yahoo.com/");
JavascriptExecutor js;
js = (JavascriptExecutor)driver;
String scriptReturningString = "";
String scriptResult = (String)js.executeScript("return document.getElementsByTagName('body')[0].innerText");
System.out.println("Text Inside:"+scriptResult);
driver.quit();
}
}

WebDriver webDriver = new FirefoxDriver();
webDriver.get("https://in.yahoo.com/");
String content = webDriver.findElement(By.tagName("body")).getText();
System.out.println(content);
This code gets page's content (not source code), stores it in variable content and then prints it in console.
If you want to get page's source code you should use webDriver.getPageSource() method.

Related

How to select second one in trending list of google webpage?

package jdjjddk;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class Htegsbn {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "C:\\Users\\hp\\Downloads\\Compressed\\Chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://www.google.com/");
driver.findElement(By.xpath("//input[#title='Search']")).click();
driver.findElement(By.xpath("//div[normalize-space()='Trending searches'][2]")).click();
}
}
This is the code I wrote, while executing it stops at displaying the trending list but not clicking the second one.
In Selenium you have FindElements:
List<Element> elements = driver.findElements
(By.xpath("//div[normalize-space()='Trending searches']"));
elements[2].click();

WebElement Click() showing NullPointerException in Selenium

I was trying Selenium automation testing in https://www.yatra.com/etw-desktop/ . My objective was to click an Image Button named 'Asia' which will redirect to another page (Images attached). I copied the full XPath and tried but I am getting a NullPointerException. Please give some suggestions since I didn't find anything wrong with my code.
package com.stackroute.SeleniumProject;
import java.util.concurrent.TimeUnit;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.FindBy;
/**
* JUnit project.
*/
public class Yatra {
public static WebDriver driver = null;
#FindBy(xpath = "/html/body/my-app//app-drawer-layout/app-header-layout/iron-pages/my-home//div/div/div/div/paper-material[2]/div[1]/div/a[2]/div[4]")
WebElement Asia;
#BeforeClass
public static void setup() {
String chromePath = System.getProperty("user.dir") + "/lib/chromedriver.exe";// directory of chrome driver
System.setProperty("webdriver.chrome.driver", chromePath);
driver = new ChromeDriver();
}
#AfterClass
public static void close() throws InterruptedException {
driver.close();
}
#Test
public void test1() throws InterruptedException {
driver.manage().window().maximize();
driver.get("https://www.yatra.com/etw-desktop/");
driver.manage().timeouts().implicitlyWait(4000, TimeUnit.MILLISECONDS);
Thread.sleep(5000);
// the error is in the below line Asia.click()
Asia.click();
driver.manage().timeouts().implicitlyWait(5000, TimeUnit.MILLISECONDS);
Thread.sleep(2000);
Assert.assertEquals("page not found", "https://www.yatra.com/etw-desktop/city-list", driver.getCurrentUrl());
}
}
Image showing the element to be clicked
Image of web page after click operation
You are using PageFactory(from page object model), so you will need to init() the webelements.
For this you will need to import
org.openqa.selenium.support.PageFactory;
An before starting the test, you will need to initialise the elements:
PageFactory.initElements(driver, this) // where you pass the driver and this class to know which webelements to start.
You can take a look here to understand the approach and another POM framework easier to understand TUTORIAL

In my Junit test case driver.findElement(By.cssSelector) not being executed

For the Junit Test case, I am trying to open a browser, navigate to my site and the enter an email in an field. Although all my commands are correct, I cant understand why does it specifically stops and shows error for line 33 i.e. driver.findElement(By.cssSelector)
package JUnitTesting;
import static org.junit.Assert.*;
import java.util.concurrent.TimeUnit;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
//import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class BasicActions {
WebDriver driver;
String BaseUrl;
#Before
public void setUp() throws Exception {
//System.setProperty("webdriver.chrome.driver", "C:\\Automation\\chromedriver_win32\\chromedriver.exe");
driver = new FirefoxDriver();
BaseUrl = "https://www.flock.co/in/indexd/";
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
}
#Test
public void test() {
driver.get(BaseUrl);
System.out.println("opening the base url");
driver.findElement(By.xpath("//div[#id='main-area']//input[#type='email']")).clear();
driver.findElement(By.cssSelector("._g-s-input>input")).sendKeys("testing#mailinator.com");
System.out.println("Entering a valid email id");
driver.findElement(By.xpath("//div[#id='main-area']/div[2]/div[2]//button[#class ='_g-s-button']")).click();
System.out.println("Redirecting to web.flock.co");
}
#After
public void tearDown() throws Exception {
driver.quit();
}
}
Appropriate syntax to find element by css class is :
driver.findElement(By.cssSelector("input._g-s-input"));
I am assuming '_g-s-input' is your css class name, if not so, please replace it with appropriate css class name.

Mouse Over issue_Error Message_Can't convert from void to WebElement

Mouse Over issue_Error Message_Can't convert from void to WebElement.
Showing the "Can't convert from void to WebElement" at WebElement creation line.
Attached screenshot.
My Code:
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import[enter image description here][1] org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.Select;
public class webelements2 {
public static void main(String[] args) throws InterruptedException
{
System.setProperty("webdriver.gecko.driver","C:\\Users\\rpremala003\\Downloads\\geckodriver-v0.14.0-win64\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("https://www.carmax.com/");
Actions builder = new Actions(driver);
WebElement menuElement = driver.findElement(By.linkText("CARS FOR SALE")).click();
builder.moveToElement(menuElement).build().perform();
driver.findElement(By.linkText("Buying from CarMax")).click();
}
}
.click() doesn't return an element and you are trying to assign the .click() result into a WebElement. Just remove the .click() and it should work without error.
WebElement menuElement = driver.findElement(By.linkText("CARS FOR SALE")).click();
should be
WebElement menuElement = driver.findElement(By.linkText("CARS FOR SALE"));

Locating iFrames and controls within them using Selenium WebDriver

I'm trying to automate www.snapdeal.com using Selenium Webdriver. I'm not able to click a web element inside an iframe as I get Element not visible exception.
Below is the code snippet
package com.snapdeal.framework.rough;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
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.interactions.Actions;
public class Lab {
public static WebDriver browser;
public static WebElement currentElement;
public static Actions actions;
public static void main(String[] args) {
//System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir")+"//src//main//resources//chromedriver.exe");
browser = new FirefoxDriver();
browser.get("http://www.snapdeal.com");
browser.manage().window().maximize();
browser.findElement(By.xpath("//*[#id='pincodeSalienceComponent']/div[2]/i")).click();
actions = new Actions(browser);
actions.moveToElement(browser.findElement(By.xpath("//*[#id='accountHeader']/div[1]"))).perform();
actions.moveToElement(browser.findElement(By.xpath("//*[#id='accountHeader']/div[2]/div[2]/div[1]/p/span[2]"))).click().build().perform();
//By executing a java script
JavascriptExecutor exe = (JavascriptExecutor) browser;
Integer numberOfFrames = Integer.parseInt(exe.executeScript("return window.length").toString());
System.out.println("Number of iframes on the page are " + numberOfFrames);
//By finding all the web elements using iframe tag
List<WebElement> iframeElements = browser.findElements(By.tagName("iframe"));
System.out.println(iframeElements.get(0));
System.out.println("The total number of iframes are " + iframeElements.size());
try{
browser.switchTo().frame(1);
}catch(Exception e){
System.out.println(e.getMessage());
}
try{
browser.findElement(By.xpath("//*[#id='login-register-modal']/div[2]/div[2]/div[2]/div")).click();
}catch(Exception e){
System.out.println(e.getMessage());
}
browser.close();
}
}
Could you please guide me on how to get this resolved?
You need to switch to iframe:
// click Mobile number and email button first an then
driver.switchTo().frame("loginIframe");
// after registration is done switch back to top frame:
driver.switchTo().defaultContent();