Selenium Internet Explorer click button - java

I have a problem. I want to click for button and open link in new tab. This my code:
Actions actions = new Actions(this.driver);
actions.keyDown(Keys.CONTROL).click(myButton).keyUp(Keys.CONTROL).perform();
But link is opened on current tab, not new tab.
This is bug of that?
P.S. on chrome and firefox this code work very well

One solution would be to open a new window before clicking the link :
WebDriver driver = new FirefoxDriver();
driver.get("http://stackoverflow.com");
// open a new window
((JavascriptExecutor)driver).executeScript("window.open(window.location.href, '_blank');");
// set the context to the new window
driver.switchTo().window(driver.getWindowHandles().toArray()[1]);
// click the link
driver.findElement(By.linkText("Stack Overflow")).click();
Another solution would be to set the target attribute on the targeted element:
WebDriver driver = new FirefoxDriver();
driver.get("http://stackoverflow.com");
WebElement element = driver.findElement(By.linkText("Stack Overflow"));
// set the target _blank on the link
((JavascriptExecutor)driver).executeScript("arguments[0].target='_blank';", element);
// click the link
element.click();
// set the context to the new window
driver.switchTo().window(driver.getWindowHandles().toArray()[1]);

Your code is fine but the IEDriver is not quite as good as the ones for FireFox and Chrome. I had similar issues, as you can read here. I ended up solving my issues with the java.awt.Robot. In your case that would mean you'd need an IE-specific method for opening in new tab.
Robot r = null;
try {
r = new Robot();
} catch (AWTException e) {
e.printStackTrace();
}
...
r.setAutoDelay(30);
r.keyPress(KeyEvent.VK_CONTROL);
//then click the button
r.keyRelease(KeyEvent.VK_CONTROL);
It's not elegant but it works.

Related

Take a Screenshot with RemoteWebDriver

Is there a way to get selenium screenshots with headers ? I've tried the code below but the screenshot does not have a header.
I have a test case that requires clicking a link and making sure the action must bring to a new tab, so as evidence I have to attach capture there are two tabs.
public static void main (String args[]) throws IOException {
DesiredCapabilities dc = new DesiredCapabilities();
RemoteWebDriver driver;
URL url = new URL("http://localhost:4444/wd/hub");
dc.setCapability(CapabilityType.BROWSER_NAME, BrowserType.CHROME);
dc.setCapability(CapabilityType.PLATFORM, "MAC");
driver = new RemoteWebDriver(url, dc);
driver.manage().window().maximize();
driver.get("https://google.com");
new WebDriverWait(driver, 20).until(ExpectedConditions.presenceOfElementLocated(By.name("q")));
File getImage = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(getImage, new File("/Users/path/screenshot.jpg"));
driver.quit();
}
Current result
Expected result
No you can't, the screenshot functionality in Selenium takes an image of the rendered DOM. The browser chrome (i.e. the the UI components of the browser rendered by the OS the browser is running on) is not part of the DOM so Selenium is unaware of it.
The next question is why do you want the browser chrome in your image? If you are just trying to find out the displayed URL (as your question implies) you can use the command driver.getCurrentUrl(); instead.
As #Ardesco suggested, it is not possible to take screenshot.
However i think you can use java.awt.Robot class to capture the screen. It takes the screenshot of the current screen.
Here's an snapshot of code for capturing screenshot using java.awt,
public void getScreenshot(int timeToWait) throws Exception {
Rectangle rec = new Rectangle(
Toolkit.getDefaultToolkit().getScreenSize());
Robot robot = new Robot();
BufferedImage img = robot.createScreenCapture(rectangle);
ImageIO.write(img, "jpg", setupFileNamePath());
}

How to click a link within an iFrame which opens a new tab and switch to it

I am working on a project which includes clicking on a link and that should open in a new tab using webdriver, the problem is
The supposed link is contained in iFrame, soshift+click isn't working
private void openInNewTabAndSwitch(WebElement linkElement) {
// logic of opening in new tab goes here...
Actions newTab = new Actions(driver);
newTab.keyDown(Keys.SHIFT).click(linkElement).keyUp(Keys.SHIFT).build().perform();
Set<String> windowSet = driver.getWindowHandles();
driver.switchTo().window((String) windowSet.toArray()[1]); }
I cannot find the href attribute since some javascript function is opening it using some onClick()
<a onclick="javascript:LinkOccam (this, 'opportunity');">Mednomics Proposition</a>
Problem:-
It simply opens the required page in same tab.
Now, I cannot find anything related to this, please help..!
Other related info
I am using windows 7, Java 8, ChromeDriver
While you click on a WebElement which opens a New TAB you don't need to worry about whether the WebElement is on Top-Level Browsing Context or within an iFrame. Additionally while switching the TAB induce WebDriverWait and switch accordingly. To achieve that you can use the following code block :
private void openInNewTabAndSwitch(WebElement linkElement)
{
String parentTab = driver.getWindowHandle();
Actions newTab = new Actions(driver);
newTab.keyDown(Keys.CONTROL).click(linkElement).keyUp(Keys.CONTROL).build().perform();
WebDriverWait wait = new WebDriverWait(driver,5);
wait.until(ExpectedConditions.numberOfWindowsToBe(2));
Set<String> windowSet = driver.getWindowHandles();
for(String tab:windowSet)
{
if(!parentTab.equalsIgnoreCase(tab))
{
driver.switchTo().window(tab);
//do your work in the newly opened TAB
}
}
}

Unable to create or switch to new tab in Selenium Webdriver with JAVA on MAC

I've tried a different options just to open new tab but all of them lead to the same result : sending char "t" to google search field.
The goal in my real test is to switch between tabs in browser, but I am unable even open new one.
Very simple test
public class LoginPhp2 {
#Test
public void testGoogle() {
WebDriver driver = new SafariDriver();
driver.get("https://www.google.com");
//driver.findElement(By.cssSelector("body")).sendKeys(Keys.COMMAND + "t");
Actions action= new Actions(driver);
action.keyDown(Keys.COMMAND).sendKeys("t").build().perform();
//action.keyDown(Keys.COMMAND).sendKeys("t").keyUp(Keys.COMMAND).build().perform();
}
}
You may use javascript to open a new tab.
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("window.open();");
There are different ways of opening new tabs in windows and Mac. Actual keys to send depend on your OS, for example, Mac uses COMMAND + t, instead of CONTROL + t.
You can open new tab in Mac using:
driver.findElement(By.cssSelector("body")).sendKeys(Keys.COMMAND +"t");
After this you can switch to any of the opened tabs:
ArrayList<String> tabs = new ArrayList<String> (driver.getWindowHandles());
driver.switchTo().window(tabs.get(0));
I have followed the approach suggested earlier:
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("window.open();");
This is opening new tab in the same window. I have tried it on my MAC machine

Focus on recent open tab using selenium webdriver

I have one website in which When I click on button, it open new tab with link in same browser.
I want to tell selenium to focus on that recently open tab.
I tried many methods but none of them seem helpful in my case.
I have tried :
Method 1 :
driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL);
driver.findElement(By.cssSelector("body")).sendKeys(Keys.TAB);
Method 2 :
((JavascriptExecutor) webDriver).executeScript("window.focus();");
Method 3:
driver.switchTo().window(driver.getWindowHandles().last());
Try this code, use Java Robot. that worked for me.
ArrayList<String> tabs2 = new ArrayList<String>(driver.getWindowHandles());
System.out.println(tabs2.size());
for (int i = tabs2.size()-1; i>=0; i--) {
Thread.sleep(2000);
driver.switchTo().window(tabs2.get(i));
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_TAB);
robot.keyRelease(KeyEvent.VK_TAB);
robot.keyRelease(KeyEvent.VK_CONTROL);
System.out.println(driver.getTitle() + "i: " + i);
// do what you needed
}

Unable to execute Sikuli script after i click on the Web-element. ( Selenium webdriver with Java)

Here is my code
public static void main(String[] args) throws InterruptedException {
WebDriver driver = new FirefoxDriver();
//Open the URL
driver.get("http://www.toolsqa.com/automation-practice-form/");
//Maximize the window
driver.manage().window().maximize();
//Click on Button which will open file upload window
driver.findElement(By.xpath(".//*[#id='photo']")).click();
// Implementing Sikuli
ScreenRegion s = new DesktopScreenRegion();
Target target = new ImageTarget(new File("D://SELENIUM WORK-PLACE/File_upload//img//Capture.PNG"));
ScreenRegion r = s.wait(target, 20000);
r=s.find(target); // Locate the target on the screen.
//Create a Canvas object of the type DesktopCanvas.
Canvas test = new DesktopCanvas();
test.addBox(r);
test.addLabel(r, "I am present here");
test.display(5);
}
}
Actual Result : Clicks on the Web-element, It opens a File upload box. Nothing happens. ( But if i close the Box and re-open it manually then it works fine)
Expected Output- It should work at the first time we open the File upload box.
Your code is fine. Fix the path to: "D://SELENIUM WORK-PLACE//File_upload//img//Capture.PNG"
Then, try using the below image for getting the Filename textfield instead. Because bigger the size of image, there are more possibilities of Sikuli being unable to detect the element as even a pixel's change might get in the way of Sikuli identifying an element.:

Categories

Resources