How to click on <input type=file> across browsers using Selenium Webdriver? - selenium

I'm working on dealing with a file-chooser dialog using Selenium 2 - WebDriver. Believe it or not, my problem is NOT dealing with the OS-native file-chooser. That part I can handle!
The problem is getting Selenium to properly click on the "Choose File" button. Since the original source html is simply <input type='file'>, the browser determines how to render it as a field and a button. As a result, the placement and naming of the button changes depending on browser. I've got it working in Chrome, but only because Chrome places the button on the leftmost alignment and Selenium happens to click there by default.
Any ideas? It's not clear to me if an input of this type is truly navigable from within the DOM anyway...

The proper way to upload a file on any OS is to
Find the <input type='file'> element. You need not to worry about different implementations and exact positioning. Just find the element for example by xpath //input[#type='file']
sendKeys() or type() (or whatever method writes text into elements in your language) the path to file to that input element.
Sample Java code:
// find the input element
WebElement elem = driver.findElement(By.xpath("//input[#type='file']"));
// 'type' the file location to it as it were a usual <input type='text' /> element
elem.sendKeys("C://path/To/File.jpg");
This works on every OS and browser in WebDriver.

Have exactly the same situation with element <input type='file'>. In my case it is created using ExtJS.
I don't know whether you have solved this question or not but let me provide my solution.
JavascriptExecutor executor = (JavascriptExecutor)getDriver();
executor.executeScript("arguments[0].click();", element);
Neither sendKeys() or type() nor using ActionBuilder was helpful for me. The only JavascriptExecutor works like a charm.

I tested with the following element:
<INPUT style="WIDTH: 550px; background-color:yellow" type="file">
Results:
IE: doubleclick on any area of the element, the "Choose File" dialogue would occur;
Firefox: click on any area of the element, the "Choose File" dialogue would occur.

Related

Click hidden element using selenium webdriver

I need to click on the fields bug, epic, feature, issue and task which appear on click the "+" icon.
I inspect each of the elements and the see the same common xpath highlighted in the image in developer tool.
How do i find the xpath of each of the elements highlighted and click on them using selenium webdriver?
<body class="lwp">
<noscript><div class="absolute-fill flex-column flex-center"><h2>JavaScript is Disabled</h2><p>Please enable javascript and refresh the page</p></div></noscript>
<input type="hidden" name="__RequestVerificationToken" value="t6WnlqRwnH3QtDIOt2b3JiHWYI1V5vB3MheOCOTH7Fx6QaBNoXWz4k8P4luP9i7TYw70KVq4O2vuwa8DQRuLcdUxo_KKVjcmxorJTHAE3c42sdjYqojLZkOSNt1gad70mD0BBA2" />
<div data-componentregion="page" class="full-size"></div>
<div class="bolt-portal-host absolute-fill no-events scroll-hidden"></div>
</body>
You need to handle windows here because when you click "+" new window appearing with elements "bug","epic","feature",,,etc
should be parent window and "epic",,,, are child window
I could not identify unique xpath for the elements as all the elements had same xpath. using sikulix library solved my problem

isEnabled() function in selenium always returns true, when webdriver is running on sites developed using Angular 6

The isEnabled() function in selenium webdriver returns true always, when the webdriver is running on sites developed using Angular 6. All the answers relating to this topic that I found talked about the disabled not being an attribute and instead being written in the class of the button. Check this link for a detailed description.
Button enabled or disabled : How does webdriver decide?
But the code of the website that I am working on does not have it written in the class. Here is the code of my website.
<button _ngcontent-c61="" class="push-right-sm mat-raised-button mat-primary" color="primary" mat-raised-button="" disabled="">
<span class="mat-button-wrapper">SAVE</span><div class="mat-button-ripple mat-ripple" matripple=""></div><div class="mat-button-focus-overlay"></div></button>
How should I check if this element is enabled or not correctly?
Edit: Here is how I check it through my code.
WebElement button = driver.findElement(By.xpath("//button/span[contains(text(),'SAVE')]"));
if(button.isEnabled()){ System.out.println("The button is enabled."); }
Your locator is finding the SPAN inside the BUTTON and not the button itself. Try
//button[./span[.='SAVE']]
NOTE: You don't need to use contains() in this case since the entirety of the text is "SAVE" so I removed contains().
This basically reads as find a BUTTON that has a SPAN that has contained text equal to "SAVE". I tested it on the HTML you provided and it's working.

How to click on button when input type =Button in Selenium Web driver by using Java

How to click on button when input type is button , I am using below code, click on button is working but data is not saved.
driver.findElement(By.cssSelector("input[type='button'][value='Save']")).click();
driver.findElement(By.cssSelector("input[#type='button'][#value='Save']")).click();
driver.findElement(By.cssSelector("input[#type='button']")).click();
And below is the development code for your reference:
<input id="save_btn_expe" class="edit_forms_save_btn" type="button" value="Save">
WebElement setElement = driver.findElement(By.id("save_btn_expe"));
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].click()", setElement);
OR
driver.findElement(By.id("save_btn_expe")).click();
OR
driver.findElement(By.xpath("//input[#id='save_btn_expe']")).click();
By considering your statement "click on button is working". You can use the submit method to post form data. You can use two other alternates for click. Following code is in c#:
driver.FindElement(By.Id("save_btn_expe")).submit();
// Send enter key on the element.
driver.FindElement(By.Id("save_btn_expe")).SendKeys(OpenQA.Selenium.Keys.Enter);
Before trying the above code, check manual save to make sure there is no bug on save.
Since it's an input tag element, though the type is button .click() or .submit() will not work. You have to provide the Enter key as an input. Use following code :
driver.findElement(By.xpath("//input[#id='save_btn_expe']")).sendKeys(org.openqa.selenium.Keys.ENTER);
I hope this will work for you, as it did for me.
if using for in DOM, the selector below work for me :
Selenide.$("label[for=\"save_btn_expe\"]").click();

Webdriver unable to find element

I was unable to find the element (with id below) with Selenium even though it's visible in the html source page after successfully clicking on 'Search' button (using Selenium) of the previous page that has url as follows:
String url="https://sjobs.brassring.com/1033/ASP/TG/cim_advsearch.asp?partnerid=25314&siteid=5290";
driver.get(url);
if(driver.findElements(By.id("submit1")).size() != 0)
driver.findElement(By.id("submit1")).click(); // clicking on 'Search' button
if(driver.findElements(By.id("ctl00_MainContent_GridFormatter_YUIGrid")).size() != 0)
System.out.println("FOUND!");
String pageSource= driver.getPageSource();
"FOUND!" was never rendered, nor pageSource contained the element with the above id. I am using Selenium 2.3.3 and testing with latest versions of IE, Chrome, and Firefox webdrivers. Could someone please help? Thank you.
About 1/3 from the bottom of the target page are the followings (third line is location of the id):
<div id="ctl00_MainContent_GridFormatter_datatable" class="datatable">
<div id="THeadersDiv" style="display:none;">
<table id="ctl00_MainContent_GridFormatter_YUIGrid" class="basicGrid" border="0"> <!-- this is the element in question -->
I think I got it. I believe that the driver cannot find the element because there are two elements both with identical IDs. (which is terrible web code). I looked at the rest of the code, and it looks like the two elements also share the same class, and are the only two elements with that class.
Therefore, I believe that doing a By.className(".basicGrid") should work

Trouble locating a checkbox in an iframe with Selenium Web Driver

I'm trying to locate a checkbox in an iframe in a Javascript based website using Web Driver and python. I've tried locating by ID and XPATH and neither seem to work since I must be looking in the wrong frame. The checkbox is visible on the page to the user and selenium IDE also seems to come up with the same answer as me, but it still results in 'NoSuchElementException: Message: u'The element could not be found'
html for checkbox: input type="checkbox" onclick="CE.CESECUR.onClickFullSecurity()" id="cefullsecure"
selenium code:
_settings_ssl_locator = (By.XPATH, ".//*[#id='cefullsecure' and onclick='CE.CESECUR.onClickFullSecurity()']")
def click_settings_enable_ssl(self):
self.selenium.find_element(*self._settings_ssl_locator).select()
I think you you would need to switch to the frame first and then locate the element, do the task/action on the elements present on the frame and switch back to the previous frame.
WebElement checkBoxframe = driver.findElement(By.tagName("enter frame name here"));
driver.switchTo().frame(checkBoxframe);
//write your selenium code here for checkbox
driver.switchTo().defaultContent();