Access invisible textinput element with Selenium - c#

I am trying to access an input element, but VS throws me the following exception when I try to click or sendkeys into it:
An unhandled exception of type 'OpenQA.Selenium.ElementNotVisibleException' occurred in WebDriver.dll
Additional information: element not visible
After googling I found it that this is probably a invisible input element and I need to do it with a JavaScript Driver. I am not that good at C# and I have never worked with Selenium before, thus I fail to implement this part on my own, so I hope you can help me.
This is what I see in the source code of the form when I inspect the element:
<span class="origin-ux-textbox-control origin-ux-control">
<span>
<input id="ebi_email-input" maxlength="256" type="text" name="" value="peterparker#web.de" autocorrect="off" autocapitalize="off" autocomplete="off"></span>
</span>
This is what I tried:
driver.FindElement(By.Id("ebi_email-input")).Click();
driver.FindElement(By.Id("ebi_email-input")).Clear();
driver.FindElement(By.Id("ebi_email-input")).SendKeys(email);
The exception is thrown at the first line.

I suspect some duplicate ids are being used. try to find how many elements are returned by FindElements method if more than one then I guess you can loop through and find the visible one and then send keys. Or, if you have to use javascript following should work as well.
IJavaScriptExecutor js = (IJavaScriptExecutor) driver;
IWebElement element = driver.FindElement(By.Id("ebi_email-input"));
js.ExecuteScript("arguments[0].setAttribute('value', 'youremailaddress')", element);

Related

I want to read a text and store it in a variable through selenium in C#

I have this button on which a text is written. The text on the button is coming from a database and changes regularly. I want to save this text in a variable for further processing. The html tag is given below
<button type="button" id="hvac1_sequence" class="btn btn-warning" style="width:60px;">Lag</button>
I want to save "Lag" in the variable through Selenium Webdriver using C#. I have tried these following codes but none of them worked as I experienced error.
var element = driver.FindElement(By.Id("hvac1_sequence")).Text
var element = driver.FindElement(By.ClassName("btn btn-warning")).Text
var element = driver.FindElement(By.XPath("//button[#id='hvac1_sequence']")).Text
I have also tried to save them in IWebElement rather than var. Still same error. Can anyone help me?
Edit 1 : Error is Something like Selenium cannot find the Id or Selenium cannot find the Xpath given
Edit 2 : Exact Error
An unhandled exception of type 'OpenQA.Selenium.NoSuchElementException' occurred in WebDriver.dll. Additional information: no such element: Unable to locate element: {"method":"id","selector":"hvac1_sequence"}
The error was occuring because the frame was not selected. I used the following method to select the frame
driver.SwitchTo().Frame("temporary");
Where the temporary is the id of iframe

How to find two different elements but with same information using XPath

I'm trying to find an element that repeats itself in the same page. I tried to use the following XPath's that I was able to find in FirePath, but I didn't have any success running it through my selenium automation tests.
This are the two XPath:
//div[#id='selectGenericMult']/child::div/child::input']
(//*[#id='selectGenericMult']/child::div/child::input)[last()]
And this the information on my page:
<input class="ui-select-search input-xs ng-pristine ng-valid ng-touched" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" placeholder="Selecione" ng-disabled="$select.disabled" ng-hide="$select.disabled" ng-click="$select.activate()" ng-model="$select.search" role="combobox" aria-label="Select box" ondrop="return false;" style="width: 1331px;" type="text"/>
Screenshot with the two fields and part of the HTML code
Another screenshot with the the HTML code of both input text, showed on the first screenshot
Ok, so I finally had some time to look over it (not really, but made time) and this should work for both elements, per your screenshot. You may need to tweak them a lil', hopefully not though.
First Element:
//div[contains(#class,"ng-scope")]//input
Second Element:
//div[#class="col-md-12"]//input
I tested them on my end and they work fine. Let me know if it's not working for you somehow.
I would suggest looking into how xpath works and study it, it's actually quite fun once you get the hang of it. You are able to find anything on the screen with just some simple parameters.
Good luck!
Use below Xpath
//input[#class='ui-select-search input-xs ng-pristine ng-valid ng-touched' and #role='combobox' and #aria-label='Select box']
It will return you all element with same tag
Hope it will help you :)
To find the element as per the HTML you provided you can use the following unique xpath:
//input[#class='ui-select-search input-xs ng-pristine ng-valid ng-touched'][#placeholder='Selecione']

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

How do I access this element in C# using Selenium?

I am trying to find the following element and enter text into it. I have tried a number of different ways to access the element but always get the same error. My current line of code
searchTerm = driver.FindElement(By.Id("keyword"));
generates the same error
Unable to locate element: {"method":"id","selector":"keyword"}
The element, shown below, clearly has the Id 'keyword'.
<input maxlength="100" size="20" value="" name="keyword" id="keyword" title="keyword" class="FORMshrt2">
I used firebug to capture the complete XPath for this element.
/html/body/div/span/table[3]/tbody/tr/td/table[1]/tbody/tr[2]/td/div[1]/span/form/div[3]/table[3]/tbody/tr[2]/td/table/tbody/tr[1]/td/table/tbody/tr[11]/td[2]/span/input
How do I access this element?
Try closing tag input with /> like explained here.
<input maxlength="100" size="20" value="" name="keyword" id="keyword" title="keyword" class="FORMshrt2" />
I don't know if Selenium expects this closing tag, but everything else look ok.
The element might not have appeared at the moment you've started looking for it. Wait for the element to become present in the DOM:
IWebElement element = new WebDriverWait(driver, TimeSpan.FromSeconds(timeOut)).Until(ExpectedConditions.ElementExists((By.Id("keyword"))));
Use wait statement then try the below code
you can just try the following code, which may help for your case,
Thread.Sleep(5000);
driver.FindElement(By.XPath("//input[#class='FORMshrt2']")).Click();
driver.FindElement(By.XPath("//input[#class='FORMshrt2']")).SendKeys("your text");
By using your class name i'm identifying the element, first clicking on it and then passing the string.

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