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";
Related
Im struggling with the problem, that i cant really figure out, where i should look for the indentification of an specific element. In a lot examples i found in the internet, most of the elements have clear ids, names and so on.
The webpage im testing right now, has elements which have no ids or names. They have most of the time just a "type", "class" and other.
I know, that i can use "class" as the identification, but after talking with a coworker today, he suggested me, not to use class in find element as those are CSS classes which appear more than one time on the webpage.
Heres an example of a inspect of a searchfield i would like to get the identification from.
<input type="text" class="mud-input-slot mud-input-root mud-input-root-text mud-input-root-adorned-start" placeholder="Search here" _bl_7285135c-aa68-4a79-981f-4ee1af405a95="">
I used for now this, which does work.
webDriver.FindElement(By.XPath("//input[#placeholder='Search here']")).SendKeys("Super");
But in other elements, im using "class" in XPath which i would like to change. Example of such is here:
Inspect of a text field:
<input type="text" class="mud-input-slot mud-input-root mud-input-root-text mud-input-root-margin-normal" placeholder="Name" _bl_b3249641-8126-4e48-a3fd-9fd64aa2fb80="">
And currently im finding the element with:
IWebElement TitleField = webDriver.FindElement(By.XPath("//input[#class='mud-input-slot mud-input-root mud-input-root-text mud-input-root-margin-normal']"));
My coworker mentioned also, that i can right click in spectace on an element, click on copy and choose there either "copy selector" or "copy XPath".
But when i click on "copy XPath", ill get this:
/html/body/div[2]/div/div/div/div[1]/div[2]/div/div/input
Or for selector this:
body > div.mud-layout > div > div > div > div.mud-toolbar.mud-toolbar-gutters.mud-table-toolbar > div.mud-input-control.mud-input-input-control.mt-0 > div > div > input
Is this something i could also use in FindElement?
What are another possible ways to identify a element in my example?
Locators automatically generated with dev tools are useless in most cases.
You have to learn how to create proper, exact, strong and efficient locators.
We mostly locating web elements with CSS Selectors or XPath.
In most cases you will have to use element tag AND some or several element attributes altogether to get an unique locator.
Very often you will have to indicate some unique parent element properties to reach that element.
So normally, in real life, element locators are looking like
//div[contains(#class,'ApplicationsView')]
or
//li[contains(#class,someClass')]//div[contains(#class,'thisClassName')]
or even like this
//div[contains(#class,'ObjectList')]//div[contains(#class,'ListItem')]/span/..//i[contains(#class,'active')]
You should try this
IWebElement menu = CurrentDriver.FindElement(By.CssSelector("div[class='menu-panel right']"));
Your first question :
Is this something i could also use in FindElement? - Yes You can. But that would be a terrible xpath since it is a absolute xpath. Tell your coworker that we should always use relative xpath.
Read here what is the difference between Absolute xpath and Relative xpath
Your next question :
What are another possible ways to identify a element in my example? - I would go with css selector.
for this HTML :
<input type="text" class="mud-input-slot mud-input-root mud-input-root-text mud-input-root-margin-normal" placeholder="Name" _bl_b3249641-8126-4e48-a3fd-9fd64aa2fb80="">
simply write css selector as :
input[placeholder='Name']
In your C# it would be something like this :
IWebElement NameField = webDriver.FindElement(By.CssSelector("input[placeholder='Name']"));
NameField.SendKeys("Your name");
I am using WatiN to test a site.
There is a checkbox that I need to verify whether it is checked or not, then act on it accordingly.
When the checkbox is checked, the HTML shows
<input type="checkbox" name="bean.enabledContentTypes" id="jive-form-choose-contenttypes-1" value="1" checked="CHECKED"/>
I see that I can use 'Find.ByID' or 'Find.ByName' and a bunch of other 'Find.By...' options, but there isn't one for 'Find.ByChecked'.
How can I Find by this apparently non-standard attribute?
My only idea (that I can't figure out) is to store the entire element, and then somehow check the Element as a string (not sure how to do this anyways) and see if Element.Contains("checked").
Seems like there's probably a more poignant way to do what I'm am after?
I found the answer:
Find.By("attribute name", "attribute value")
So for my purposes, it is:
ie.TextField(Find.By("checked", "CHECKED")).Click();
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 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.