How to make the hidden element visible using java script executor - java

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);

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();

Cannot click on input type = "radio" that is inside a HTML label?

I have a problem where I cannot click on the input type = "radio" using the id="same-supplier-no":
<label for="same-supplier-no" ng-class="{checked: !ctrl.energyModel.BillViewModel.SameSupplier}" class="">
<input type="radio" id="same-supplier-no" name="same-supplier" ng-model="ctrl.energyModel.BillViewModel.SameSupplier" ng-value="false" ng-change="ctrl.sameSupplierValueChanged(); ctrl.analyticsProvider.sendVirtualPageView('SameSupplierNo')" class="ng-pristine ng-untouched ng-valid" value="false">
<span></span>No
</label>
In Selenium when I do:
driver.findElement(By.id("same-supplier-no")).click();
it says that "element not visible". There are previous radio buttons in the HTML that have labels on it and they have ID's for the labels (this one does not).
I just refer to the label id and click that instead and it works. But this one doesn't have an ID for the label and I want to avoid xpath usage, however, if I have no choice, then I will just select via xpath/css.
I have also tried via locator name i.e. By.className("same-supplier"); and using a For loop and if statement to store the input type=radio element, still doesn't work.
Question is: why can I not click the input type = "radio", why is in not visible to Selenium? Is it because it is nested inside a Label? If so, does anyone know how I can click it using the id or just getting to the element?
Thank you.
Note: I am using Java 7 for Selenium.
From the chat conversation I could try some stuff on the website you are using. The radio never becomes visible. As a result of this it is impossible to use the click() method on it.
The label that contains this radio is visible however. This means you could just use click() on this label.
driver.findElement(By.cssSelector("label[for='same-supplier-no']")).click();
Or if you really want to find the element using the radio you could use xpath.
driver.findElement(By.xpath("//*[#id='same-supplier-no']/parent::label")).click();
use action to click on the lable and after that use:
Actions actions = new Actions(driver);
IWebElement menuHoverLink = driver.findElement(By.XPath("//label[#for='same-supplier-no']"));
actions.moveToElement(menuHoverLink);
actions.click();
actions.perform();
driver.findElement(By.id("same-supplier-no")).click();
After you click on "find postcode" button, add below two line of code -
WebDriverWait wait = new WebDriverWait(driver,10);
wait.until(ExpectedConditions.visibilityOf(driver.findElement(By.xpath(".//*[#id='same-supplier-question']/div/div/label[2]/span"))));
This is has worked at my end...
You can change the xpath from absolute to relative....

input does not appear in firefox

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 :)

Wait in Selenium not functioning for a given element

I am attempting to make WebDriver wait for an element to appear, but I only have a few bits of code to go off. Heres the html body content for the button;
<input class="btn" value="Analyze" type="button" data-bind="click:$root.windowsAnalysis.analyzeFilesClick,enable:$root.windowsAnalysis.analyzeFilesEnabled">
I have added the below line to my test but it is not making the WebDriver 'Wait'.
new WebDriverWait(Login.driver,10).until(ExpectedConditions.visibilityOf
(Login.driver.findElement(By.cssSelector("input[#class='btn'][#value='Analyze']"))));
Any ideas?
Many Thanks in advance
The Problem is with your CSS path,
By.cssSelector("input[#class='btn'][#value='Analyze']");
the # before the class and value is not valid, it should be,
By.cssSelector("input[class='btn'][value='Analyze']");

How to identify an element in selenium even if xpath is same

I am new to selenium and trying to autoamte a web application in junit framework. As many get some problem in identifying web elements,I too stuck at a point where two submit buttons are having same xpath and css selector.
The only difference what I can observe is.. In the two form tags, I can see that className is different(for first form tag it is "feature_space_checkbox" and for second form tag it is "auto_fs_steps_checkbox")
As, I need to identify the second submit button..So I tried to identify the second submit button as below
driver.findElement(new ByChained(By.className("auto_fs_steps_checkbox"),By.xpath("//*[#id='edit_brochure_2863']/input[3]")));
When I try to execute this, I got the error as
org.openqa.selenium.NoSuchElementException: Cannot locate an element using By.chained({By.className: auto_fs_steps_checkbox,By.xpath: //*[#id='edit_brochure_2863']/input[3]})
Can anyone please correct me where I made the mistake
Adding DOM for this scenario
<form action="/brochures/2865/feature_space_checked" class="feature_space_checkbox" id="edit_brochure_2865" method="post"><div style="margin:0;padding:0">
<input name="commit" type="submit" value="Submit">
</form>
For the second submit button it is..
<form action="/brochures/2865/update_auto_fs_steps" class="auto_fs_steps_checkbox" id="edit_brochure_2865" method="post"><div style="margin:0;padding:0">
<input name="commit" type="submit" value="Submit">
</form>
Firstly, XPath and CSS selectors are not definitive. There are many XPath and CSS for every element on a page so to say they have the same Xpath and CSS selectors is incorrect.
For your example, is there any need to use XPath or combine two selectors?
The following CSS would work;
form.auto_fs_steps_checkbox input
There is no need to use chaining as this can all be expressed in XPath:
//*[#id='edit_brochure_2863' and #class='feature_space_checkbox']/input
So this will be in Java:
driver.findElement(By.xpath("//*[#id='edit_brochure_2863' and #class='feature_space_checkbox']/input"));
Of course, for the second submit button it will be
driver.findElement(By.xpath("//*[#id='edit_brochure_2863' and #class='auto_fs_steps_checkbox']/input"));
xpath for the second submit would be
driver.findElement(By.xpath("//form[#class='auto_fs_steps_checkbox']/input"));
This is enough to identify the second button as here class name is unique and id is same for both. So its better we do it by class name .

Categories