i'm usinng selenium 2 beta. i'm trying to click button which opens file attachment dialog. but when i click it nothing happens.
<input class="zf" name="Passport" id="PassportUpload" type="file" onclick="return { oRequired : {} }" maxlength="524288">
driver.findElement(By.name("Passport")).click();
using just selenium not selenium 2 i can click it easily.
I guess that the issue is only when using Internet Explorer since IE and FF handles the File Input slightly different: in FF you can click on the button or the field to invoke the Open dialog, while in IE you can click on the button or double-click on the field.
WebDriver using native events so it is sending a native mouse click to the File Input control which is translated to the click on the input field.
It was working in Selenium 1 because it is using the JavaScript to fire the events. To make it work in WebDriver you need to invoke the JavaScript:
WebElement upload = driver.findElement(By.name("Passport"));
((JavascriptExecutor)driver).executeScript("arguments[0].click();", upload);
However the code abouve will not in Firefox, so you can use something like:
WebElement upload = driver.findElement(By.name("Passport"));
if (driver instanceof InternetExplorerDriver) {
((JavascriptExecutor)driver).executeScript("arguments[0].click();", upload);
} else {
upload.click();
}
maybe try following code:
WebElement upload = driver.findElement(By.name("Passport"));
if (driver instanceof InternetExplorerDriver) {
((JavascriptExecutor)driver).executeScript("arguments[0].click();", upload);
} else if (driver instanceof FirefoxDriver) {
((JavascriptExecutor)driver).executeScript("arguments[0].click;", upload);
}else {
upload.click();
}
Related
I am currently looking for a solution to click on Sava button in chrome print preview window with selenium Java.
Is there any way we can handel chrome print preview page?
I have tried with the Robot class, but it seems not reliable/stable for my application.
Could you please someone help me to achieve this with selenium Java.
I fail to see any "Save" button on print preview page for Chrome browser
Here is how you can click "Cancel" button, I believe you will be able to amend the code to match your requirements:
First of all make sure to wait for the preview page to be available using Explicit Wait
Change the context to the print preview window via WebDriver.switchTo() function
All the elements at the print preview page are hidden in ShadowDom therefore you will need to:
Locate the element which is the first parent of the element you're looking for
Get its ShadowRoot property and cast the result to a WebElement
Use the WebElement.findElement() function to locate the next parent
repeat above steps until you reach the desired button
Example code just in case:
new WebDriverWait(driver, 10).until(ExpectedConditions.numberOfWindowsToBe(2));
driver.switchTo().window(driver.getWindowHandles().stream().skip(1).findFirst().get());
WebElement printPreviewApp = driver.findElement(By.tagName("print-preview-app"));
WebElement printPreviewAppConten = expandShadowRoot(printPreviewApp, driver);
WebElement printPreviewSidebar = printPreviewAppConten.findElement(By.tagName("print-preview-sidebar"));
WebElement printPreviewSidebarContent = expandShadowRoot(printPreviewSidebar, driver);
WebElement printPreviewHeader = printPreviewSidebarContent.findElement(By.tagName("print-preview-header"));
WebElement printPreviewHeaderContent = expandShadowRoot(printPreviewHeader, driver);
printPreviewHeaderContent.findElements(By.tagName("paper-button")).get(1).click();
where expandShadowRoot function looks like:
private WebElement expandShadowRoot(WebElement parent, WebDriver driver) {
return (WebElement) ((JavascriptExecutor) driver).executeScript("return arguments[0].shadowRoot", parent);
}
Save button will not work because page is not part of webpage. selenium only support web based application.
But you can use SIKULI to handle above scenario.
Need help on following scenario(using Java):
Doing it manually like this: after filling in some info in a parent page, clicking Continue button on it,
<INPUT TYPE='button' VALUE='Continue' onClick='sendForm()'>
A child window(UserConfirmationPage) pops up with those info from its parent window, clicking the Continue button on the child page, posting the data to server.
<FORM NAME='userConf' ACTION='user.jsp' METHOD='post'>
Do you want to continue?<BR>
<INPUT TYPE='button' VALUE='Continue' onClick='createUser()'>
</FORM>
However, when I do it using Selenium Web Driver, on the parent page,
btnContinue.submit()
a child page pops up with those info from parent page just like what I got when I do it manually but parent does not exist any more. While using
btnContinue.click()
a blank child page is opened without getting any info from parent page and it also complains "session is lost".
I also tried:
JavascriptExecutor jse = (JavascriptExecutor) driver;
jse.executeScript("arguments[0].click();",btnContinue);
and
new Actions(driver).moveToElement(driver.findElement(By.xpath("//input[#value='Continue']"))).click().perform();
but nothing works.
It seems that neither Submit() nor Click() could simulate what was done manually. Any idea?
Thanks a lot in advance!
You did not specify the language, so I assume you're using JAVA:
// since new tab has been opened - need to switch to this tab
// get a list of the currently open windows
Set<String> allTabs = driver.getWindowHandles();
// save the window handle for the current window
String programTab = driver.getWindowHandle();
// switching to the Save tab
String saveTab = ((String) allTabs.toArray()[1]);
driver.switchTo().window(saveTab);
// set timeout
// Click button
driver.findElement(By.id("buttonId")).click();
// set timeout
// switch back to the program tab
driver.switchTo().window(programTab);
Try this it is simplest and easiest code which help you to understand Parent and child scenario.
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.get("http://demo.guru99.com/popup.php");
driver.findElement(By.xpath("html/body/p/a")).click();
// return the parent window name as a String
String parentWindow=driver.getWindowHandle();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
// Pass a window handle to the other window
for(String childWindow: driver.getWindowHandles())
{
System.out.println("child");
//switch to child window
driver.switchTo().window(childWindow);
//find an element and print text of it
WebElement textLabel=driver.findElement(By.xpath("html/body/div[1]/h2"));
System.out.println(" text: "+textLabel.getText());
driver.close();
}
System.out.println("Come to parent window");
//switch to Parent window
driver.switchTo().window(parentWindow);
//find an element and print text of it
WebElement logotext=driver.findElement(By.xpath("html/body/div[1]/h2"));
System.out.println("text: "+logotext.getText());
driver.close();
}
I have to check switching behavior of sites but when i test it on site http://www.baidu.com , which in usual browsers, opens options in a new tab, opens the clicked option in the same tab while using selenium. How to make it execute as it executes normally in browsers?
Following code will help you to open link in new window :
driver.get("http://www.google.com"); //Replace your URL
WebElement link = driver.findElement(By.xpath("Your Element Xpath"]"));
Actions act = new Actions(driver);
act.keyDown(Keys.SHIFT).click(link).keyUp(Keys.SHIFT).build().perform();
for (String winHandle : driver.getWindowHandles()) {
driver.switchTo().window(winHandle);
}
// It will close new opened window
driver.close();
You can use id or other locator for element as per your flexibility.
We are just pressing keys Shift + Up by selenium code.
How to open a new tab in the same window session of the browser through Selenium WebDriver command?
Opening a new tab in the same browser window is possible, see solutions for Firefox:
How to open a new tab using Selenium WebDriver with Java?
Controlling firefox tabs in selenium
The problem is - once you've opened a tab, there is no built-in easy way to switch between tabs. Selenium simply doesn't provide an API for that.
Instead of a tab, open a new browser window.
Yes you can do that , See below my sample code for that :
//OPEN SPECIFIC URL IN BROWSER
driver.get("http://www.toolsqa.com/automation-practice-form/");
//MAXIMIZE BROWSER WINDWO
driver.manage().window().maximize();
//OPEN LINKED URL IN NEW TAB IN SAME BROWSER
String link1 = Keys.chord(Keys.CONTROL,Keys.ENTER);
driver.findElement(By.linkText("Partial Link Test")).sendKeys(link1);
Above code will open link1 in new tab. you can run above code to see effect. Above is public link includes testing form.
But as #alecxe told that there is no way to switch between tabs. So better you open new browser window.
Using java script we can easily open new tab in the same window.
public String openNewTab(){
String parentHandle = driverObj.getWindowHandle();
((JavascriptExecutor)driverObj).executeScript("window.open()");
String currentHandle ="";
// below driver is your webdriver object
Set<String> win = driver.getWindowHandles();
Iterator<String> it = win.iterator();
if(win.size() > 1){
while(it.hasNext()){
String handle = it.next();
if (!handle.equalsIgnoreCase(parentHandle)){
driver.switchTo().window(handle);
currentHandle = handle;
}
}
}
else{
System.out.println("Unable to switch");
}
return currentHandle;
}
I am afraid, but there is a way to switch between tab's .
We have successful answers for this problem.
Please find the below link.
switch tabs using Selenium WebDriver with Java
Selenium can switch between tabs.
Python:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome()
element = driver.find_element_by_css_selector("html") # Gets the full page element, any element works for this
key_code = Keys.CONTROL # Use Keys.COMMAND for Mac
driver.execute_script("window.open();") # Executes Javascript to open a new tab
driver.switch_to.window(1) # Switches to the new tab (at index 1, first tab is index 0)
driver.switch_to.window(0) # Switches to the first tab
I'm trying to automate the webpage "http://www.quikr.com",when I open this you will get a pop up window first saying "Please Choose Your Location" then after closing it , I can see the main page of quikr.
I tried closing that Popup page by automation ,but not able to do
Tried using xpath
driver.findElement(By.xpath("//*[#id='csclose']/strong")).click();
Tried using className
driver.findElement(By.className("cs-close cs-close-v2")).click();
Tried using id
driver.findElement(By.id("csclose")).click();
Please help me with this
to close multiple popups in webdriver and switch to parent window
String parent = driver.getWindowHandle();
Set<String> pops=driver.getWindowHandles();
{
Iterator<String> it =pops.iterator();
while (it.hasNext()) {
String popupHandle=it.next().toString();
if(!popupHandle.contains(parent))
{
driver.switchTo().window(popupHandle);
System.out.println("Popu Up Title: "+ driver.switchTo().window(popupHandle).getTitle());
driver.close();
The Following Code Works for Me to Handle Pop Up/ Alerts in Selenium Webdriver
Just Copy Paste this Code After the Event which is triggering the Pop up/Alert i.e after clicking on save.
if(driver.switchTo().alert() != null)
{
Alert alert = driver.switchTo().alert();
String alertText = alert.getText();
alert.dismiss(); // alert.accept();
}
in your case you try to run this code at starting of the code bcz it will directly close the pop up
Since this is a JavaScript modal, when the page finishes loading the JavaScript code could still be running. The solution is to wait until the button to close the modal be displayed, close it and then follow with your test. Like this:
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(30));
wait.Until(ExpectedConditions.ElementIsVisible(By.Id("csclose")));
driver.FindElement(By.Id("csclose")).Click();
Tested myself and works fine.
Hope it helps.
i have tried it in ruby and this one works
see if this can help you in any way :)
require 'selenium-webdriver'
require 'test/unit'
require 'rubygems'
class Tclogin < Test::Unit::TestCase #------------ define a class----------------
def setup
##driver = Selenium::WebDriver.for :firefox
##driver.navigate.to "http://www.quikr.com" #---- call url----
##wait = Selenium::WebDriver::Wait.new(:timeout => 60) # seconds #----define wait------
end
def test_login
##driver.find_element(:css, "strong").click
end
end
you can also use follwing xpath
##driver.find_element(:xpath, "//a[#id='csclose']/strong").click
public void closePopup() throws Exception {
WebDriver driver = new InternetExplorerDriver();
driver.get("http://www.quikr.com/");
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("csclose"))).click();
System.out.println("Successfully closed the start Popup");
}
Try driver.findElement(By.Id("csclose")).click(); I hope that will help
Simple pressing Alt + F4 buttons worked for me, e.g.:
driver.findElement(By.cssSelector("html body div div img")).sendKeys(Keys.chord(Keys.ALT, Keys.F4));