Selenium click print button to open print dialog, but blocked - selenium

Selenium click print button to open print dialog, but blocked.
<button id="print" onclick="window.print()"/>
WebElement print = driver.findElement(By.id("print"));
print.click(); // blocked here
// use the Robot to close print dialog
The print preview window is opened after clicking the print button. but the print.click() is blocked until I close the print dialog manually. Is the a way to tell Selenium to return immediately after clicking the button?

Related

Clicking button with selenium doesn't throw exception but the button isn't actually clicking?

I am trying to click a button, which once clicked some text should appear. Selenium is not throwing any errors which means the buttons should have been clicked however the text is not appearing (it works manually).
<button class = "redButton one">
<img src = "images/name.png" class = redImage">
"Name"
</button>
I tried to click the button with the xpath: "//button[contains(text(), 'Name')]". I don't understand why the text is not appearing.
What is the expected text to be displayed after button click event? In order to help with this request, provide the HTML snippet of <button> before & after click event. Since this is working fine manually, did you try adding adequate wait time for the text to display?

How to attach a file on Windows PC using Selenium WebDriver in Java

Manual steps:
Click Browse on the application Windows dialog box displays.
Enter file path in the file name field in windows dialog box.
Click the Open button
It attaches the file.
Code using the robot class:
#Test
public void test(){
WebElement element = driver.findElement(By.className("attach"));
element.click();
waitSeconds(9000);
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_SHIFT);
robot.keyPress(KeyEvent.VK_S);
robot.keyRelease(KeyEvent.VK_SHIFT);
robot.keyPress(KeyEvent.VK_1);
robot.keyPress(KeyEvent.VK_PERIOD);
robot.keyPress(KeyEvent.VK_T);
robot.keyPress(KeyEvent.VK_X);
robot.keyPress(KeyEvent.VK_T);
robot.keyPress(KeyEvent.VK_ENTER);}
When I run the test, it works just fine except for the last line.
Last line presses enter key just fine. After that, dialog box opens again. So file is never attached. Windows dialog box has two buttons: open and cancel. It should press Open button once after the file path is entered in the file name field. How can I do it?
Using org.openqa.selenium.JavascriptExecutor to set elements attribute 'value':
import org.openqa.selenium.JavascriptExecutor
//...
JavascriptExecutor javascriptExecutor = (JavascriptExecutor) driver;
String script = "document.getElementsByClassName('attach')[0].setAttribute('value', 'C:\...\D1.txt');";
javascriptExecutor.executeScript(script);

Right click is not happening and element is getting clicked on the same tab

I have a Selenium script to execute the following steps:
Launch the website - https://vusevapor.com/
Hit "I am 21+" button
Move to element devices.
Right-click on ciro complete kit.
The issue is that the right-click menu remains as is, and the element is getting clicked on the same page.
Here is my code:
//website
driver.get("https://vusevapor.com/");
//clicking on i am 21+ button
driver.findElement(By.xpath("/html/body/aside/div[2]/div/div/div[2]/div/a[1]/span")).click();
Thread.sleep(5000);
//xpath of devices menu
WebElement devices = driver.findElement(By.xpath("//*[#id=\"store.menu\"]/nav/ul/li[2]/a/span"));
//move to element devices
Actions act = new Actions(driver);
act.moveToElement(devices).build().perform();
Thread.sleep(3000);
//xpath of ciro complete kit
WebElement ciroKit = driver.findElement(By.xpath("//*[#id=\"store.menu\"]/nav/ul/li[2]/ul/li[2]/ul/li[1]/a/span"));
//right click on ciro complete kit
//*****Issue********right click is happening but the element is getting clicked on the same tab and right click menu remains as is
act.contextClick(ciroKit).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ENTER).build().perform();
try this:
option 1:
act.contextClick(ciroKit).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.RETURN).build().perform();
option 2-
Use Action and Robot class:
act.contextClick(ciroKit).build().perform();
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_DOWN);
robot.keyPress(KeyEvent.VK_ENTER);
Hope this helps.

Entering text in pop up and saving it on TestNG

I'm new to automation testing. I'm using twitter as a test website to learn selenium.
I want to create a new tweet and for doing that I'm using the below scenario. I'm able to open the 'New tweet' pop-up but still can't enter text in its text box. Please help.
Pre-Requisite: Login into Twitter
Click on the button (for creating new tweet) on top right side of screen.
The pop up opens up
Click on the text box present on pop-up to enter text.
The cursor gets highlighted in text box and save button enables
Enter some text
Click on Save button
For me it's working till point 2 and after that it doesn't enter text in textbox
See my code below:
String popUpHandle = driver.getWindowHandle();
driver.switchTo().window(popUpHandle);
driver.findElement(By.id("global-new-tweet-button")).click();
driver.findElement(By.xpath("(//button[#type='button'])[123]")).click();
driver.findElement(By.xpath("(//button[#type='button'])[123]")).sendKeys("test tweet");
driver.findElement(By.xpath("//button[#type='button'])[106]")).click();
#user3241182
I just tested this myself. You do not need to use any window handling.
Code Below is a provided solution and works great.
WebDriver driver = new FirefoxDriver();
driver.get("https://twitter.com/");
WebElement username = driver.findElement(By.id("signin-email"));
username.clear();
username.sendKeys("your_twitter_handle");
WebElement pass = driver.findElement(By.id("signin-password"));
pass.clear();
pass.sendKeys("your_twitter_passwd");
WebElement signInButton = driver.findElement(By.xpath("/html/body/div/div[2]/div/div[2]/div[2]/div[2]/form/table/tbody/tr/td[2]/button"));
signInButton.click();
WebElement tweetButton = driver.findElement(By.id("global-new-tweet-button"));
tweetButton.click();
WebElement tweetBox = driver.findElement(By.id("tweet-box-global"));
tweetBox.clear();
tweetBox.sendKeys("This is an automated tweet!");
WebElement tweetSubmit = driver.findElement(By.xpath("/html/body/div[9]/div[2]/div[2]/div[2]/form/div[2]/div[2]/button"));
tweetSubmit.click();

Unable to select IE8 popup window using selenium RC

I am trying to send commands to a popup window in IE8 with Selenium RC.
Currently, I am able to click on a button and open the popup window.
However, I am unable to select the window to send commands.
I have done extensive research on this subject for about a week and I believe this is strictly an IE issue. I have found some work-arounds; several blogs have instructed users to open up a blank window to "catch" the commands.
However, when I implement the code below, a blank window pops up and upon clicking the button that opens the pop up, a THIRD window opens and loads with the page I need.
Am I setting the target inaccurately? Please see below for selenium rc commands as well as the html source code of the page.
Java Code
selenium.runScript("selenium.browserbot.document.getElementByID(
'ctl00_ContentPlaceHolder1_WebGroupBox1_btnAddRequirement').target='popup'"
);
selenium.openWindow("", "popup");
///Click on Add Requirement button
selenium.click("id=ctl00_ContentPlaceHolder1_WebGroupBox1_btnAddRequirement");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
selenium.selectWindow("popup");
HTML for Add Requirement button
<input id="ctl00_ContentPlaceHolder1_WebGroupBox1_btnAddRequirement"
type="submit"
style="font-family:Arial,Times New Roman,Courier New;"
onclick="return validateProject();
WebForm_DoPostBackWithOptions(new
WebForm_PostBackOptions(
"ctl00$ContentPlaceHolder1$WebGroupBox1$btnAddRequirement",
"",
true,
"",
"",
false,
false
)
)
"
value="Add New Requirement"
name="ctl00$ContentPlaceHolder1$WebGroupBox1$btnAddRequirement"
>
In WatiN (different I know), I had to send a combination of alt+tab and enter to deal with pop-up boxes.