Selenium WebDriver how to insert text into field - java

I'm designing tests in Selenium WebDriver Framwework and I would like to insert text into fields in login box.
Here is a website www.fitandjump-widzew.pl
After click on "Zaloguj" button in top right corner, Login box appears.
I would like to insert text into E-mail inputfield.
Here is my code:
WebElement emailInput = driver.findElement(By.xpath("//[#id=\"inputFields\"]"));
emailInput.click();
emailInput.sendKeys("grzegorzrudniak#gmail.com");
After execution I get this error:
org.openqa.selenium.ElementNotVisibleException: element not visible
Can anyone help me to insert text into this field?
Please take a look at second input in this box, it's called "Hasło".
Xpath of this two fields is the same.
Additional question is how to insert text into "Hasło" inputfield as well?

Just make sure the locator you are using identifies single element [Unique]. Use single quotes in xpath and use correct xpath, you haven't used any html tag after // I've used * which means it is valid for all HTML tags.
WebElement emailInput = driver.findElement(By.xpath("//*[#id='inputFields']"));
//emailInput.click(); // no need to click on the element, you can directly type.
emailInput.sendKeys("grzegorzrudniak#gmail.com");

Related

Select visible elements into dialog window

I have a Angular SPA application which is having several dialog windows:
Full code: https://pastebin.com/81ikb5gE
<mat-dialog-container aria-modal="true" class="mat-dialog-container ng-tns-c18-93 ng-trigger ng-trigger-dialogContainer ng-star-inserted" tabindex="-1" id="mat-dialog-12" role="dialog" style="transform: none;"><!----><mobileweb-inquiry-menu-dialog _nghost-shn-c52="" class="ng-star-inserted"><mobileweb-client-area _ngcontent-shn-c52="" id="clientarea" _nghost-shn-c3="">
.....
</mat-dialog-container>
I facing the following issue: Into this dialog I have the same html tag ids. Sometimes I have the same ids 4 times. The application is working fine bug I get always the first id and the rest are not found.
I use this code to open the dialog window:
WebDriverWait webDriverWait = new WebDriverWait(driver, 25);
System.out.println("Click on Button " + name + " using id locator " + buttonId);
WebElement webDriverElement = webDriverWait.until(ExpectedConditions.visibilityOfElementLocated(By.id(buttonId)));
webDriverElement.click();
Is it possible to isolate the driver instance of WebDriver only to the elements of the current active dialog?
For example is it possible to get all elements into the dialog window and cut all of the rest html elements?
Are there any other solutions?
The application is working fine bug I get always the first id and the rest are not found :-
for this issue you can try to use xpath axes or xpath indexing.
let's say you have a xpath like this :
//input[#class='some-class']
that represent 4 items, you can perfrom xpath indexing like this :
(//input[#class='some-class'])[1]
or
(//input[#class='some-class'])[2]
and for this :
Is it possible to isolate the driver instance of WebDriver only to the elements of the current active dialog? For example is it possible to get all elements into the dialog window and cut all of the rest html elements?
I would say to have a locator that represent that particular model dialogue.
and you can call getAttribute('innerHTML') and should represent the particular HTML content.
Xpath axes

How to identify an element through classname and click on it using Selenium and Java?

Click button via class because it has no ID . Or via value?
tried className , cssSelector , partialLinkText and LinkText but sadly did not work clicking the save button
System.out.println("Succesful in Saving Product ");
WebElement save = driver.findElement(By.className("bttn-positive save-button"));
save.click();
Should be able to click save button
we can not use the multiple class name in the className locator. So, you can use the XPath locator with the multiple class name as below (//input[#class='bttn-positive save-button'])
Code:
System.out.println("Succesful in Saving Product ");
WebElement save = driver.findElement(By.xpath("//input[#class='bttn-positive save-button']"));
save.click();
You can't pass multiple classnames while using driver.findElement(By.className("bttn-positive save-button")) and doing so you will face Invalid selector: Compound class names not permitted error.
To click() on the green button with text as Save you have to induce WebDriverWait for the elementToBeClickable() and you can use either of the following Locator Strategies:
cssSelector:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("input.bttn-positive[value^='Save'][type='button']"))).click();
xpath:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[contains(#class, 'bttn-positive') and starts-with(#value, 'Save')][#type='button']"))).click();
Try save.submit();
Submit buttons are used to submit the entire form to the server. We can either use the click () method on the web element like a normal button as we have done above or use the submit () method on any web element in the form or on the submit button itself.
In this case "save.click()" will be work, but some time to save any product on any app like eCommerce or Banking domain it will not working properly & one more important think click () cause a new page to load, this method will attempt to load the page . So better to use "save.submit()" if the current elements is form Or with in the form the this will be submitted. As up ur requirements submit () one is the better option.

How use the sendKeys(Keys.TAB) + sendKeys("text") for contact us form

How to fill in the few fields in a column, with commands sendKeys(Keys.TAB) and sendKeys("text").
For Example:
We have 3 fields in Contact Us form at the bottom of the page:
Name
Email
Message
And Submit button.
I have scroll the page to the "Name" field and filled in the "Name", TAB and want to filled in the another text in the next fields (email) by using the sendKeys(Keys.TAB)
That's how I tried it:
WebElement element = driver.findElement(By.cssSelector("#contact > div > div.main > div.form-block"));
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", element);
WebElement webElement = driver.findElement(By.xpath("//*[#id=\"edit-submitted-name\"]"));
webElement.sendKeys("Name");
webElement.sendKeys(Keys.TAB);
webElement.sendKeys("email");
webElement.sendKeys(Keys.TAB);
webElement.sendKeys("Message");
But it filling in all texts in the first field because webElement moving to first field ("Name").
I know, that I can use driver.FindElement(By.xpath(## next field ##)).sendKeys("text");
But I need exactly to use the TAB button for testing.
Maybe in Java exist the command, to type the text in the focused field? I can't find it.
You have to use Action class of Selenium and create a series of actions to use tab.
Also try all steps manually by clicking tab. sometime tab not work on webpages.

How to get the right Xpath for the <li> HTML element?

i should make Selenium to click on the element of drop down menu using Java and Inteliji. I should click on the "today" button. I tried to copy the xpath, use cssselector, i used extensions like xpath finder etc, no result. The element is <li> type, so i guess the problem is here. Any suggestions how to find the correct Xpath?
P.S. sorry for uploading the image, as a new user, i can't put them exactly in the text.
Drop down menu image
html code for the elements
You can't always get reusable XPath locator for selenium from the browser's tool. It returns an absolute XPath. You need to construct relative XPath for the elements.
Here you can learn about XPath and how XPath locators work.
The following locators based on the image you have posted.
XPath:
WebElement liToday = driver.findElement(By.xpath("//div[contains(#class,'daterangepicker') and contains(#class,'dropdown-menu')]/div[#class='ranges']/ul/li[text()='Today']"));
CSS Selector:
WebElement liToday = driver.findElement(By.cssSelector("div.daterangepicker.dropdown-menu > div.ranges > ul > li"));
After locating the element,
this part is for after you have clicked the date box and the dropdown is showing.
new WebDriverWait(driver,30).until(ExpectedConditions.visibilityOf(liToday));
liToday.click();

Selenium: how to get the value of hidden element which has all div tags

I would like to get the value of all div tags specified in attached. I have tried with all possible locators like classname etc, which is showing null. and tried with JavaScript also which is returning null.
Please see the screen shot and I need the selected text which is in blue color starts with "Enables enterprise IT to deploy networking services"
You need to research creating selectors as this isn't a difficult one. There are numerous approaches for this element, but here's one for you: $$("#offers-popover .description"). Obviously this is a CSS selector based on the $$ and you use getText from the Selenium API in order to scrape the element text, which is what I assume you are intending to do.
driver.findElement(By.css("#offers-popover .description")).getText();
Since your element is not visible you can try this:
String divText = driver.findElement(By.className("description")).getAttribute("textContent");
Or, if this is not the only element on the page with the class description:
WebElement popElement = driver.findElement(By.id("offers-popover"));
String divText = popElement.findElement(By.className("description")).getAttribute("textContent");

Categories