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.
Related
I have a big problem and i need solution
I have html code on the web page:
<input class="fPersonalInput" name="email" id="email" value="" data-value="adres e-mail" maxlength="33" type="text">
I don't know how to set focus on this element
Any help is much appreciated........Thanks in advance
Use selenium.FindElement(by.XPath("xpath"))
To get the xpath of the element use the developer tool of chrome and do right-click in the element Copy>Copy XPath
I think what you need is to:
1) Get the element reference
2) Call Focus() method
(Browser.Document.GetElementById("email") as GeckoHtmlElement).Focus();
However, if you need to set the focus in order to set its value, be sure not to use any 'SendKeys()' method or similar, but rather just set the value like that:
(Browser.Document.GetElementById("email") as GeckoInputElement).Value = "busy#the.moment";
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']
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);
I am new to Selenium. In a specific scenario I am not able to catch an input element. below is the code:
<ul class = "form1">
<li class="firstName">
<input placeholdervalue="First name" tabindex="1" placeholder="First name" class="text" placeholdevalue="First name" data-input-rule="name" data-value-rule="required" maxlength="20" type="text">
</li>
</ul>
I want to locate input element. I tried locating it using locator By.ClassName, By.CssSelector("input[class='text placeholder']") and also tried:
wait.Until(ExpectedConditions.ElementIsVisible(By.CssSelector("input[className='text' and placeholdevalue='First name'"))).SendKeys("Vipul");
but input element is not getting selected.
Please let me know the right way to select input element.
I would use a dot notation to match the classes of ul, li and input elements:
By.CssSelector("ul.form1 li.firstName input.text")
If the element still cannot be found, then there could two most commonly met reasons:
it is inside an iframe and you need to switch to it
you need to wait for the element to appear
I'll expand these items in case you would still have problems finding the element.
Thanks for the reply.
It worked with this,
driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(20));
wait.Until(ExpectedConditions.ElementIsVisible(By.CssSelector("ul.form1 li.firstName input.text"))).SendKeys("Vipul");
Is this the right way of using wait?
Also, can anybody point out to resources which explain working with frames using selenium webdriver?
-Amit
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);