If you want to solve the question, when you want, one should write out the pesel numbers on the form screen.
My code looks like this:
WebElement element = driver.findElement(By.id("pesel"));
element.sendKeys("74091943656");
Simple loop:
String[] values = {"88010901", "827272", "28272"};
for (String value: values) {
// code for loading the form
WebElement element = driver.findElement(By.id("pesel"));
element.sendKeys(value);
// continue with saving the form?
}
Related
Can anyone guide me, how to select a year and month drop-down using Java in Selenium?
Had used the code mentioned below, But it didn't work.
Java Code:
{
List<WebElement> NomDOBYear = driver.findElements(By.className("ui-datepicker-year"));
Select selectYear= new Select((WebElement) NomDOBYear);
selectYear.selectByVisibleText("1991");
WebElement NomDOBMonth = (WebElement) driver.findElements(By.className("ui-datepicker-year"));
Select selectMonth= new Select(NomDOBMonth);
selectMonth.selectByVisibleText("Nov");
}
Use
driver.findElement(By.className("ui-datepicker-year"));
instead of
driver.findElements(By.className("ui-datepicker-year"));
When you use driver.findElements(By.className("ui-datepicker-year")) you are storing return elements in a list(I am expecting that there are 2 or more elements with the same className as "ui-datepicker-year").
So, if that is the case then you should note that the constructor "Select" class of selenium takes a 'WebElement' as a parameter which can be the first or any element stored inside the list.
Then your code should be written like -
List<WebElement> NomDOBYear = driver.findElements(By.className("ui-datepicker-
year"));
Select selectYear= new Select(NomDOBYear.get(0));//first element of the list
selectYear.selectByVisibleText("1991");
WebElement NomDOBMonth = driver.findElement(By.className("ui-
datepicker-month"));// you got the class name wrong here
Select selectMonth= new Select(NomDOBMonth);
selectMonth.selectByVisibleText("Nov");
Otherwise if there is only 1 element with className "ui-datepicker-year" or "ui-datepicker-month" in the entire DOM then your code should be written like -
WebElement NomDOBYear = driver.findElement(By.className("ui-datepicker-year"));
Select selectYear= new Select(NomDOBYear);
selectYear.selectByVisibleText("1991");
WebElement NomDOBMonth = driver.findElement(By.className("ui-datepicker-month"));
Select selectMonth= new Select(NomDOBMonth);
selectMonth.selectByVisibleText("Nov");
I would still recommend using XPath for finding elements.
I am fetching date webelements from facebook and I am looping it by using the below code.
public class select_facebook {
public static void main(String[] args) throws Exception {
WebDriver driver = new FirefoxDriver();
driver.get("http://www.facebook.com");
List<WebElement> days = driver.findElements(By.xpath("//select[#id='day']"));
System.out.println(days.size());
for (int i=0;i<days.size();i++) {
System.out.println(days.get(i));
}
}
}
But I get output as
1
[[FirefoxDriver: firefox on XP (58765a0e-31a0-40bc-8565-3418ae54682c)] -> xpath: //select[#id='day']]
But same code in for loop if I use System.out.println(days.get(i).gettext());
It list all the dates 1 to 31.
My question is then why should I call this as
List<WebElement> days = driver.findElements(By.xpath("//select[#id='day']"));
Because even the size of the webElements is just :1
System.out.println(days.size());
instead I could have called it as
WebElement days = driver.findElement(By.xpath("//select[#id='day']"));
You will get list of Elements in return of
List<WebElement> days = driver.findElements(By.xpath("//select[#id='day']"));
because there could be more than 1 element by same id ('day').
You should have to focus on what you exactly need. If you want more elements use FindElements() and when you are interested in only one element then use FindElement().
If there are more number of elements and if you use FindElement() it returns you very first element and rest others are get neglected. So make sure about your requirement.
This is because an id value can be given to only one HTML element. In other words, multiple elements cannot have the same id value on the same page in valid HTML DOM only.
FindElement() it returns you WebElement:
WebElement SINGLEELEMENT= driver.findElement(By.SELECTOR("//TAGNAME[#ATTRIBUTENAME='ATTRIBUTEVALUE']"));
FindElements() it returns you WebElements i.e List<WebElement> of multiple elements. It return 1 if only one element present in it or multiple if presents more:
List<WebElement> LISTOFELEMENTS= driver.findElements(By.SELECTOR("//TAGNAME[#ATTRIBUTENAME='ATTRIBUTEVALUE']"));
You can put looping on it to get each one element and work on each individually.
I am trying to select all the divs withclass tile-consultation in (http://raspored.finki.ukim.mk/Home/Consultations), click each of them and extract some data from each div, I tried using:
List<WebElement> professors = driver.findElements(By.className("tile-consultation"));
ListIterator<WebElement> theListOfProfessors = professors.listIterator();
Thread.sleep(1000);
int i = 1;
while(theListOfProfessors.hasNext()) {
WebElement professorI = driver.findElement(By.cssSelector(".tile-consultation:nth-of-type(2)"));
professorI.click();
Thread.sleep(1000);
close = driver.findElement(By.cssSelector("button.btn-close"));
close.click();
Thread.sleep(1000);
}
but how can I change 1 to the 2nd, 3d and so on in a while loop?
You've already got the work done. You've already found the web elements and made a listiterator here:
List<WebElement> professors = driver.findElements(By.className("tile-consultation"));
ListIterator<WebElement> theListOfProfessors = professors.listIterator();
The findElements method will return a collection of elements matching the selector. There's no need for you to retrieve the elements again like you're trying to with driver.findElement(By.cssSelector(".tile-consultation:nth-of-type(x)" inside that loop. You merely need to iterate over the listiterator theListOfProfessors that you already created. E.g. something to the effect of
while(theListOfProfessors.hasNext()) {
WebElement elem = theListOfProfessors.next()
// do something with elem
}
I am trying to loop through a drop-down menu, I am able to get each and every listed element but when I use the .click() and .submit() commands, it selects the first one and does not continue any further. I am aware for it to continue any further, I need to reselect the drop-down arrow so the list becomes visible to selenium after every submit. I tried commenting to make it readable. ANY help is greatly appreciated.
I wrote down the following,
WebElement search = driver.findElement(By.id("search-form"));
WebElement arrowDownButton = search.findElement(By.className("dropdown-toggle"));
arrowDownButton.click();
WebElement menu = search.findElement(By.className("dropdown-menu"));
List <WebElement> listOptions = menu.findElements(By.tagName(LI)); //selecting the listed elements , LI is "li" (declared previously)
int numberOfCountries = listOptions.size();
log("We have " + numberOfCountries + " entires");
int i=0; //for looping
//store into an array because web elements disappear
WebElement []listOfCountries = new WebElement[listOptions.size()]; //making an array of size listed elements
for (WebElement aOption : listOptions)
{
listOfCountries[i] = aOption; //saving the value into an array
String dataValue = aOption.getAttribute("data-value"); // what country am I wanting click
i++;
}
for(WebElement country : listOfCountries)
{
log(country.getText()); //log is a another function executing System.out.println
country.click(); //clicking on the web element
country.submit(); // submitting the element
arrowDownButton.click(); //reselecting the drop-down menu -> Why isn't this working?
}
}
Here are some methods I have already tried:
Instead of creating, saving and iterating through an array, I just directly tried to click the element. However, it also just clicks the first element.
Here is the error I am receiving:
Exception in thread "main" org.openqa.selenium.ElementNotVisibleException: Element is not currently visible and so may not be interacted with arrowDownButton.click(); but I cannot figure out WHY it fails to RESELECT.
for(WebElement country : listOfCountries)
{
log(country.getText()); //log is a another function executing System.out.println
country.click(); //clicking on the web element
Thread.Sleep(500);
country.submit(); // submitting the element
// You have to reselect this element. Because doesn't make sense anymore.
// Try reselect next element
arrowDownButton.click(); //reselecting the drop-down menu -> Why isn't this working?
}
see if ur drop down menu is not in a select list, probably it is, in that case ur current code wont work. It will be helpful if you could post the Html code of the webelement ur trying to interact with.
Am using Eclipse, TestNG and Selenium 2.32.
List <<f>WebElement> elementOption = driver.findElements(By.xpath("//li[#role='option']"));
The code driver.findElements(By.xpath("//li[#role='option']")); returns all the elements which are not displayed as well. The above 'elementOption' now contains all the elements, even the elements that are not displayed in the webpage. We can use IsDisplayed along with findElement method which will return only the element which is displayed in the webpage. Is there anything similar to IsDisplayed that can be used with findElements which will return only the elements that are displayed?
In C#, you can create WebDriver extension method like this:
public static IList<IWebElement> FindDisplayedElements<T>(this T searchContext, By locator) where T : ISearchContext {
IList<IWebElement> elements = searchContext.FindElements(locator);
return elements.Where(e => e.Displayed).ToList();
}
// Usage: driver.FindDisplayedElements(By.xpath("//li[#role='option']"));
Or use Linq when you call FindElements:
IList<IWebElement> allOptions = driver.FindElements(By.xpath("//li[#role='option']")).Where(e => e.Displayed).ToList();
However, I am aware of that extension methods and Linq don't exist in Java. So you probably need to create you own static method/class using the same logic.
// pseudo Java code with the same logic
public static List<WebElement> findDisplayedElements(WebDriver driver, By locator) {
List <WebElement> elementOptions = driver.findElements(locator);
List <WebElement> displayedOptions = new List<WebElement>();
for (WebElement option : elementOptions) {
if (option.isDisplayed()) {
displayedOptions.add(option);
}
}
return displayedOptions;
}
If the elements which you are trying to retrieve contains attributes like style having display values, then you might need to just change your XPATH to get only displayed elements.
List <WebElement> elementOption = driver.findElements(By.xpath("//li[#role='option'][contains(#style,'display: block;')]"));
or
List <WebElement> elementOption = driver.findElements(By.xpath("//li[#role='option'][not(contains(#style,'display: none'))]"));