Using AWT Robot with Selenium WebDriver + Java, I want to switch browser to full screen mode(that we achieve manually using hitting F11 key). Hence, I wrote following code:
#Test
public void f() throws AWTException, InterruptedException {
Robot robot = new Robot();
WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com");
driver.manage().window().maximize();
Thread.sleep(2000);
robot.setAutoDelay(50);
robot.keyPress(KeyEvent.VK_F11);
Thread.sleep(5000);
driver.quit();
}
But browser doesn't switch to full screen. Can someone suggest, how can I achieve that?
Related
Trying to send data in table element (cell). Verified ID, Xpath, CssSelector and none them is wrong.Even, put timeout till page load. Even verified the iFrame (Already switchTo current iFrame).
No such element found error pops up every time. Not sure if I need to switchTo iFrame again after page load?
I tried all the possible ways but not pass through. I really appreciate any suggestions or new direction to think.
Thank you in advance.
Run my script for better insight where its failing.
public class SapDijon
{
WebDriver driver;
JavascriptExecutor jse;
public static void main(String[] args) throws Exception
{
SapDijon sapObj = new SapDijon();
sapObj.invokeBrowser();
sapObj.initializeSapDijon();
sapObj.ForecastME59();
}//End of Main
public void invokeBrowser()
{
System.setProperty("webdriver.chrome.driver", "U:\\Research Paper\\Selenium\\Drivers\\Chrome\\chromedriver_win32\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().deleteAllCookies();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(40, TimeUnit.SECONDS);
driver.manage().timeouts().pageLoadTimeout(40, TimeUnit.SECONDS);
}
public void initializeSapDijon() throws Exception
{
Thread.sleep(1200);
driver.get("http://dijon.cob.csuchico.edu:8041/sap/bc/gui/sap/its/webgui/?");
driver.findElement(By.id("sap-user")).sendKeys("H5");
Thread.sleep(1200);
driver.findElement(By.id("sap-password")).sendKeys("Onsjhjsa1087");
Thread.sleep(1200);
driver.findElement(By.id("sap-client")).clear();
Thread.sleep(1200);
driver.findElement(By.id("sap-client")).sendKeys("485");
Thread.sleep(1200);
driver.findElement(By.id("LOGON_BUTTON")).click();
}
public void ForecastME59() throws InterruptedException
{
driver.switchTo().frame("ITSFRAME1");
Thread.sleep(800);
driver.findElement(By.xpath("//td[#id='tree#105#4#1']//span[#class='lsSTStatusImg lsMirrorRtl lsSTStatusIcon urSTExpClo urCursorClickable']")).click();
Thread.sleep(800);
Actions action = new Actions(driver);
WebElement md61 = driver.findElement(By.xpath("//span[#id='tree#105#6#1#1#i']"));
action.doubleClick(md61).perform();
driver.findElement(By.id("M0:46:::4:2-imgSymb")).click();
driver.findElement(By.id("M0:46:::4:26")).sendKeys("HH-F");
driver.findElement(By.id("M0:50::btn[0]")).click();
Thread.sleep(6000);
driver.manage().timeouts().pageLoadTimeout(4, TimeUnit.SECONDS);
driver.findElement(By.xpath("//span[#id='tbl5732[1,8]_c-r']/input[#id='tbl5732[1,8]_c']")).click();
driver.findElement(By.xpath("//span[#id='tbl5732[1,8]_c-r']/input[#id='tbl5732[1,8]_c']")).sendKeys("100");
}
}//End of Class
After " action.doubleClick(md61).perform()" switch back from frame to default content and again switch to the available iframe.
To move back to the parent frame, you can either use switchTo().parentFrame() or if you want to get back to the main (or most parent) frame, you can use switchTo().defaultContent();
The issue persists because of a new window opened and the script was unable to find the element. The solution is to switch to a newly opened window to find element locator.
ArrayList tabs = new ArrayList (driver.getWindowHandles());
driver.switchTo().window(tabs.get(1));
I am trying to test an E-Commerce website using Selenium webdriver. The problem in the test is that whenever I try to add stuff in the cart it just pops a news letter window which I tried handling using alert but I cannot.
Can someone please help me. I am attaching a screenshot below along with the code.
public class Ui {
public static void main(String[] args) {
System.setProperty("webdriver.gecko.driver","C:/New folder/geckodriver.exe");
//First Iam going to initialize the webdriver by using Firefox driver//
WebDriver driver = new FirefoxDriver();
driver.get("https://www.build.com/");
driver.manage().window().maximize();
driver.findElement(By.xpath(".//*[#id='search_txt']")).sendKeys("K-6626-6U ");
Actions enter = new Actions(driver);
enter.moveToElement(driver.findElement(By.xpath(".//*[#id='site-search']/div/button"))).click().build().perform();
}
}
First thing is - Its not an alert it a window popup So you need to locate the close button and then click
Use below code for the same :
public class Ui
{
public static void main(String[] args)
{
System.setProperty("webdriver.gecko.driver","C:/New folder/geckodriver.exe");
//First Iam going to initialize the webdriver by using Firefox driver//
WebDriver driver = new FirefoxDriver();
driver.get("https://www.build.com/");
driver.manage().window().maximize();
new WebDriverWait(driver, 60).until(ExpectedConditions.visibilityOf( driver.findElement(By.xpath("//button[#class='close external-close']")))).click();
}
}
Here you have to use ExplicitWait until popup get visible and then have to perform click. If you won't use the wait then it will throw ElementNotVisibleException.
wait for some time and click on escape
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.get("https://www.build.com/");
driver.manage().window().maximize();
//give own time
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
Actions enter = new Actions(driver);
enter.sendKeys(Keys.ESCAPE).perform();
}
I am using www.flipkart.com,where I want to hover over "Appliances" and click on "Television".
public static void main(String args[]) throws InterruptedException {
System.setProperty("webdriver.chrome.driver","C:\drivers\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("http://www.flipkart.com");
driver.manage().window().maximize();
Thread.sleep(1000);
WebElement mainMenu = driver.findElement(By.xpath("//a[#title='Appliances']"));
WebElement submenuxpath = driver.findElement(By.xpath("//li[#class='Wbt_B2'][2]//li[#class='_1KCOnI _1h5QLb']//a[#title='Televisions']"));
Actions builder = new Actions(driver);
builder.moveToElement(mainMenu).perform();
//builder.moveToElement(submenuxpath).click().perform();
//driver.click(submenuxpath);
Thread.sleep(1000);
driver.close();
}
It is able to hover over on "Appliances". But I am getting error in driver.click(submenupath) or builder.moveToElement(submenuxpath).click().perform(). Where I am going wrong?
driver.click(submenupath) error: The method click(WebElement) is undefined for the type WebDriver. Quick fix : add cast to driver. Even if I am doing add cast,it is not working then. For builder.moveToElement(submenuxpath).click().perform(),no error is coming,but it is not clicking also.
I want to perform drag and drop in W3school web page using selenium. Code is working fine but output is not showing on the webpage.
link is :- http://www.w3schools.com/html/tryit.asp?filename=tryhtml5_draganddrop
My code is :-
public String dragAndDrop(String object,String data){
APP_LOGS.debug("waiting for popup closer");
try{
driver.switchTo().frame("iframeResult");
WebElement element = driver.findElement(By.xpath(".//*[#id='drag1']"));
WebElement target = driver.findElement(By.xpath(".//*[#id='div1']"));
(new Actions(driver)).dragAndDrop(element, target).build().perform();
}catch(Exception e){
return Constants.KEYWORD_FAIL+" -- Unable to drag"+e.getMessage();
}
return Constants.KEYWORD_PASS;
}
We can also interact with keyboard / mouse events using Actions class and robot class in Selenium. I have used Robot class to resolve your issue.
The Robot class is present in java.awt package. You can check all the methods in the docs.
public static void Task1() throws AWTError, AWTException, InterruptedException
{
WebDriver driver = new FirefoxDriver();
driver.get("https://www.w3schools.com/html/tryit.asp?filename=tryhtml5_draganddrop");
driver.switchTo().frame("iframeResult");
WebElement element1 = driver.findElement(By.xpath(".//img[#id='drag1']"));
WebElement element2 = driver.findElement(By.xpath(".//*[#id='div1']"));
Actions action = new Actions(driver);
Point element3 = driver.findElement(By.xpath(".//*[#id='drag1']")).getLocation();
int i=element3.getX()+800;
int b=element3.getY()+250;
Robot robot = new Robot();
robot.mouseMove(i, b);
// Press left click of mouse
robot.mousePress( InputEvent.BUTTON1_DOWN_MASK);
robot.delay(4000);
robot.mouseMove(i+20, b-120);
robot.mousePress( InputEvent.BUTTON1_DOWN_MASK);
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
Thread.sleep(10000);
driver.close();
}
Use below simple test code but selenium always like to choose second google suggestion result as the search text:
For example:
I input "Selenium", google will give suggestion list like below:
Selenium
Selenium WebDriver
Then webdriver will always pick up "Selenium WebDriver". But I used webdriver to sendKeys as "Selenium".
Is it a bug to webdriver?
public class HelloWorld {
private WebDriver driver;
#Before
public void setUp() {
System.setProperty("webdriver.ie.driver", "D:\\IEDriverServer.exe");
DesiredCapabilities caps = DesiredCapabilities.internetExplorer();
caps.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
driver = new InternetExplorerDriver(caps);
driver.get("http://www.google.com");
}
#After
public void tearDown() {
driver.quit();
}
#Test
public void testLitianyiNewsIsExisting() throws InterruptedException {
WebElement inputField = driver.findElement(By.name("q"));
inputField.sendKeys("selenium");
//Thread.sleep(5000);
driver.findElement(By.name("btnK")).submit();
}
}
I'm pretty sure googles immediate results are bugging you here. Once you are halfway typing your query, Google will start showing you the results already and the "btnK" button will no longer be visible. Try this in stead:
#Test
public void testLitianyiNewsIsExisting() throws InterruptedException {
WebElement inputField = driver.findElement(By.name("q"));
inputField.sendKeys("selenium");
inputField.sendKeys(Keys.ENTER);
}