how to send_keys for div element in python selenium? - java

Below is the the HTML script:
<div class="textarea-wrapper">
<div class="textarea" tabindex="0"role="textbox" aria-label="Message text
input box" placeholder="Type a message..." spellcheck="true" data-
role="message-text-input" ng-keydown="preventReturns($event)" ng-
click="handleOneToOneMessage($event)" ng-
keyup="removeWhitespace();handleOneToOneMessage($event)" ng-blur="onBlur()"
ng-paste="parsePlainText()" ondrop="return false;" contenteditable="true"/>
</div>
The command i am using to send keys to that textarea:
mesg_box = browser.find_elements_by_css_selector(".textarea")
time.sleep(3)
browser.execute_script("arguments[0].click();", mesg_box[0])
mesg_box[0].send_keys("hiii")
here mesg_box will return 2 items and they are returning fine but I couldn't able to click on the input field may be because it has only div field but not the input field.
The test case is passing fine if the browser in focus.
The above Code is working Fine for chrome(when minimized also) but is failing for Firefox if Firefox is not on focus I mean if Firefox is minimized.
please let me know if there is any solution to send keys for the text area even when Firefox is minimized.

Related

Block an unexpected popup with Selenium

I am automating an e-commerce website but I am getting an unexpected random popup on any page. If I know that a popup will fire on a given web page, I can handle it. What do I do if it randomly occurs on any page?
I know how to block a popup but can't use that in this situation. Due to this popup, my scripts are failing. HTML code:
<div class="acsClassicInner" role="document">
<div class="acsLogoSpacer">
<img src="//gateway.foresee.com/sites/barneys/staging/trigger/sitelogo.gif" class="acsSiteLogo" title="" alt="">
<img src="https://static.foresee.com/logos/foresee/150_67.png" class="acsNoDisplay" title="ForeSee" alt="ForeSee">
<div title="ForeSee" alt="ForeSee" class="acsVendorLogoSVG"></div>
</div>
<h1 id="fsrHeading">We'd welcome your feedback!</h1>
<p>Thank you for visiting Barneys.com. You have been selected to participate in a brief customer satisfaction survey to let us know how we can improve your experience.</p>
<p class="acsNoticeAboutSurvey">The survey is designed to measure your entire experience, please look for it at the <u>conclusion</u> of your visit.</p>
<p class="acsAttribution">This survey is conducted by an independent company ForeSee, on behalf of the site you are visiting.</p>
No, thanks
<a id="acsFocusFirst" tabindex="3" href="about:blank" target="_blank" class="acsInviteButton acsRightButton acsAcceptButton" title="Yes, I'll give feedback (Opens in a new window)" role="button" precog_scanned="true">Yes, I'll give feedback</a>
</div>
Look into EventFiringWebDriver and WebDriverEventListener, and look for the popup in question> lets say the popup occurs after a button click, you can look for it in beforeClickOn and if it does occur do an action, else don't do anything.
Sorry I dont have any Java EventFiringWebDriver code snippets to share
http://seleniumhq.github.io/selenium/docs/api/java/index.html
code from selenium github
from selenium.webdriver import Firefox
from selenium.webdriver.support.events import EventFiringWebDriver, AbstractEventListener
class MyListener(AbstractEventListener):
def before_navigate_to(self, url, driver):
print("Before navigate to %s" % url)
def after_navigate_to(self, url, driver):
print("After navigate to %s" % url)
driver = Firefox()
ef_driver = EventFiringWebDriver(driver, MyListener())
ef_driver.get("http://www.google.co.in/")
Create a helping method with the following pseudo code:-
1. Check if there's a pop up.
2. If it finds pop up, then handle it.
3. If not, then return.
Now use this function in your script every time whenever there's a change in the content on the screen due to some action.

how to find xpath in selenium webdriver

Following is the HTML of the page, How do i get the Xpath or is there anyother way to automate using java?In fact, we shud click on this "continue" button.
<regform-button>
<button ng-disabled="activityIndicator" ng-click="validate()" type="button">
<div template="api-loader" ng-http-loader="">
<div class="http-loader__wrapper ng-hide" ng-show="showLoader" ng-include="template">
<span class="api-loader"></span>
</div>
</div>
<ng-transclude>
</button>
</regform-button>
If you are using Google Chrome, right click on the button, and select Inspect in the popup. The html will open in a developer tools frame. Right click on the element in the developer tools frame, hover over copy, select copy xpath.
Here is the XPath to the form on the URL.
//*[#id="main-frame"]/div[1]/div[2]/div/div[1]/div[1]/form/regform-steps/ng-transclude/regform-step[1]/ng-form/ng-transclude/regform-button/button
WebDriver driver = new ChromeDriver();
driver.get("https://uk.match.com/unlogged/landing/2016/06/02/hpv-belowthefold-3steps-geo-psc-bowling?klid=6740");
//fill in fields
WebElement element = driver.findElement(By.xpath("//*[#id=\"main-frame\"]/div[1]/div[2]/div/div[1]/div[1]/form/regform-steps/ng-transclude/regform-step[1]/ng-form/ng-transclude/regform-button/button"));
element.click();
driver.findElement(By.xpath("//regform-button/button")).click();
Try opening the console for Google Chrome and typing the following:
document.evaluate("//regform-button/button", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue.click()
It works...that's if you've specified the required fields. Also be sure to wait for a while (2 secs to be sure) after specifying the required fields as there might be an additional load after setting the required fields.
Edit: The code is javascript..I just took the xpath given by the previous answers and used that to locate the element through js. I'm saying that "//regform-button/button" should work..
Once you have selected the required values from the drop-downs appearing before Continue button, you can click on Continue button using the following statement:
driver.findElement(By.xpath("//span[text()='Continue']")).click();
The following xpath worked for me.But I dont know, why is it different for each different browser.
For Firefox :
`driver.findElement(By.xpath("//*[#id='main-frame']/div[1]/div[2]/div/div[1]/div[1]/form/regform-steps/ng-transclude/regform-step[1]/ng-form/ng-transclude/regform-button/button")).click()`;
For chrome: driver.findElement(By.xpath("//regform-button/button")).click();

Selenium WebDriver (Java) : Handle Field Validation Message

How to handle this kind of pop-up. My goal is to get the message when Submit button clicked then validate it againts my own text (maybe using assert). I've tried to locate the element using firepath (xpath) but when i click locate Element button on firebug, the pop-up disappear.
Here is the screenshot of the pop-up.
popUp
Here is the code :
<p class="errors"></p>
<input id="email" class="form-control" type="email" value="" name="email" required="" oninput="setCustomValidity('')" oninvalid="this.setCustomValidity('Email Cannot Be Empty')" placeholder="Email *" data-placeholder="X" data-format="">
Thank you in advance.
From the scrrenshot it looks like a tooltip. Something like when we mouse over Google title in https://www.google.co.in/.
To verify tooltip we can get the attribute 'title' and verify.
Example : in https://www.google.co.in/. tooltip is placed in title attribute as below.
title="Google"
<div id="hplogo" style="background-size:272px 92px;height:92px;width:272px" title="Google" onload="window.lol&&lol()" align="left">
For your scenario, the displayed tip message is available in 'oninvalid' attribute as below. So get this attribute value and validate it.
oninvalid="this.setCustomValidity('Email Cannot Be Empty')"
If the element is not inside a iframe, then you can directly try as follows:
String emailId = driver.findElement(By.id("email")).getText()
// write string equals login here comparing emailId that is captured and the one you want to compare to.
if not, first find the iframeand switch to it and then use above code to find the element. More detailed answer related to switching b/w frame is here
Finding the elements in the Pop-up:
Instead of clicking on the Locate Element button (of Firebug) first, Right click on the element you want to find in the Pop-up, and select Inspect with Firebug, which gives the corresponding HTML code for the element.
This is a bit late in the game but the way you get the custom validity message and not the generic one you have to call the reportValidity() event within JavaScript in Selenium. You'll see a driver.executeScript() method and this is where you must call the reportValidity event on the element being validated. This is how I did it:
wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions
.ElementToBeClickable(By.CssSelector("input#NewPassword.form-control")));
IWebElement input = driver.FindElement(By.CssSelector("input#NewPassword.form-control"));
input.SendKeys(string.Empty);
IWebElement form = driver.FindElement(By.TagName("form"));
form.Submit();
driver.ExecuteScript("document.getElementById('NewPassword').reportValidity();");
Assert.AreEqual("New password required", input.GetAttribute("validationMessage"));

Can't click on customised checkbox on Chrome and IE11

I have to click on customised checkbox, it's inside a div like the code following:
<div class="checkbox input-group">
<input id="checkboxH" class="checkboxClass" type="checkbox" name="checkboxH">
<label class="required ng-scope" for="checkboxH">
Text of checkbox
</label>
</div>
I use label as element to be clicked, and it works on FF as code below
webDriver.findElement(By.xpath("//label[#for='checkboxH']")).click();
but it doesn't work on Chrome (latest version) and IE11, the error is:
org.openqa.selenium.WebDriverException: unknown error: Element is not clickable at point (634, 498). Other element would receive the click:
Note that the checkbox is not visible on the screen, it has to be scrolled into view. But with the same code, it works with FF. FF can click on checkbox even when it is not in the view.
I tried another way, like this:
WebElement a = webDriver.findElement(By.xpath("//label[#for='checkboxH']"));
a.sendKeys(Keys.ENTER);
a.click();
And it also works with FF, but not Chrome and IE. It seems, selenium with FF automatically scrolls element into view and performs click on it
I am stuck here with Chrome and IE. Any help much appreciated. Thanks
EDIT
Apparently, I fixed issue with IE by setting this capability, and it works for IE:
capability.setCapability("nativeEvents", false);
With Chrome, I don't know which capacities could fix this issue
I have a work around: I use Actions to move to element, then I use sleep for 2 second, then it works.

Selenium 2.42.2 is not working to invoke .exe like for AutoIt

I want to upload a photo by selenium webdriver.
I used following environment:
[Selenium 2.42.2][1]
TestNg,
Firefox 26to 30,
[AutoIt][2]
Here is Test.java code
driver.findElement(By.id("photo")).click();
Runtime.getRuntime().exec("Z://browseExample.exe");
But selenium does not run the line to execute automatically.
If I manually clicks on browseExample.exe then it writes the desired path in browse textbox and clicks on open.
I noticed that, with selenium jar like 2.39, 2,38 it works fine.
But with those jars, my application's one locator of password field is not working.
When password is entered it displays in plain text and even clicking on submit button, it doesn't accept that password.
Here is the html of password field
<div class="formbox">
<input type="password" value="Password" onclick="this.value == 'Password' ? this.value = '' : this.value;" onfocus="this.select()" onblur="javascript:return ShowDefaultText();" maxlength="255" title="Please enter password" name="textBoxPassword" tabindex="2" id="textBoxPassword" style="display: none;">
<input value="Password" onclick="this.value == 'Password' ? this.value = '' : this.value;" onfocus="javascript:return HideDefaultText();" maxlength="255" title="Please enter password" tabindex="2" name="textBoxPasswordRegular" class="logintext" id="textBoxPasswordRegular" style="display: block;">
</div>
Here is AutoIT code of browseExample.exe
; Wait 10 seconds for the Upload window to appear
WinWait("[CLASS:#32770]","",10)
; Set input focus to the edit control of Upload window using the handle returned by WinWait
ControlFocus("File Upload","","Edit1")
Sleep(2000)
; Set the File name text on the Edit field
ControlSetText("File Upload", "", "Edit1", "Z:\snipett.txt")
Sleep(2000)
; Click on the Open button
ControlClick("File Upload", "","Button1");
Not sure that this will help you, but i think you don't necessary to use autoit to interact with dialog. I don't know java, but in C# there are a lot of libs to get any window and has possibility to interact with any element in app (e.g. UIAutomation). You can use winapi to get browse For file dialog and find textBox in it. Or you can use .net library for same purposes, i think there are a lot of articles how to use .net library in java (java .net interop).
Open Dialog is not a top level window, its a child window.
In order for Autoit to detect child windows you need to set:
Opt("WinSearchChildren", 1) ;0=no, 1=search children also
Allows the window search routines to search child windows as well as top-level windows.
0 = Only search top-level windows (default)
1 = Search top-level and child windows
Also its smart to add #RequireAdmin to the top of your Autoit script. That can solve a permission elevation problems.
About password field,
In your html you have some onclick code that will do certain processing of the password, like eg. changing the type of the input (this is why you see plain text.)
Invoke click and focus events on the input field before or/and after setting the value.

Categories

Resources