C# selenium cant find input in form - c#

I am trying to locate a button in a form... but it doesnt find it...
I have tryed using threading.sleep(5000); but it dont help.
I have tryed so many ways but nothing helped me it always says that it cant locate the button...
here is my code:
IWebElement startb2 = driver.FindElement(By.XPath("//form/*[#name='start']"));
here is the html code of the form:
<form method="post" action="php/PickFruits.php">
<input type="submit" name="start" value="התחל לקטוף" class="button_submit" style="margin-right:-97px;">
</form>
I don't want to use the value because it is in Hebrew... and I cant use it in the c# console... please help me guys.
Edit:
Now its find the location of the input but it doesnt click on it... code:
IWebElement startb = driver.FindElement(By.XPath("//*[#type='submit']"));
startb.Click();

To switch to an iframe, use the below code.
//To find the iframe
IWebElement Object = driver.FindElement(By.XPath("//*[#class='active_iframe']");
//To switch to and set focus to the iframe
driver.SwitchTo().Frame(Object);
And then perform click operation.
IWebElement startb = driver.FindElement(By.XPath("//*[#type='submit']"));
startb.Click();

Related

C# Selenium WebElement.Click throws exception not interactable

enter image description here
enter image description here
enter image description here
enter image description here
When I start the program, it starts and runs fine until it goes to the point where I want the driver to click the button and it throws me the exception the web element not intractable. I've tried a lot of methods but none of them worked. The problem I think is that when this button is clicked the HTMl changes. Please help :D
this is the line of code that trows me the exception (C#) :
driver.FindElement(By.XPath("//button[#id='exifInfoMobile']")).Click();
error : OpenQA.Selenium.ElementNotInteractableException: 'element not interactable
(Session info: chrome=103.0.5060.114)'
and this is the HTML :
<li class="photo-info">
<button class="btn btn-photo-info" id="exifInfoMobile">
<span class="text">info</span>
</button>
</li>
I've tried with WebDriverWait , By class name and by id but it didn't work. I think it has something to do with a model because when this button is clicked, the sites html changes by adding an info panel on the right. Sorry again for the bad question but its my first time asking one here.
If a regular .click() does not work, you can click with JavaScript.
You can use this
var element = sel.driver.FindElement(By.Id("exifInfoMobile"));
((IJavaScriptExecutor)driver).ExecuteScript("arguments[0].click();", element);
or this
IJavaScriptExecutor js = (IJavaScriptExecutor)driver;
var element = sel.driver.FindElement(By.Id("exifInfoMobile"));
js.ExecuteScript("arguments[0].click();", element);

Not able To click in selenium browser with C#

Button type is Input.
<input type="submit" value="Send" class="wpcf7-form-control wpcf7-submit btn solid">
I want to click this Button using code, try to click on Button By ClassName.
My code is as per below but not get any success.
string url1 = "myUrl";
IWebDriver driver = new ChromeDriver(Application.StartupPath);
driver.Navigate().GoToUrl(url1);
driver.FindElement(By.ClassName("wpcf7-form-control .wpcf7-submit btn solid")).Click();
This throws the following error :
Compound class names not allowed. Cannot have whitespace in class name. Use CSS selectors instead. and not use single class because there is a other class with same name.
driver.FindElement(By.XPath("//input[#type='submit']")).Click();
I would try to avoid using XPath, Instead I would add an Id to your input.
The reason being XPath is more likely to be changed over time than Id.
Also if you add another element with the same HTML, selenium will not be able to differentiate between them and will always select the first element.
Using Id also makes your code more readable.
You can try this:
<input type="submit" value="Send" Id="YourButtonId" class="wpcf7-form-control wpcf7-submit btn solid">
And
driver.FindElement(By.Id("YourButtonId")).Click();

Execute a java script to make an element visible

I'm trying to automate the use of a website using Selenium, at one time I want to click on a hidden button defined this way :
<body>
<div class="smenu" id="smenu4">
<input tabIndex="-1" type="button" onclick="SearchEng" value="FindEng" />
<!--> Lots of inputs <!-->
</div>
</body>
I already tried to simply click the button, but it doesn't work. I can select it and retrieve information though. That's why I'm now trying to run a javascript to make the button visible before clicking it. Here is my code :
IWebElement element = driver.FindElement(By.XPath("//div[#id='smenu4']/input[#value=FindEng"));
String js = "arguments[0].style.height='auto'; arguments[0].style.visibility='visible';";
((IJavaScriptExecutor)driver).ExecuteScript(js, element);
The thing is nothing happens when I launch it. Is it possible that I can't run scripts ? Can you run them on any website ? - I use internet explorer 11, windows 7. Thanks !
Inspect the element in your Browser and check that what is the reason that the element is invisible. Or just simply compare the element when it is visible or not. Possible reasons:
The element is not in the DOM yet.
The element hasn't got width and/or height.
visibility parameter. (http://www.w3schools.com/cssref/pr_class_visibility.asp)
display parameter. (http://www.w3schools.com/cssref/pr_class_display.asp)
When you know the reason, you will know the solution :)

C# & WebDriver: Xpath Statement to a Web Driver Statement

I am attempting to click on a button using Selenium WebDriver using an Xpath Identifier.
The HTML for the button object is as follows:
<div class="button-wrap fr">
<a class="button blue-button submit"><span class="button-left"></span><span class="button-mid"><span class="text-button-ds">Email me the link<span>Email me the link</span></span></span><span class="button-right"></span></a>
</div>
I was able to get the Xpath for the button. It is as follows:
//*[#id="forget-password-form"]/div/div[3]/div[2]/a/span[2]
I would like to write a Web Driver statement that clicks the button using an Xpath statement.
However, what I have written is not working (See below)
submitButton = driver.FindElement(By.XPath("//div[#id='forget-password-form'][3]/[2]"));
submitButton.Click();
Thanks in Advance
As an FYI, the following lines located the button and clicked it:
submitButton = driver.FindElement(By.XPath("//span[#class='button-mid']"));
submitButton.Click();
Cheers

"SCRIPT5 Access is denied" error on IE9 when firing .click() from onchange

We want to reduce the number of steps it takes for a user to upload a file on our website; so we're using jQuery to open and postback files using the below markup (simplified):
<a onclick="$('#uplRegistrationImage').click();">
Change profile picture
</a>
<!-- Hidden to keep the UI clean -->
<asp:FileUpload ID="uplRegistrationImage"
runat="server"
ClientIDMode="static"
Style="display:none"
onchange="$('#btnSubmitImage').click();" />
<asp:Button runat="server"
ID="btnSubmitImage"
ClientIDMode="static"
Style="display:none"
OnClick="btnSubmitImage_OnClick"
UseSubmitBehavior="False" />
This works absolutely fine in Firefox and Chrome; opening the file dialog when the link is clicked and firing the postback when a file is selected.
However in IE9 after the file upload has loaded and a user has selected a file; insteaed of the OnChange working I get a "SCRIPT5 Access is denied" error. I've tried setting an arbitrary timeout, setting intervals to check if a file is given to no avail.
There are a number of other questions relating to this; however none appear to have a decent answer (One said set the file dialog to be transparent and hover behind a button!)
Has anyone else resolved this? Or is it absolutely necessary that I provide a button for IE users?
For security reasons, what you are trying to do is not possible. It seems to be the IE9 will not let you submit a form in this way unless it was an actual mouse click on the File Upload control that triggers it.
For arguments sake, I was able to use your code to do the submit in the change handler, but it worked only when I clicked the Browse button myself. I even set up polling in the $(document).ready method for a variable set by the change handler that indicates a submission should be triggered - this didn't work either.
The solutions to this problem appear to be:
Styling the control in such a way that it sits behind a button. You mentioned this in your question, but the answer provided by Romas here In JavaScript can I make a "click" event fire programmatically for a file input element? does in fact work (I tried in IE9, Chrome v23 and FF v15).
Using a Flash-based approach (GMail does this). I tried out the Uploadify demo and it seems to work quite nicely.
Styling a File Upload:
http://www.quirksmode.org/dom/inputfile.html
http://www.shauninman.com/archive/2007/09/10/styling_file_inputs_with_css_and_the_dom
References:
jQuery : simulating a click on a <input type="file" /> doesn't work in Firefox?
IE9 file input triggering using Javascript
getting access is denied error on IE8
Hey this solution works.
for download we should be using MSBLOB
$scope.getSingleInvoicePDF = function(invoiceNumberEntity) {
var fileName = invoiceNumberEntity + ".pdf";
var pdfDownload = document.createElement("a");
document.body.appendChild(pdfDownload);
AngularWebService.getFileWithSuffix("ezbillpdfget",invoiceNumberEntity,"pdf" ).then(function(returnedJSON) {
var fileBlob = new Blob([returnedJSON.data], {type: 'application/pdf'});
if (navigator.appVersion.toString().indexOf('.NET') > 0) { // for IE browser
window.navigator.msSaveBlob(fileBlob, fileName);
} else { // for other browsers
var fileURL = window.URL.createObjectURL(fileBlob);
pdfDownload.href = fileURL;
pdfDownload.download = fileName;
pdfDownload.click();
}
});
};
This solution looks like it might work. You'll have to wrap it in a <form> and get it to post in the jquery change handler, and probably handle it in form_load using the __eventtarget or and iframe or whatever it is that web forms uses, but it allows you to select a file, and by submitting the form, it should send it. I can't test it however, since I don't have an environment set up at home.
http://jsfiddle.net/axpLc/1/
<a onclick="$('#inputFile').click();">
Change profile picture
</a>
<div id='divHide'>
<input id='inputFile' type='file' />
</div>
$('#inputFile').change(function() { alert('ran'); });
#divHide { display:none; }
Well, like SLC stated you should utilize the <Form> tag.
First you should indicate the amount of files; which should be determined by your input fields. The second step will be to stack them into an array.
<input type="file" class="upload" name="fileX[]"/>
Then create a loop; by looping it will automatically be determined based on the input field it's currently on.
$("input[#type=file]:nth(" + n +")")
Then you'll notice that each file chosen; will replace the input name to the file-name. That should be a very, very basic way to submit multiple files through jQuery.
If you'd like a single item:
$("input[#type=file]").change(function(){
doIt(this, fileMax);
});
That should create a Div where the maximum file found; and attaches to the onEvent. The correlating code above would need these also:
var fileMax = 3;
<input type="file" class="upload" name="fileX[]" />
This should navigate the DOM parent tree; then create the fields respectively. That is one way; the other way is the one you see above with SLC. There are quite a few ways to do it; it's just how much of jQuery do you want manipulating it?
Hopefully that helps; sorry if I misunderstood your question.

Categories