Can someone guide me how I can achieve the following:
I am using selenium web driver java.
Whenever I click the preview button on the webpage, the pdf is opened in a new browser and I need to save that pdf with the name given dynamically.
So far I am able to click the preview button and a new browser is opened with the pdf. Here the browser doesn't have url.
After the pdf is open I am sending keys control+s.
Then save dialog window appears. I am stuck here about how to save pdf to the local drive.
The main browser is IE but i am trying in Firefox first
You can try this code :-I think this is what you are looking for. let me know If this what you are expecting.
System.setProperty("webdriver.gecko.driver", "D:/geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.get("http://www.adobe.com/content/dam/Adobe/en/devnet/acrobat/pdfs/pdf_open_parameters.pdf");
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
Thread.sleep(2000);
java.awt.Robot robot = new java.awt.Robot();
Thread.sleep(1000);
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_S);
robot.keyRelease(KeyEvent.VK_S);
robot.keyRelease(KeyEvent.VK_CONTROL);
Thread.sleep(2000);
robot.keyPress(KeyEvent.VK_ENTER);
Thread.sleep(2000);
robot.keyPress(KeyEvent.VK_TAB); // file replace move to yes button
Thread.sleep(2000);
robot.keyPress(KeyEvent.VK_ENTER); // hit enter
Just first execute the code, see if it is working, and is it what you want.
Last three lines of code are written for replace existing pdf file. So you just first comment those three lines, execute the code and from next time, include last three lines of code
You need to use Robot Class to handle events.
And let me know whether this is working at your end.
I think you should try to download the file immediately instead of trying to manage that browse window.
You can set the attribute download of the a element, and then click on the element. See code below:
WebElement pdf = driver.findElement(By.cssSelector("a"));
String script = "arguments[0].setAttribute('download');"
((JavascriptExecutor)driver).executeScript(script, pdf);
pdf.click();
Related
I've tried
File file = new File("/Users/swapnil.kotwal/Desktop/AntVsGradle.jpg");
driver.findElement(By.xpath("//input[#class='libray_create-resource_choose-file_hidden-input']")).sendKeys(file.getAbsolutePath());
And My HTML is something like below
<div class="libray_create-resource_choose-file">
<button class="btn is-hollow_blue libray_create-resource_choose-file_button undefined">Choose File</button>
<input type="file" class="libray_create-resource_choose-file_hidden-input">
</div>
<div class="library_create-modal_footer">
<button class="btn is-text_only btn-cancel undefined">Cancel</button>
<button class="btn is-filled_blue undefined" disabled="">Add</button>
</div>
I found that file input which is hidden got the file path set properly.
The problem is there Choose File button element is different from file input element //input[#class='libray_create-resource_choose-file_hidden-input']"
There seems to some JS event which make final Add button enable on click of Choose File button.
So, I imported file into file HTML element but how can I enable Add button?
I tried to make that button enabled
WebElement yourButton= driver.findElement(By.className("is-filled_blue"));
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].removeAttribute('disabled','disabled')",yourButton);
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.elementToBeClickable(yourButton));
It makes that button visible but still not allow to actually click on it.
First let me say that -- I don't program in Java, -- but I do a lot of selenium coding in Python.
When I was looking at this question from purely a selenium view point I noted that you aren't selecting the choose file button prior to sending your input. I would assume that you have to select this button to enable the element - btn is-filled_blue undefined
Here is some pseudocode that might help trigger that button to switch from disabled to enabled.
File file = new File("/Users/swapnil.kotwal/Desktop/AntVsGradle.jpg");
WebElement select_button = driver.findElement(By.xpath("//*[#class='btn is-hollow_blue libray_create-resource_choose-file_button undefined']"));
select_button.click();
WebElement upload_file = driver.findElement(By.xpath("//input[#class='libray_create-resource_choose-file_hidden-input']"));
upload_file.sendKeys(file.getAbsolutePath());
/* You might need to send an Enter, Return or Tab before the
add_file_button changes. This process requires testing on the website.
upload_file.sendKeys(Keys.ENTER);
upload_file.sendKeys(Keys.RETURN);
upload_file.sendKeys(Keys.TAB);
*/
boolean add_file_button_presence = driver.findElement(By.className("is-filled_blue")).isDisplayed();
boolean add_file_button_enabled = driver.findElement(By.className("is-filled_blue")).isEnabled();
if (add_file_button_presence==true && add_file_button_enabled==true)
{
WebElement add_file_button = driver.findElement(By.className("is-filled_blue"));
add_file_button.click();
}
Since I don't normally program in Java the structure and syntax of my pseudocode could be incorrect. If it is please let me know and I will either delete this answer or correct the issues with some research.
That undefined class looks suspect - try removing it by adding another js call:
js.executeScript("arguments[0].style.removeProperty('undefined')",yourButton);
My suggestion would be always limit the usage of JavaScript(JavascriptExecutor) code to an extent it simulates the end user action and not manipulates the application behavior
like enabling a button which is disabled functionally etc.
Our purpose is to simulate the application steps in the same way how an end user would use it.
I would suggest to use any third part tool like AutoIt/Sikuli to handle this case since sending file path doesn't enable the 'Add' button automatically as expected
1)Click on the 'Choose File' button using Selenium which opens the upload window, which is not a web component.
2)Since Upload window is not a web component Selenium doesn't support it.
3)Use any third party tools like AutoIt/Sikuli to handle the Windows upload popup by setting the filepath and submit.
4)As we are uploading the file in the UI in the same way how an end user would do, 'Add' button will be enabled automatically.
AutoIt
Try to use JavascriptExecutor to click it too, before you remove disabled attribute.
WebElement element = driver.findElement(By.xpath("xpath"));
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].click();", element);
If you use JavascriptExecutor you dont need to make it visible
I managed to upload file by using below hack... I never click on Choose File link/button which was launching Upload File Windows pop-up and by any mean that windows pop-up was not going away.
Tried below options.
1.
select_button.click();
select_button.submit();
action.sendKeys(Keys.ESCAPE).perform();
select_button.sendKeys(Keys.ESCAPE);
robot = new Robot();
robot.keyPress(KeyEvent.VK_ESCAPE);
robot.keyRelease(KeyEvent.VK_ESCAPE);
none of this hiding that pop-up on Mac OS
So, I found a Jugad(#hack in English) I never click on Choose File button which were launching that Upload File Window
public void uploadFile() {
WebElement element = driver.findElement(By.className("libray_create-resource_choose-file_hidden-input"));
element.sendKeys("/Users/swapnil.kotwal/projectName/automation.pdf");
WebElement addButton = driver.findElement(By.className("is-filled_blue"));
// click was important, thanks to this answer in this thread https://stackoverflow.com/a/67095019/1665592
driver.executeScript("arguments[0].click();", addButton);
driver.executeScript("arguments[0].removeAttribute('disabled','disabled'); arguments[0].style = \"\"; arguments[0].style.display = \"block\"; " +
"arguments[0].style.visibility = \"visible\";", addButton);
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.elementToBeClickable(addButton));
addButton.click();
}
My Java Selenium WebDriver script clicks a link in a web page that opens a new tab and navigates to an external site. There are multiple of these links to different social media.
The problem I have is that sometimes the new tab opens and tries to load the external page but stops all together.
I figured this isn't a big deal, I will just refresh the page with my code like I would manually in the browser and it will attempt to reload the page. I was wrong.
Evidently, perhaps in Chrome only, the reload/refresh functionality included with Selenium and even JavaScript does not work the same as clicking the refresh button. In this circumstance, they do nothing at all.
Luckily, this only happens every now and then but it does cause my test to fail when it does happen.
When this happens, the title area of the tab says "untitled", the page is blank white with nothing on it, and the desired URL will be in the address bar. Only manually clicking the refresh button will reload the page properly.
I haven't seen this happen in Firefox even once so I am going to assume it is only an issue in Chrome.
Does anyone know how to work around this issue?
Here is what I have tried in JavaScript:
document.location.reload(false);
document.location.reload(true);
I tried these in Java Selenium:
driver.navigate().refresh();
public Actions act = new Actions(driver);
act.keyDown(Keys.CONTROL).sendKeys(Keys.F5).keyUp(Keys.CONTROL).perform();
I should also say that these refresh methods work normally when the page is working normally. It is literally this one situation where they do not and I don't know what to do. Is there no way to programmatically click the refresh button in the browser? I need to find out if I can move the mouse and have it click.
Might be chrome web driver issue. try updating to latest.
driver = new ChromeDriver();
driver.manage().window().maximize();
baseUrl = "http://www.google.com/";
driver.get(baseUrl);
driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL +"t");
ArrayList<String> tabs = new ArrayList<String> (driver.getWindowHandles());
driver.switchTo().window(tabs.get(1)); //switches to new tab
driver.get("https://www.facebook.com");
driver.switchTo().window(tabs.get(0)); // switch back to main screen
driver.get("https://www.linkedin.com");
I am writing selenium script for Gmail login and logout functionality. I am able to successfully login using below code.
//Open gmail
driver.get("http://www.gmail.com");
// Enter userd id
WebElement element = driver.findElement(By.id("Email"));
element.sendKeys("xyz#gmail.com");
//wait 5 secs for userid to be entered
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
//Enter Password
WebElement element1 = driver.findElement(By.id("Passwd"));
element1.sendKeys("Password");
//Submit button
element.submit();
But i could not write a script to logout. Could yu please provide me script for logout?
Thanks!
It is a very bad idea trying to automate Gmail. First of all, it is against Google's policy and when you sign up for Gmail, you accepted terms and conditions that you won't automate. Having said that there are many efficient ways to connect to your Gmail account. One of the approaches is to use IMAP client like IMAP4. Using this, you can connect to your Gmail, go through your inbox, delete messages,sign out etc. By doing this, you don't have to worry about automating UI portion. Also, Google changes its UI frequently just to stop people from automating it, so if you automate Gmail UI, then it might work today but it won't work after a couple of days.
Go through this link and you should be able to implement IMAP4 in your tests within few minutes:
http://mailsystem.codeplex.com/discussions/269058
Try the below code.
driver.findElement(By.className("gb_Ta")).click(); // To click the flyout menu
driver.findElement(By.className("gb_71")).click(); // To click the sign out button
Try the code below
driver.findElement(By.xpath("//*[#id='gb']/div[1]/div[1]/div/div[3]/div[1]/a")).click();
// click on actual logout button step 2
driver.findElement(By.id("gb_71")).click();
//closing the webdriver window after successful completion of the test
driver.close();
WebDriverWait wait=new WebDriverWait(driver,50);
WebElement logout=driver.findElement(By.cssSelector("span.gb_4.gbii"));
logout.click();
WebElement signout=driver.findElement(By.id("gb_71"));
signout.click();
After login to gmail, Try this code to logout:-
driver.findElement(By.xpath("//span[#class='gb_7 gbii']")).click(); driver.findElement(By.id("gb_71")).click();
use xpath
// Click on the image icon present in the top right navigational Bar
driver.findElement(By.xpath("//div[#class='gb_1 gb_3a gb_nc gb_e']/div/a")).click();
//Click on 'Logout' Button
driver.findElement(By.xpath("//*[#id='gb_71']")).click();
//Close the browser.
driver.close();
use cssSelector
//Click on the profile image present in the right top corner
driver.findElement(By.cssSelector("span.gb_3a.gbii")).click();
//Click on 'Sign Out' button
driver.findElement(By.id("gb_71")).click(); //Close the browser
window driver.close();
use cssselector:
driver.findElement(By.cssSelector("span.gb_8a.gbii")).click();
driver.findElement(By.id("gb_71")).click();
Try this code for sign out the gmail using the selenium webdriver. It's working for me
driver.findElement(By.cssSelector(".gb_b.gb_db.gb_R")).click();
Thread.sleep(5000);// Click on the image icon present in the top
right navigational Bar
driver.findElement(By.cssSelector(".gb_Fa.gb_Pe.gb_We.gb_wb")).click();
Thread.sleep(5000); //Signout button
I'm using Selenium Webdriver with Java and trying to force a click to open a new window. I can't use a contextClick because the option to open the link doesn't exist if I right-click the image button. Any ideas on how this can be done using Wedbriver?
You can consider pressing the Ctrl button and later performing the click activity.
Code Example
WebElement element = driver.findElement(By.xpath("CHOOSE_YOUR_XPATH_HERE")); //Choose best way of selecting WebElement
element.sendKeys(Keys.CONTROL);
element.click();
Hope this works for you
I am writing an automation script in Java in selenium webdriver. When I try to logoff my main window screen I am getting a pop-up window that does not allow me to go to the main screen unless I click some option "Leave page" or "Stay on page" . Can I get the script to close this screen and logoff the main application.Firepath does not work for this as well. I am a beginner trying to learn automation.Thanks.
Adding some of your code to the question would be useful...
Here is a wild guess, assuming that you have a WebDriver driver instance:
driver.switchTo().alert().accept(); // Leave Page
driver.switchTo().alert().dismiss(); // Stay on page
Try these first:
driver.switchTo().alert().accept(); // to leave page
or
driver.switchTo().alert().dismiss(); // to accept page
if doesn't work, then you can use Robot class to press Enter or any Key like -
Robot robot = new Robot();
robot.setAutoDelay(3000);
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
Only thing you've to notice - which button the default cursor or which one is highlighted, Robot will perform action on the highlighted object only, from there you can press tab to go anywhere else.
Hope this helps.