How to Capture Full page screenshot of using selenium WebDriver - selenium

I have created a Selenium Full page screenshot test for Responsive tests using selenium.
But if I am going to run against the SharePoint Site then it's not taking a full-page screenshot but If I'll give any particular public site to capture the screenshots.
Here is the code snippet:
public class resTest {
#Test(groups = {"Test1"})
public void ResTest() throws InterruptedException, IOException {
System.setProperty("webdriver.gecko.driver", "C:\\browserdriver\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get(getUrl());
driver.manage().window().maximize();
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(30));
//Enter email ID
WebElement enterEmail = wait.until(ExpectedConditions.elementToBeClickable(By.name("loginfmt")));
enterEmail.sendKeys(getUsername());
WebElement clickNext = wait.until(ExpectedConditions.elementToBeClickable(By.className("win-button")));
clickNext.click();
//Enter Password
WebElement SubmitPass = wait.until(ExpectedConditions.elementToBeClickable(By.name("passwd")));
SubmitPass.sendKeys(getPassword());
//Click on Sign In button
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//input[#value='Sign in']")));
element.click();
WebElement afterNext = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//input[#value='No']")));
afterNext.click();
Thread.sleep(9000);
File src = ((FirefoxDriver)driver).getFullPageScreenshotAs(OutputType.FILE);
FileHandler.copy(src, new File(System.getProperty("user.dir")+"/Res2/screen.png"));
}
}

On a headless browser I use the following to get a screen of the full page
full_width = driver.execute_script('return document.body.parentNode.scrollWidth')
full_height = driver.execute_script('return document.body.parentNode.scrollHeight')
driver.set_window_size(full_width, full_height)
driver.save_screenshot(path)
The advantage of a headless browser window is that you can resize it to whatever size you want, even if it is larger than your screen resolution. For example, if your screen is 1920x1080 and you resize the headless browser window to 1000x5000 and take a screenshot, then the image will be 1000x5000 pixels.

Related

Using Selenium web driver, not able to take Screenshot of web page with tool-tip text displayed on it

I am trying to take screen shot of the web page showing the tool tip text, but I can only see WebElement text being highlighted after mouse hover, but tool tip is not even shown up on the web page during script execution.
Here is the piece of code, that I am trying to execute. Need help to figure out what am I missing..
public static void main(String[] args) throws InterruptedException, IOException, AWTException
{
String URL = "https://www.rediff.com";
System.setProperty("webdriver.chrome.driver", "C:\\Users\\bhara\\eclipse-workspace\\Drivers\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get(URL);
driver.manage().window().maximize();
driver.manage().timeouts().pageLoadTimeout(40, TimeUnit.SECONDS);
String WebElementXpath = "//*[#class='mailicon']";
String toolTipText ;
WebElement ToolTipElement = driver.findElement(By.xpath(WebElementXpath));
Actions act1 = new Actions(driver);
act1.moveToElement(ToolTipElement).build().perform();
Robot robot = new Robot();
Point point;
point = ToolTipElement.getLocation();
int x = point.getX();
int y = point.getY();
robot.mouseMove(x,y);
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File("screenshot.png"));
System.out.println( ToolTipElement.getAttribute("title"));
}
I don't see any such issues in your code block as such. However if you observe the HTML DOM of the WebElement:
The text Lightning fast free email is the title attribute but not the tooltip as such. Hence through using Actions class to Mouse Hover over the element won't bring up the popup. However, I made some small changes and executed you code to get the same result as follows:
Code Block:
ChromeOptions options = new ChromeOptions();
options.addArguments("start-maximized");
options.setExperimentalOption("excludeSwitches", Collections.singletonList("enable-automation"));
options.setExperimentalOption("useAutomationExtension", false);
WebDriver driver = new ChromeDriver(options);
driver.get("https://www.rediff.com/");
new Actions(driver).moveToElement(new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//a[#class='mailicon']")))).build().perform();
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File(".\\Screenshots\\Rediffmail.png"));
System.out.println(driver.findElement(By.xpath("//a[#class='mailicon']")).getAttribute("title"));
driver.quit();
Console Output:
Lightning fast free email
Screenshot:

Finding and clicking on elements in a new page by Selenium

The situation is as such -
From "something/login.php" page, i am clicking on a button which takes me to the home page "something/dashboard.php"
Trying to click on a element on the new page "something/dashboard.php" but unable to locate element.
I have then checked the currenturl with
String actualUrl = driver.getCurrentUrl();
and i am getting "something/login.php" page which means the driver is in the previous page. How do i change my focus to the new page and find elements of it?
Here is my code -
public static void main(String[] args) throws InterruptedException {
//Set chromedriver path
System.setProperty("webdriver.chrome.driver", "E:\\Tools\\New folder\\chromedriver_win32\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
String url = "something";
driver.get(url);
driver.findElement(By.id("username")).sendKeys("something");
driver.findElement(By.id("password-field")).sendKeys("something");
driver.findElement(By.id("btn3")).click();
for (String handle : driver.getWindowHandles()) {
driver.switchTo().window(handle);}
driver.findElement(By.xpath("//*[#id=\"accordion\"]/li[3]/a")).click();
Please use below code after login
driver.navigate().refresh();

How can i make selenium wait till the next page is loaded with same URL and have same fields

I have this code show below:
driver.findElement(By.id("submit")).sendKeys(Keys.ENTER);
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
driver.findElement(By.id("search-trigger")).sendKeys(Keys.ENTER);
driver.findElement(By.id("search-trigger")).sendKeys("Shampoo");
driver.findElement(By.id("search-trigger")).sendKeys(Keys.ENTER);
I want to search for a product and search option is before and after login page but here i want to do it after login page
I have used
driver.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS);
but it doesn't work and i cannot use
WebElement “”= driver.findElement(By.id(""));
as the search option is on both pages
and URL remains same after login also
Per my understanding, in short, you want to ensure user has logged in before searching for a product. However u will not be able to use WebElement “”= driver.findElement(By.id(""));
Did you try explicit wait for the presence of username/logout text...etc
WebElement element = wait.until(ExpectedConditions.elementIsVisible(By.id(>someid>)));
Below worked for me, tried searching Samsung after login in flipkart:
public static void f() throws InterruptedException
{
By signin = By.xpath(".//*[#id='container']/div/header/div[1]/div[1]/div/ul/li[9]/a");
Thread.sleep(2000);
By mobile = By.xpath("html/body/div[2]/div/div/div/div/div[2]/div/form/div[1]/input");
By password = By.xpath("html/body/div[2]/div/div/div/div/div[2]/div/form/div[2]/input");
By login = By.xpath("html/body/div[2]/div/div/div/div/div[2]/div/form/div[3]/button");
By myaccount = By.xpath(".//*[#id='container']/div/header/div[1]/div[1]/div/ul/li[8]/a");
By search = By.xpath(".//*[#id='container']/div/header/div[1]/div[2]/div/div/div[2]/form/div/div[1]/div/input");
//geko driver for firefox
System.setProperty("webdriver.gecko.driver", "path of geckodriver.exe");
System.setProperty("webdriver.chrome.driver", "path of chromedriver.exe");
driver = new ChromeDriver();
driver.get("https://www.flipkart.com/");
driver.findElement(signin).click();;
driver.findElement(mobile).sendKeys("xxxxxxxxx");
driver.findElement(password).sendKeys("xxxxxxxx");
driver.findElement(login).click();
WebDriverWait wait = new WebDriverWait (driver, 10);
wait.until(ExpectedConditions.elementToBeClickable(myaccount));
driver.findElement(search).sendKeys("samsung");
}

Popup scroll not working in selenium webdriver

I am running my script in mozilla firefox I want to scroll popup I
applied so many methods but doesn't work for me
I used keys.tab to reach that element but it was unable to enter text in that textfield using senkeys("xyz#gmail.com)
I used scroll method
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("document.getElementById('youama-email').scrollIntoView(true);");
some exception occur
3.I used Moveto element method but got exception
WebElement element = driver.findElement(By.id("youama-email"));
Actions
actions = new Actions(driver);
actions.moveToElement(element);
actions.click();
actions.perform();
// Initialize Javascript executor
JavascriptExecutor js = (JavascriptExecutor) driver;
// Scroll inside web element vertically (e.g. 100 pixel)
js.executeScript("arguments[0].scrollTop =
arguments[1];",driver.findElement(By.id("<div-id>")), 100);
Please help me to scroll and enter into the email as well as other
fields that will appear after scroll [![enter image description
here][1]][1]
[1]: http://i.stack.imgur.com/D0hqI.png
Try this code. I think what you didn't do was to wait for the element to be visible which i did. See the below code. It is running correctly.
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.get("http://wyomingvapor.com/");
driver.findElement(By.xpath(".//*[#id='header']/div/div[2]/div/a[1]")).click();
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(".//*[#id='y-to-register']/input")));
driver.findElement(By.xpath(".//*[#id='y-to-register']/input")).click();
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(".//*[#id='youama-firstname']")));
driver.findElement(By.xpath(".//*[#id='youama-firstname']")).sendKeys("xyz#gmail.com");
Thread.sleep(2000L);
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_TAB);
robot.keyRelease(KeyEvent.VK_TAB);
robot.keyPress(KeyEvent.VK_TAB);
robot.keyRelease(KeyEvent.VK_TAB);
robot.keyPress(KeyEvent.VK_X);
robot.keyRelease(KeyEvent.VK_X);
robot.keyPress(KeyEvent.VK_Y);
robot.keyRelease(KeyEvent.VK_Y);
robot.keyPress(KeyEvent.VK_Z);
robot.keyRelease(KeyEvent.VK_Z);
If you still stuck then do reply to me, Jyotsana.
Happy Learning :-)

How to handle modal dialog box in Selenium?

There is a scenario where I get the modal dialog box when I tried to login.
I am not able to handle login scenario because webdriver is not getting access to the modal dialog box. Is there any way to handle this?
See the details in below image:
public class TwitterLogin
{
static WebDriver driver;
static String baseUrl="myURL";
public static void main(String[] args)
{
driver= new FirefoxDriver();
driver.get(baseUrl);
driver.manage().window().maximize();
//******Click on Login Link**********//
WebElement LoginButton= driver.findElement(By.xpath("//button[#class='Button StreamsLogin js-login']"));
LoginButton.click();
WebElement UsernameTextbox= driver.findElement(By.xpath("//input[contains(#name,'email]')]"));
UsernameTextbox.clear();
UsernameTextbox.sendKeys("uName");
WebElement PasswdTextbox=driver.findElement(By.xpath("//input[contains(#type,'password')]"));
PasswdTextbox.clear();
PasswdTextbox.sendKeys("1234");
}
}
After clicking on Login button, I'm not able to enter username & password.
just write the following code after click on login button:
Thread.sleep(3000); //i use here wait implicit wait. Try to use explicit wait here.
driver.findElement(By.cssSelector("a.Icon.Icon--close.Icon--medium.dismiss")).click();
In Python , work with Selenium 4 and later versions:
copy selector path
wait to open new dialog
time.sleep(5)
phone_path='div:nth-child(4) > span > div:nth-child(3) > button'
log_in_phone = driver.find_element(By.CSS_SELECTOR, phone_path)
log_in_phone.click()