input does not appear in firefox - java

I test a system that only works in IE, if I run it in Firefox, a single entry (cpfField) , does not appear on the page and consego not identify because of this, but in IE, gives much trouble in the tests.
Is there a way around this behavior page to run tests in Firefox?
Code in Firefox:
<input type="text" name="cpf" maxlength="11" size="15" value="" class="textbox" id="cpfField">
Code in IE11:
<INPUT id=cpfField class=textbox maxLength=11 size=15 name=cpf>

You can add an attribute in an HTML run time using javascript. If your page is not going to reload then this value will not remove. I am not sure but you can have try with same.
JavascriptExecutor js = (JavascriptExecutor) driver;
WebElement element = driver.findElement(Your Locator);
js.executeScript("arguments[0].setAttribute('id', 'cpfField')",element);
Now try to click your button
Hope it will help you :)

Related

Call function result missing 'value' while automating a certain page using Selenium (Java)

I am trying to automate a certain page. I want to make the mouse click anywhere in the page, but I am getting the following error:
call function result missing 'value'
The elements in the page is:
<div id="b1" iframegroupid="P1" class="btn_act"><img src="theme/images/hi.png" draggable="false"><span>K</span></div>
Another element is:
<div draggable="false" role="presentation" tabindex="0" class="xc" widgetid="SCREEN_field_Submit" id="CLOSE_1" title="">
The code that I have is:
WebElement close_frame = Driver.driver.findElement(By.id("P1"));
//driver.get(Constant.geturl());
driver.switchTo().frame(close_frame);
WebElement btn1 = Driver.driver.findElement(By.id("CLOSE_1"));
btn1 .click();
I dont understand what is going wrong.
i think you should have to update your chromedriver with latest verion.
update to update chromedriver to 2.36 or greter, older versions have such issues.
i am also confused about your Driver.driver. and directly driver. methods.
try using
driver.switchTo().frame(driver.findElement(By.id("P1")));
wait.until(ExpectedConditions.elementToBeClickable(By.id<CLOSE_1>));
driver.findElement(By.id("CLOSE_1")).click();

How can I get Selenium WebDriver (Java) to click this button?

I am new at using Selenium WebDriver to automate test cases. So far (using Selenium and Java), I am able to open the testing website, enter the username and password, and log in. After logging in, however, the user is re-directed to a screen with a security warning that must be accepted before they can access the actual website. To do this, they must click on a button called "I Agree". I can't get Selenium to click the button and, without it, I can't get to the rest of the site to automate. Here is the HTML for the button:
<form name="landingHandlerSF" method="post" action="/apps/bap/secLandingHandler.do">
<input name="userAgreedTerms" value="" type="hidden">
<input name="submit" value="landing" type="hidden">
<input name="buttonAction" value="I Agree" onclick="setValue('agreetoTerms', 'Y')" type="submit">
</form>
Here is the code I have tried (which doesn't work):
WebElement button = driver.findElement(By.name("buttonAction"));
button.click();
Could someone please help me with this?
As per my understanding, page is redirecting to new html page but driver will be pointing to parent page(Login page in your case) , so you may have to switch to child window in order to click on I Agree button.
The following code will switch the driver away from the current window (ie, the login window) to the new window (ie, the security warning). After clicking I Agree that security warning will be closed and the driver will switch back automatically
String thisWindow = driver.getWindowHandle();
Set<String> windowHandles = driver.getWindowHandles();
for (String windowHandle : windowHandles) {
if (!windowHandle.contains(thisWindow)) {
driver.switchTo().window(windowHandle);
}
}
If it is not navigating to new page then it must be under some iframes , in that case you may need to switch to frame and click the button.
Hope this will work.
Try this and let me know what happened.
Did you try to inject JavaScript code into the browser?
driver.executeScript("setValue('agreetoTerms', 'Y')");
or
driver.executeScript("document.getElementsByName('buttonAction')[0].click()");

How to make the hidden element visible using java script executor

Currently am working on Selenium Webdriver with Java
Am trying to click on a button but i can't able to click because it is hidden. Please let me know how to make the hidden element visible 1st then how can click the button.
Please give me some example and my HTML tag is:
<input id="iskpiFilterAction" type="hidden" value="1" name="isKpiFilterAction">
Hmm, your question doesn't make sense for me. But I can exactly answer for your question.
For selenium 2 (webdriver):
WebDriver driver = ...
JavascriptExecutor jsExecutor = (JavascriptExecutor) driver;
jsExecutor.executeScript("document.getElementById('iskpiFilterAction').type = 'button';");
Result is:
This code causes changing type of element (from hidden to button), but it doesn't make sense for all of us. These two elements have different purpose/use. For more information see:
Original purpose of <input type="hidden">?
What's the point of having hidden input in HTML? What are common uses for this?
http://www.w3schools.com/jsref/dom_obj_hidden.asp
I didnt quiet understand the question.. However .. if you have a hidden object which you want to unhide dynamically using JavaScript using some trigger, this is a way you could do that:
<head>
<script>
function unhide()
{
document.getElementById("iskpiFilterAction").type = "button";
}
</script>
</head>
<body onload="unhide()">
<input id="iskpiFilterAction" type="hidden" value="1" name="isKpiFilterAction">
</body>
I am using body onload event to unhide the object so the moment this page loads you will see the button which u can then click. However if you want it some be triggered at some other event you can use the function accordingly.
Hope it helps.
Try this:
WebElement element = driver.findElement(By.id("iskpiFilterAction"));
((JavascriptExecutor) driver).executeScript("arguments[0].style.type = 'button';", element);

Xpath does not work with Selenium

I'm trying to build tests for an old system. The HTML is not well formed. I need to identify and click a radio button.
The html looks like this:
...
<td class="tablerow" colspan="3">
<INPUT type=radio name="ticket" value="22" >ramdom1
<INPUT type=radio name="ticket" value="1" >ramdom2
<INPUT type=radio name="ticket" value="3" >ramdom3
<INPUT type=radio name="ticket" value="99" >ramdom4
</td>
...
I was trying to select the input using xpath as follows:
String xpath = "//input[contains(#name, 'ticket') and contains(#value, '3')]";
WebElement rb = driver.findElement(By.xpath(xpath));
But selenium doesn't found the element.
If change it to
String xpath = "//input[contains(#name, 'ticket')]";
List<WebElement> rbs = driver.findElements(By.xpath(xpath));
or
String xpath = "//input[contains(#value, '3')]";
List<WebElement> rbs = driver.findElements(By.xpath(xpath));
It works, selenium returns a list of elements, including the one that I need. The problem occurs only when I try to use both conditions in a same xpath.
Of course that I could iterate over the list and test each value, but I would like to understand if I'm doing something wrong or not. Since IE doesn´t have native xpath support, I thought this could be a selenium implementation issue.
I'm using Selenium WebDriver (2.37.1) with IE Driver.
Not sure whether this is a Selenium implementation issue but this should work:
"//input[contains(#name, 'ticket')][contains(#value, '3')]"
The use of and is basically the same so the result should be correct here.
I am unsure why that doesn't work, and this technically isn't an answer, but you can replicate precisely what Selenium does to ensure it's not Selenium or any of it's tools at fault.
Selenium uses a library called "Wicked Good XPath" for a Javascript-based implementation of an XPath engine because IE doesn't have a "native" one.
So, to reproduce the scenario, take a copy of your page and add Wicked Good XPath to the script headers. Documentation on the front page of that website is very simple, and very easy to follow.
Once loaded in IE, open the Developer Tools and go into the Console. Wicked Good XPath will need to be "initialised" as such, and therefore you'll need to call wgxpath.install() in the console.
Once done, you now have access to the same library that Selenium would be using. Now, you can call a function within IE's developer console to access the DOM using XPath:
document.evaluate("//input[contains(#name, 'ticket') and contains(#value, '3')]", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue
The exact element you need will be returned, at least for me.
Now, admittedly, you don't need XPath at all for this, you can get away with using CSS selectors:
input[name~='ticket'][value='3']
We can use the following css
1.css=input[value='22']
2.css=input[value='1']
3.css=input[value='3']
4.css=input[value='99']
For Checking the Radio Buttons.

How to type text on hidden text box in WebDriver with Java

I am trying to sendkeys to an input,but I don't know why it warns me like this:
org.openqa.selenium.InvalidElementStateException: Element must not be hidden, disabled or read-only (WARNING: The server did not provide any stacktrace information)
The HTML page source:
<span id="mini-7" class="mini-textbox mini-textbox-empty" style="border-width: 0pt; width: 342px;">
<input class="mini-textbox-input" type="text" autocomplete="off" style="width: 338px;">
<input type="hidden">
</span>
My code:
driver.findElement(By.cssSelector("#mini-7 > input.mini-textbox-input")).clear();
driver.findElement(By.cssSelector("#mini-7 > input.mini-textbox-input")).sendKeys("yy");
Then I change my code like this:
JavascriptExecutor jse = (JavascriptExecutor)driver;
((JavascriptExecutor) jse).executeScript("arguments[0].type ='text';",driver.findElement(By.xpath("//span[#id='mini-7']/input[2]")));
But this time it throws out js error. Why?
I use sendkeys to the first input,this input is not hidden
That input could still have css property visibility: hidden or display: none. That's what exception is telling you. Check all the properties with browser's dev tools.
First of all it needs to change the value of type attribute as text from hidden. Now, you are able to type on that text by using WebDriver. So, the overall code for typing with WebDriver using Java and Javascript as follows:
WebDriver driver = new FirefoxDriver();
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("document.getElementById('mini-7').setAttribute('type', 'text');");
driver.findElement(By.cssSelector("#mini-7 > input.mini-textbox-input")).clear();
driver.findElement(By.cssSelector("#mini-7 > input.mini-textbox-input")).sendKeys("yy");
For 1st input field which is not hidden, use absolute path for cssSelector.
What you can do in this case is making the element visible by modifying the css tag for
visibility: hidden
or
display: none
using javascript just before sending keys to it.

Categories