Not able To click in selenium browser with C# - 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();

Related

C# get value from hidden input

I've been trying to get the value from a hidden input and innertext from a span on a website (note that I don't have access to edit the HTML code).
Code from website:
<input type="hidden" name="hidv" value="1582912961">
<span class="valo">0.888</span>
I could use a code similar to this but don't know the correct use of it:
label5.Text = webBrowser2.Document.GetElementById("hidv").InnerText;
label6.Text = webBrowser2.Document.GetElementById("valo").InnerText;
First I'd put an ID on the input tag
<input id="myId" type="hidden" name="hidv" value="1582912961">
Then you could use JS, jQuery, etc, to select the value.
$('#myId').val();
$('input[name=hidv]').val()
also just a note Document.GetElementById requires an Id. Otherwise, you could've done Document.GetElementsByClassName("valo") if you wanted to use the class name instead of an Id attribute.

How do you access the elements repeating in Selenium within the <button>

There are 2 buttons on the different pages, BEREKEN and VERTEL... Part 1 of code works fine, I can access and click the button, Part 2 of the code does not work. I am not sure why.
Part 1:
<button type="button" class="btn btn-primary" ng-click="next(formstep1, 2);calculate(formstep1, 2);" ng-disabled=""><span>
Bereken
</span></button>
Code used in Selenium:
searchButton = driver.FindElement(By.CssSelector(".btn.btn-primary"));
searchButton.Click();
Part 2:
<button type="button" class="btn btn-primary" ng-click="next(formstep2, 3);calculate(formstep2, 3);" ng-disabled=""><span>
Vertel ons iets over de woning
</span></button>
Code used in Selenium:
searchButton = driver.FindElement(By.CssSelector(".btn.btn-primary"));
searchButton.Click();
I have had this problem when the original element, though not visible, is still part of the DOM. Therefor FindElement will locate the first element in the DOM which is not visible (in this case "Bereken") and return the error. If you search the DOM when the Vertel button is present you will probably find multiple elements with the class you are matching on.
For example the ng-click attribute is unique and you can match on a substring of its value like this:
//Bereken
berekenButton = FindElement(By.CssSelector("button[ng-click*='formstep1']"));
//Vertel
vertelButton = FindElement(By.CssSelector("button[ng-click*='formstep2']"));
^ matches on prefix, # matches on suffix, * matches on substring.

C# selenium cant find input in form

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

Finding Button within Button Group Class Using Selenium Webdriver in C#

My end goal is to be able to click Yes or No on a webpage.
Using fire bug I can see the Yes and No buttons reside in a class called "btn-group"
<div class="btn-group btn-group-default answer-toggle" aria-label="..." role="group">
<button class="btn ng-pristine ng-valid active btn-secondary ng-touched" btn-radio="true" ng-model="consultation.previousSoundVoid" ng-class="{'btn-secondary': consultation.previousSoundVoid}" type="button">
Yes
</button>
<button class="btn ng-pristine ng-untouched ng-valid" btn-radio="false" ng-model="consultation.previousSoundVoid" ng-class="{'btn-secondary': consultation.previousSoundVoid == false}" type="button">
No
</button>
</div>
I think I could find Element by XPath but I am trying to improve my Selenium skill.
I am hoping there is a way to first find the "btn-group" class then somehow choose Yes or No within that.
Or be able to create variables for both the Yes and No options. If I do:
var button = Driver.Instance.FindElements(By.ClassName("btn"));
It returns 21 options. I found that:
button[15].Text returns "Yes"
button[16].Text returns "No"
Instead of having to look through all 21 results and worrying about if the index changes, can I search the results by text somehow?
The best way is to identify those element using text and xpath since they do not have any unique id
//button[.='Yes']
Desperate to use cssSelector?
.btn.ng-pristine.ng-valid.active.btn-secondary.ng-touched
since it is a compound class
You can also go for nth-child() with css
[role='group']>button:nth-child(1)
whereas 2 will let you select the button with text No
Case 1: When answer-toggle is unique in the HTML page
Driver.Instance.FindElements(By.XPath("//div[contains(#class,'answer-toggle')]//button[contains(text(),'Yes')]")).Click();
This will first locate a div that has answer-toggle in its class name and the find a button that has text Yes
Case 2: answer-toggle is not unique in your HTML page the you can use the complete class name as follows
Driver.Instance.FindElements(By.XPath("//div[#class='btn-group btn-group-default answer-toggle']//button[contains(text(),'Yes')]")).Click();
or
Driver.Instance.FindElements(By.XPath("//div[#class,'btn-group btn-group-default answer-toggle']//button[contains(text(),'Yes')]")).Click();
Case 3: If there is only one button in your HTML page that has text "Yes"
Driver.Instance.FindElements(By.XPath("//button[contains(text(),'Yes')]")).Click();
Same thing applies for No button. You just have to replace Yes with No.
There are definitely multiple options to locate the buttons, but I think that answer-toggle class sounds a good thing to rely on:
var buttons = Driver.Instance.FindElements(By.CssSelector("div.answer-toggle button.btn"));

How to get from html the element of a register field by Name?

I'm trying to make a register program :
For example:
Button1 click event:
WebBrowser wb = new WebBrowser();
wb.Navigate("register site");
wb.Document.GetElementById("passwort").SetAttribute("value", textBox1.Text);
wb.Document.GetElementById("register").InvokeMember("click");
Here is the register page html code ( From view source ) :
<td><h5>Password:<br><input type="password" name="passwort"></td>
<td> <input type="submit" name="register" value="Registri"></td>
Is there any chance to get the element by his name?because it has no ID .
You can't access the elements directly by name, but you could access it by finding the input tags first, and indexing into the result to find the tags by name.
wb.Document.GetElementsByTagName("input")["passwort"]

Categories