How to resolve NoSuchElementException in HtmlUnitDriver - java

I am trying to run my script in background using HtmlUnitDriver but it throwing Nosuchelementexection every time,but it getting the current url.
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
public class Example{
public static void main(String[] args) {
WebDriver driver = new HtmlUnitDriver(true);
driver.get("http://www.google.com");
String url=driver.getCurrentUrl();
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
System.out.println(url);
String Text=driver.findElement(By.id("gbqfba")).getText();
System.out.println(Text);
driver.findElement(By.xpath("//*[#id='gbqfba']")).getSize().getHeight();
driver.findElement(By.xpath("//*[#id='gbqfba']")).getSize().getWidth();
driver.findElement(By.xpath("//*[#id='gbqfba']")).click();
driver.quit();
}
}

Did you try adding a Thread.sleep to see if it is a timing problem?

Related

clear method not working with testng but working without it

.clear() in my test script is not working with testng version 6.14.2 but when i am running the same code without testng the clear method is working as expected.
i am running the code as mentioned below:
driver.findElement(By.id("email")).clear();
But this loc is not performing any action.
Blockquote
clear() is working for me. refer the following snippet
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
public class Testngexample {
private WebDriver driver;
#BeforeClass
public void setUp() {
System.setProperty("webdriver.chrome.driver","Add chromedriver path chromedriver.exe");
driver = new ChromeDriver();
}
#AfterClass
public void tearDown() {
driver.quit();
}
#Test
public void GoogleEarch() throws InterruptedException {
driver.get("https://www.google.com/");
driver.manage().window().maximize();
driver.findElement(By.name("q")).click();
driver.findElement(By.name("q")).sendKeys("testing");
Thread.sleep(5000);
driver.findElement(By.name("q")).clear();
Thread.sleep(5000);
driver.close();
}
}

Getting error while running my first selenium code

I just started learning Selenium.
This is my code:
package sele;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Abc {
public static void main(String[] args)
{
WebDriver driver = new FirefoxDriver();
driver.close();
}
}
This is the exception:
at org.openqa.selenium.remote.service.DriverService$Builder.build(DriverService.java:296)
at org.openqa.selenium.firefox.FirefoxDriver.createCommandExecutor(FirefoxDriver.java:277)
at org.openqa.selenium.firefox.FirefoxDriver.(FirefoxDriver.java:247)
at org.openqa.selenium.firefox.FirefoxDriver.(FirefoxDriver.java:242)
at org.openqa.selenium.firefox.FirefoxDriver.(FirefoxDriver.java:238)
at org.openqa.selenium.firefox.FirefoxDriver.(FirefoxDriver.java:127)
at sele.Abc.main(Abc.java:10)
I am not sure if it is the same in java but in C# you use:
driver.Quit();
at the end of your code. Some use
driver.Dispose();
This will work as well.
Look here for more Help.

How to get INPUT field data in eclipse?

I am tried to using following syntaxes but, I didn't get result.
package demo_package;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.testng.Assert;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Class {
public WebDriver driver= new FirefoxDriver();
#BeforeTest
public void beforetest() {
driver.get("URL");
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.manage().window().maximize();
}
#Test
public void Login() {
driver.findElement(By.id("username")).sendKeys("username");
driver.findElement(By.id("password")).sendKeys("password");
driver.findElement(By.id("login")).click();
}
#Test
public void Gettext() {
driver.findElement(By.id("psge_click")).click();
WebElement msg=driver.findElement(By.id("input_id"));
String expectedText=("sample text");
String text=msg.getText();
System.out.println(text);
Assert.assertEquals(text,expectedText);
}
I am using this code for ERROR Message, getting result, but input data not getting "empty" is displayed
Try as below to get the data from input field :-
WebElement msg=driver.findElement(By.id("input_id"));
String text = msg.getAttribute("value");
Hope it will help you....:)

Why I can't take a screenshot using selenium webdriver in java?

I am working on a project on regression testing using Selenium webdriver in java. I have written the following code to take a screenshot.
package SelTest;
import java.io.File;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;
public class takeScreenShottest {
public WebDriver driver;
#Test
public void openBrowser() throws Exception{
driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.get("http://www.google.com");
try{
driver.findElement(By.id("testing")).sendKeys("test");
}catch(Exception e){
System.out.print("I am in Exception");
getscreenshot();
}
}
public void getscreenshot() throws Exception{
File scrFile=((TakesScreenshot)).driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File("E:\\Android-workspace\\Test1\\src\\ScreenShot\\screenshot.png"));
}
public static void main(String[] args) throws Exception{
takeScreenShottest ts = new takeScreenShottest();
ts.openBrowser();
}
}
I am getting an error for
File scrFile=((TakesScreenshot)).driver).getScreenshotAs(OutputType.FILE);
Why is this happening?
You can use this as below:
FileUtils.copyFile(((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE), new File("E:\\Android-workspace\\Test1\\src\\ScreenShot\\screenshot.png"));
Error solved. The error was TakesScreenshot cannot be resolved to a variable and Syntax error on token ")". So, I removed the ")." So the correct syntax was
File srcFile=((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);

NoSuchMethodError: org.openqa.selenium.remote.CommandInfo.<init>(Ljava/lang/String;Lorg/openqa/selenium/remote/HttpVerb;)V

i'm trying to run selendroid web for practice but i'm getting errors even i update selenium version (2.48.2) and set all paths correctly.
below is my code and i'm getting this error.
Please help me...
NoSuchMethodError: org.openqa.selenium.remote.CommandInfo.(Ljava/lang/String;Lorg/openqa/selenium/remote/HttpVerb;)V
package com.guru.test;
import io.selendroid.SelendroidCapabilities;
import io.selendroid.SelendroidDriver;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Test;
public class testWeb {
public WebDriver driver;
#BeforeSuite
public void setUp() throws Exception
{
DesiredCapabilities caps = SelendroidCapabilities.android();
driver = new SelendroidDriver(caps);
}
#Test
public void WebSiteTest() throws Exception
{
driver.get("http://google.com");
WebElement searchQuery = driver.findElement(By.name("q"));
searchQuery.click();
searchQuery.sendKeys("Test");
WebElement submit = driver.findElement(By.name("btnG"));
submit.click();
}
#AfterSuite
public void tearDown() throws Exception{
driver.quit();
}
}
I think you have mixed up selenium and selendroid jars. selendroid-standalone + selendroid-client should be enough for this.

Categories

Resources