I have a situation where I need to open a link incognito window from another url.
I have tried by using action class to perform control shift n key events but it's not working.
Robot class working fine but I'm looking for other alternative.
Actions act = new Actions(driver);
act. KeyDown(Keys.cONTROL);
act.keyDown(Keys.SHIFT);
act.keyDown("n").build(). perform ();
But it's not working
In case you want to open a new window in incognito mode i.e. without cookies you can simply open a regular new Selenium driver new window and the to clear the cookies with the following command:
driver.manage().deleteAllCookies();
Related
I am very new to Selenium WebDriver with Java. There is a Upload button on a job portal. When I click on that button windows explorer is displayed to choose the file. there are open and cancel buttons on this window. i want to select the cancel button. since it is a windows explorer i cannot inspect the cancel button. how do we write the code for cancelling the button. Thanks in advance.
driver.get("https://my.indeed.com/resume?from=gnav-homepage&co=US&hl=en_US");
driver.manage().window().maximize();
Thread.sleep(3000);
driver.findElement(
By.xpath("//[#id='container']/div/div/div[2]/div/div/div[2]/div/div[1]/div/div[1]/button")).click();
I do not have Java programming skills. I hope the below python code is easy to translate. In my opinion, combining windows action with selenium calls is often flaky.
There is a hidden element called 'upload resume button', you can change the attribute value to see it on the UI, use the send keys method on the element to upload the resume.
from selenium.webdriver import Remote, DesiredCapabilities
driver = Remote(desired_capabilities=DesiredCapabilities.CHROME)
driver.get('https://my.indeed.com/resume?from=gnav-homepage&co=US&hl=en_US')
driver.execute_script(
"document.getElementById('upload-resume-button').setAttribute('class', '')"
)
upload_your_resume = driver.find_element_by_id('upload-resume-button')
upload_your_resume.send_keys(r'C:\test\resume.docx')
The above code worked in my local.
Short answer is you cant, but if you want to upload a local file you can use send_keys on the input field, sending the file path.
Here is a python exemple:
driver.find_element_by_id("IdOfInputTypeFile").send_keys(os.getcwd()+"/image.png")
you can use Robot class to simulate native keyboard and mouse actions to interact with windows based pop-ups.
The shortcut to close any opened window is: “Alt + Space +C” – Closes the focused window.
Robot rb = new Robot();
rb.keyPress(KeyEvent.VK_ALT);
rb.keyPress(KeyEvent.VK_SPACE);
rb.keyPress(KeyEvent.VK_C);
rb.keyRelease(KeyEvent.VK_C);
rb.keyRelease(KeyEvent.VK_SPACE);
rb.keyRelease(KeyEvent.VK_ALT);
I'm using IEDriver to test my webapp. My test is written in Java and driver is defined as following:
System.setProperty("webdriver.ie.driver","C:\\Selenium\\IE\\IEDriverServer.exe");
DesiredCapabilities capabilities = DesiredCapabilities.internetExplorer();
capabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS,true);
driver = new InternetExplorerDriver(capabilities);
driver.manage().window().maximize();
I'm facing the problem when the tests simulates F5 after form submission with:
Actions actions = new Actions(driver);
actions.keyDown(Keys.CONTROL).sendKeys(Keys.F5).perform();
As a result appears IE window saying:
I've tried to skip it by performing
actions.keyDown(Keys.CONTROL).sendKeys(Keys.ENTER).perform();
and
driver.switchTo().alert().accept();
As I've understood, this window is not an alert, so driver can not switch to it.
So is there any way to execute "Retry" in this window?
Java Robot utility can be used to click on the Retry button. Sample code is written below:
Robot robot = new Robot();
robot.keyPress(java.awt.event.KeyEvent.VK_ENTER);
robot.keyRelease(java.awt.event.KeyEvent.VK_ENTER);
Hope it helps!
If you use F5, then the page will refresh items which are modified, considers cache, so you will get warning.
Instead try reloading the entire page something like,
driver.navigate().refresh();
This will load the page without any warning. This post will give you some insights.
Look for Diff b/w F5 and navigate().refresh()
I am using three instances of fire fox driver for automation.I need to bring current active firefox browser into front, Because I am using some robo classes for some opertation. I had tried java script alert for google chrome in mac ( same operation) and its worked fine. In windows used user32 lib. In the case of firefox mac its showing the alert in background but the web page is not come into front.
((JavascriptExecutor)this.webDriver).executeScript("alert('Test')");
this.webDriver.switchTo().alert().accept();
The above code I used for chrome in Mac. Same code is working and showing alert for firefox but the window is not coming to front.
Please suggest if there any other method for doing the same in firefox.
Store the window handle first in a variable, and then use it to go back to the window later on.
//Store the current window handle
String currentWindowHandle = this.webDriver.getWindowHandle();
//run your javascript and alert code
((JavascriptExecutor)this.webDriver).executeScript("alert('Test')");
this.webDriver.switchTo().alert().accept();
//Switch back to to the window using the handle saved earlier
this.webDriver.switchTo().window(currentWindowHandle);
Additionally, you can try to maximise the window after switching to it, which should also activate it.
this.webDriver.manage().window().maximize();
Try switching using the window name:
driver.switchTo().window("windowName");
Alternatively, you can pass a "window handle" to the switchTo().window() method. Knowing this, it’s possible to iterate over every open window like so:
for (String handle : driver.getWindowHandles()) {
driver.switchTo().window(handle);
}
Based on the Selenium documentation: http://docs.seleniumhq.org/docs/03_webdriver.jsp
As described in other topics, you can use
driver.manage().window().setPosition(new Point(-2000, 0));
too.
# notifications for selenium
chrome_options = webdriver.ChromeOptions()
prefs = {"profile.default_content_setting_values.notifications": 2}
chrome_options.add_experimental_option("prefs", prefs)
current_path = os.getcwd() # current working path
chrome_path = os.path.join(current_path, 'chromedriver')
browser = webdriver.Chrome(executable_path=chrome_path, chrome_options=chrome_options)
browser.switch_to.window(browser.current_window_handle)
browser.implicitly_wait(30)
browser.maximize_window()
browser.get("http://facebook.com")
Only thing that worked for me on mac: self.driver.fullscreen_window().
I want to open www.google.com in one tab and then open new tab and open www.facebook.com
I am using Below code but it open facebook in first tab
driver.get("www.google.com");
driver.findElement(By.tagName("Body")).sendKeys(keys.CONTROL + "t");//opens new tab
driver.get("www.facebook.com");//but load facebook in first tab i.e on google page
Is this because I am using same name driver.get??
If the code to open the new tab worked, you can use driver.switchTo().window() to switch to the newly opened tab (as seen here).
But If the tab didn't open, Selenium does not provide any mechanism to do this, so you have to implement around it (like using the java.awt.Robot class)
You should also consider just using two different instances of the WebDriver and run both your sites in its own window
Use java awt Robot class as below.
driver.get("http://www.google.com");
Robot r = new Robot();
r.keyPress(KeyEvent.VK_CONTROL);
r.keyPress(KeyEvent.VK_T);
r.keyRelease(KeyEvent.VK_CONTROL);
r.keyRelease(KeyEvent.VK_T);
driver.get("http://www.facebook.com");
I want to be able to open a link in a new tab in Selenium 2. Also i want to close the tab when i am finished interacting with the page. How is this possible if I have a WebElement of an <a> tag?
I am using the Java API of Selenium 2 with the Firefox Driver, running on Firefox 4.
The way I figure out for selenium 2, work fine for Chrome and firefox, IE has security check issue:
Set<String> winSet = webDriver.getWindowHandles();
List<String> winList = new ArrayList<String>(winSet);
String newTab = winList.get(winList.size() - 1);
webDriver.close(); // close the original tab
webDriver.switchTo().window(newTab); // switch to new tab
At the moment, the Selenium WebDriver API doesn't have any way of handling tabs. The project would really need a consistent, cross-browser set of methods for managing tabs before I would expect to see an implementation in one of the language bindings like Java. Until then, your JavaScript solution may be the only way, and remember that your code would then be responsible for managing the lifetime of that tab.
to use selenium at its best we at sol-logics combine it with java.awt.robot class. you can send keys that can close a browser window. try using
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_W);
and reply if it works
Took awhile (~2 weeks) for me to track down the right sequence of commands, but this is by far the easiest method I've found for a Win7/Chrome setup to open a link in a new tab AND switch to the new tab automatically.
WARNING! Make sure to always perform the keyUp actions. If you fail to perform keyUp your system will keep those keys pressed until a reboot or keyUp occurs.
Windows 7/Chrome:
WebElement elem = driver.findElement(By.linkText("MyLinkText"));
// Chrome key combos:
// SHIFT + CTRL + click = Open in new tab (and switch to new tab)
// SHIFT + CTRL + RETURN = Open in new tab (in background)
Actions act = new Actions(driver);
act.keyDown(Keys.LEFT_CONTROL).keyDown(Keys.LEFT_SHIFT).perform();
// Wrap in a try/catch during implementation to ensure you perform keyUp(s).
elem.click();
act.keyUp(Keys.LEFT_CONTROL).keyDown(Keys.LEFT_SHIFT).perform();
Note: I know it's an old thread, I just wanted to catalog the solution here because I couldn't find a more elegant solution and wanted to save someone else a little time (hopefully :).
Edit: Typo
Here is how i did it using Python.
This solution is a bit dirty but it works if you want to close the tab.
Im mimicking the mac shortcut CMD + W to close a tab, if you are running windows you may have to implement a different key combination.
import from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
from selenium import webdriver
driver = webdriver.Firefox()
driver.get("http://www.amazon.com/gp/search/ref=sr_in_-2_p_lbr_brands_browse-_2895?rh=n%3A172282%2Cn%3A!493964%2Cn%3A502394%2Cp_lbr_brands_browse-bin%3ALytro")
action_chains = ActionChains(driver)
action_chains.key_down(Keys.COMMAND + "w")
action_chains.perform()
action_chains.key_up(Keys.COMMAND + "w")
driver.implicitly_wait(5)
What I use is the Robor class.
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_W);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyRelease(KeyEvent.VK_W);
This makes the Robot rapidly press and release the CTRL + W keys to simulate a user interaction. If you only use keyPress event, this will close all the tabs and windows of the WebDriver.
Hope I helped you.