Trying to find a way to close the option to download a file from a link in IE. Looked up a robot that could use the keyboard " Alt+Q" that would be the desired outcome, but when I go to run the program it gets stuck at the Robit() part.
When I click off the Webbrowser I'm working with the commands go through. In whatever application I'm working with at the time. Would love some help
public static void V_home(WebDriver driver) throws InterruptedException, AWTException{
driver.get(VUrl);
...
driver.findElement(By.linkText("Download School and District File")).click();
Robit();
}
public static void Robit() throws InterruptedException, AWTException{
Robot robot = new Robot();
Thread.sleep(1000);
robot.keyPress(KeyEvent.VK_ALT);
Thread.sleep(1000);
robot.keyPress(KeyEvent.VK_Q);
Thread.sleep(1000);
robot.keyRelease(KeyEvent.VK_ALT);
robot.keyRelease(KeyEvent.VK_Q);
}
Related
I'm trying to move to a different page (click 'my account'), and a floating advertisement appears:
advertisement
I tried to click on it, and can't find the "Close" element.
Found that it might related to frames, but still not works.
My Code:
public void clickMyAccount() {
driver.findElement(myAccountBtn).click();
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(10));
try {
Thread.sleep(5);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void clickCloseAd(){
driver.switchTo().frame(driver.findElement(By.id("google_esf")));
driver.switchTo().frame(driver.findElement(By.id("aswift_9")));
driver.switchTo().frame(driver.findElement(By.id("ad_iframe")));
driver.findElement(By.id("dismiss-button")).click();
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(10));
driver.switchTo().defaultContent();
}
Site:
https://practice.automationtesting.in/
Any idea how can I click the floating advertisement?
Tried: move between frames until able to find the Close element
Actual: still can't find this element
It is hard to handle advertisement popUp. So we can handle it in 2 ways:
downloading ad-blocker extension to your chrome and using the chrome option code.
download ad-bocker- https://chrome.google.com/webstore/detail/adblock-%E2%80%94-best-ad-blocker/gighmmpiobklfepjocnamgkkbiglidom
use chromeoption driver code:
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("disable-infobars");
Notice that popUp gets disabled is do back. so we can is click on myaccount go back and then click back:
WebElement MyAccount = driver.findElement(By.xpath("//[#href='https://practice.automationtesting.in/my-account/']"));
MyAccount.click();
driver.navigate().back();
MyAccount.click();
Found the issue:
My 'wait' not always working, so the driver tries to close the ad when not displayed - still searching for a way to correct this
It's working without frame1: "google_esf", only frame2 and frame3 needed
Updated code:
public void clickMyAccount() {
System.out.println("Click My Account");
if(driver.findElement(myAccountBtn).isDisplayed()){
driver.findElement(myAccountBtn).click();
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(10));
} else {
System.out.println("My account button not displayed");
}
}
public void clickCloseAd(){
System.out.println("Click Close Ad");
if(driver.findElement(By.id("aswift_9")).isDisplayed()){
// driver.switchTo().frame(driver.findElement(By.id("google_esf"))); //Not in use but also a frame
driver.switchTo().frame(driver.findElement(By.id("aswift_9")));
driver.switchTo().frame(driver.findElement(By.id("ad_iframe")));
driver.findElement(By.id("dismiss-button")).click();
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(10));
driver.switchTo().defaultContent();
} else {
System.out.println("Ad not displayed");
}
}
Thanks all!
However, if I set up to run using Microsoft Edge the test completes and i can see the user just created in the list. Here is my code. Very confused.
As you can see all I change to run for Microsoft Edge is un-comment the setProperty line and then change the FirefoxDriver to EdgeDriver. When I do that as I said the script runs and upon completion I log in and can see the user in the list while when using this code I cannot see the user.
public static void main (String[] args) throws InterruptedException {
// System.setProperty("webdriver.edge.driver", "/Program Files (x86)/Microsoft Web Driver/MicrosoftWebDriver.exe");
driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
baseUrl = "http://briotest.brio.viddler.com/";
firstName = "Sam";
lastName = "Bradford";
email = "sbradford#mail.com";
password = "Sooners1!";
test();
}
public static void test() throws InterruptedException {
// get to login page and enter credentials
driver.get(baseUrl + "/users/login");
driver.findElement(By.name("email_address")).clear();
driver.findElement(By.name("email_address")).sendKeys("jfayefrank#yahoo.com");
driver.findElement(By.name("password")).clear();
driver.findElement(By.name("password")).sendKeys("*********");
driver.findElement(By.cssSelector("button.button")).click();
// Now on the assets view page. Go to Users and select Create
driver.findElement(By.xpath("/html/body/header/div[2]/nav/div/ul[1]/li[6]/a")).click(); // Users tab
driver.findElement(By.linkText("Create")).click();
// Enter new users credentials and submit
driver.findElement(By.id("email")).clear();
driver.findElement(By.name("password")).clear();
driver.findElement(By.name("first_name")).clear();
driver.findElement(By.name("first_name")).sendKeys(firstName);
driver.findElement(By.name("last_name")).clear();
driver.findElement(By.name("last_name")).sendKeys(lastName);
driver.findElement(By.id("email")).clear();
driver.findElement(By.id("email")).sendKeys(email);
driver.findElement(By.name("password")).clear();
driver.findElement(By.name("password")).sendKeys(password);
try {
Thread.sleep(5000);
}
catch (Exception e) {
System.out.println("Could not perform pause");
}
// driver.findElement(By.cssSelector("input.button")).click();
driver.findElement(By.xpath("/html/body/div[1]/section/div/article/form/input[2]")).click();
System.out.println ("User created!!!");
}
Problem solved. Thanks to a coworker my problem is resolved. Apparently since Firefox was not maximized the Submit button was not really selected even though it appeared to do the action. Once I added driver.manage().window().maximize(); my users were now visible in the list. Did I mention the coworker was an intern...
I am unable to Handle the Print window/popup when I click on a Print button on the Web application. I need to be able to either Close this Window, Click on Print or Cancel. I am not sure whether this is a pop-up or a window.
Could some one help me?
see whether any web elements are visible if you hover the mouse over the popup.
if web elements are visible then its web application pop up
if no web elements are visible then its windows popup
However you can ignore the popup by sending escape key. the following code will work. i just tried, it worked.
public class demo extends parent {
WebDriver driver = new FirefoxDriver();
#Test
public void launch() throws InterruptedException {
driver.get("https://www.google.co.in");
Robot r = null;
try {
r = new Robot();
} catch (AWTException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
WebElement el = driver.findElement(By.xpath(".//*[#id='hplogo']"));
el.click();
Thread.sleep(10000);
el.sendKeys(Keys.CONTROL + "p"); // trying to invoke print pop up
Thread.sleep(10000);
r.keyPress(KeyEvent.VK_ESCAPE); //dismissing it by sending escape keys
}
}
hope you will get some idea here :)
I use java.awt.Robot to perform some mouse behaviors on my PC. The code is simple like below:
import java.awt.Robot;
import java.awt.event.InputEvent;
public class RobotProxy {
public static void main(String[] args) {
// TODO Auto-generated method stub
RobotProxy robotProxy = new RobotProxy();
try {
robotProxy.foo();
} catch (Exception e) {
// TODO: handle exception
System.out.println("Exception there...");
}
}
public void foo() throws Exception{
Thread.sleep(3000);
Robot robot = new Robot();
robot.mouseMove(501, 296);
leftClick(robot);
robot.mouseMove(505, 296);
leftClick(robot);
robot.mouseMove(509, 296);
leftClick(robot);
}
public void leftClick(Robot robot) throws Exception{
Thread.sleep(1000);
System.out.println("before Click...");
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
System.out.println("after Click...");
}
}
You can find that I use the combination of java.awt.Robot.mousePress(InputEvent.Button1_MASK) and java.awt.Robot.mouseRelease(InputEvent.Button1_MASK) to perform the mouse left click behavior.
It works fine at most time but fails sometimes. For example, the left click behavior for a kind of software's check box will fail. I can make sure I send the click command to java.awt.Robot but just nothing happens. What's more incredible is that java.awt.Robot.mouseMove(int x, int y) still works in that situation.
PC's OS is Windows8.1
The software is not market available and it's just a Windows native app written by cpp. The button on the software can be clicked but not for check box.
If the situation makes you confused, pls just tell me when will the java.awt.Robot fail to click. Thanks for your help in advance.
The problem is there is no delay between the robot.mousePress and robot.mouseRelease commands.
Here is an example of what you could add in between the two to fix the issue
Thread.sleep(100); a delay for 100ms. about as fast as you can hear, click click
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?