How to click the DOM element in selenium webdriver thru C# - c#

I m doing the website automation thru Selenium webdriver in Firefox. Everything is fine but I dont know how to click the radio button.
There are two radio buttons in the web (i) Family Information and (ii) Individual Information. Target information obtained in the Selenium IDE.
(i) name=indFamily
(ii) document.pebPostLogin.indFamily[1]
I can easily click the first one Family information thru following code:
driver.FindElement(By.Name("indFamily")).Click();
But dont know the C# command for the second one "Individual Information". I have recorded the actions in Selenium IDE in Firefox and exported into C# file but DOM commands are not exported in C#. Following error message seen in C# file.
// ERROR: Caught exception [Error: Dom locators are not implemented yet!]
Please find below the source code identified thru Firebug.
<input name="indFamily" tabIndex="6" onkeypress="submitOnEnter(window.event.keyCode, document.pebPostLogin)" type="radio" value="Family"/>
<input name="indFamily" tabIndex="7" onkeypress="submitOnEnter(window.event.keyCode, document.pebPostLogin)" type="radio" value="Individual"/>
Please help me out...

It has been fixed using Xpath. The code is below:
driver.FindElement(By.XPath("//input[#value='Individual']")).Click();
Thanks to Alexander and http://www.w3schools.com/xpath/xpath_syntax.asp

Try following code:
driver.FindElement(By.XPath("//input[#name='xxxx' and #value='xx']")).Click();
xxxx = element name
xx = value (for eg., yes)

Related

Unable to find CheckBox items with SeleniumWebDriver using C#

I need to write some AUTOMATED TCs for an web application but I can`t find using SeleniumWebDriver a checkbox button .
I tried with Xpath, ClassName but nothing helps me
<label class ="select-label custom-control-label" for="77486440">
::before
::after
</label>
I expected to check / click on CheckBox item
I fail to see any checkbox in the code you've posted, my expectation is that it should be something like <input type="checkbox"> there so I would recommend taking a closer look into your page source.
With regards to the aforementioned label you can locate it using i.e. XPath selector like:
//label[contains(#class,'select-label')]
More information:
XPath Tutorial
XPath Operators & Functions
Selenium - Locating Elements - Locating by XPath

Finding CSS selector path for Selenium C#

I am new to Selenium C# automation. Tried finding on web but did not get any help.
The html code looks like this. I need to find the element and then click it using CSS. The site only runs on IE.
<tbody>
<tr class="t-state-selected">
<td>Purchased</td>
<td class="">768990192</td>
I know web links can disappear, but here are a few I use when trying to figure out how to locate elements using Selenium's C# WebDriver:
https://automatetheplanet.com/selenium-webdriver-locators-cheat-sheet/
https://saucelabs.com/resources/articles/selenium-tips-css-selectors
https://www.packtpub.com/mapt/book/web_development/9781849515740/1
The bottom line is that you're selecting by id, class, or XPath. Each of these can be tested directly on the page using the F12 browser tools. For example, to find the first comment on your question above, you could try this in the console:
$x("//div[#id='mainbar']//tbody[#class='js-comments-list']/tr")
Here's another SO post with a quick and dirty answer.
And here is the official documentation from Selenium on how to locate UI elements.
To click on the number 768990192 which is dynamic we have to construct a CssSelector as follows :
driver.FindElement(By.CssSelector("tr.t-state-selected td:nth-of-type(2)")).Click();
You're really not giving us much info to work. I will try my best to accommodate. Even though the presented HTML is not enough to give an indication of the format and you've not presented any code of your current solution.
string url = "https://www.google.com";
IWebDriver driver = new InternetExplorerDriver();
driver.Navigate().GoToUrl(url);
driver.FindElement(By.XPath("//tr[#class='t-state-selected']")).Click();
This little code snippet.
Creates a internet explorer driver.
Goes to the url of your choice.
And then clicks the table row that has a class that equals "t-state-selected'. Which my guess is all or none of the table rows.

Selenium-Webdriver unable to find element in webpage after a page load using C#

I am trying to automate an web application using Selenium Webdriver and C#. Currently I am stuck with a simple issue. After navigating to a particular page, I am trying to click a link which has a link text of "Manage groups". However when I am executing the test it is failing to find the element in the web page.
driver.FindElement(By.LinkText("Manage groups"));
I also tried using absolute Xpath:
driver.FindElement(By.XPath("html/body/div[1]/div[1]/div[2]/div[2]/article/li[2]/a"));
In both the cases the test navigates to the page then fails with error:
unable to find the element
The issue may be caused by a small mistake in 'Manage groups' text (e.g. there may be an addition whitespace).
Can you please provide at least little part of html (you can get it by right-click on your 'Manage groups' link and selecting 'Inspect Element' option )
if not - try these ones:
By.XPath("//a[#contains(., 'Manage groups')]")
By.XPath("//a[#contains(., 'manage groups')]")
By.XPath("//a[#value='Manage groups')]")
By.partialLinkText("Manage groups")
It should work with that :)
IWebElement ClickNext = driver.FindElement(By.XPath("//a[.='Manage groups']"));
// We get the link to click on
ClickNext.Click();
Edit : Try that li instead of a
IWebElement ClickNext = driver.FindElement(By.XPath("//li[.='Manage groups']"));
// We get the link to click on
ClickNext.Click();

ElementNotVisibleException try to use Xpath to Enter data into a textbox with Selenium

I am using Selenium in C# to Enter data into textboxes on a webpage:
But i am getting this error:
OpenQA.Selenium.ElementNotVisibleException: Element is not currently visible and so may not be interacted with
I'm using #name, but there are 2 controls on the page with name="MinPrice"
heres the HTML:
<div class="form-group for-sale">
<label>Min Price</label>
<input class="form-control" name="MinPrice" min="0" placeholder="Minimum Price" value="" type="number"></input>
and this is the xpath I'm using:
txtMinPrice = Driver.Instance.FindElement(By.Name("MinPrice"));
I also tried using XPath, but similar results:
txtMinPrice = Driver.Instance.FindElement(By.XPath("//input[contains(#name,'MinPrice') and type='number']"));
If anyone has any type of idea....this is driving me nuts.
ElementNotVisibleException exception occurs when selenium can find an element in the DOM but it is not rendered on the screen.
When I have encountered this error before it has been generally caused by one of three things:
Selenium is trying to interact with an object that is present in the DOM but has not yet rendered on the screen, in which case you might consider adding some type of delay. (Avoid sleep if you can but it is useful for debugging)
The element is below the visible screen, in which case you would need to scroll to interact with it.
There is an overlapping element that is blocking the display of the element.
Add a sleep(10) in to make sure everything on the page has loaded first before any user actions are preformed. If that doesn't work also add
driver.manage().window().maximize() at the start of your test to make sure all the page elements is in view.
If that doesn't work its your xpath. Try something like //*[#class="form-group for-sale"]/input
Or use the Firefinder add on in mozilla firefox to check your xpath is valid and exists on the page.
Selenium is good at scrolling down to view an item, but when it comes to Scrolling back up it's a PiA, and usually throws that exception. I usually just do something like
element.SendKeys(Keys.Home);
Thread.Sleep(100);

Selenium find by id doesn't work extranje access to the input

I am doing screen scraping with selenium. there i have a problem,
I'm trying to access to an HTML input (by id), but c# told me that is not visible, but I can see it.
this is the c# exception:
Element is not currently visible and so may not be interacted with
but viewing this I tried to do with selenium extension of Firefox, and the command to access is strange.
how can I access to this input from my c# code?
thanks.
edit1: this is the HTML:
<input type="text" value="" name="pxfirstname1" id="pxfirstname1" style="background-color: rgb(255, 255, 255);">
Also i checked on the CSS and doesn't have any hidden option.
Edit2: I tried to export to C#, but this is the result:
this is the code:
//code
driver.FindElement(By.Id("conSelC12")).Click();
driver.FindElement(By.Id("submitInfo")).Click();
// ERROR: Caught exception [Error: Dom locators are not implemented yet!]
// ERROR: Caught exception [Error: Dom locators are not implemented yet!]
driver.FindElement(By.Id("searchInf")).Click();
//more code
// ERROR: Caught exception [Error: Dom locators are not implemented yet!]
the other commands works fine
You should be able to export the code to C# from the plugin and then you can compare their suggested C# against your existing C# and see the problem or at least have working C# code

Categories