How to get data from web page without scrolling using selenium webdriver - selenium

How to get all data from the webpage without using scroll or page up and down.
int n = 10;
for (int i = 0; i < n; i++) {
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[#type=\"text\"]")));
List<WebElement> FV = driver.findElements(By.xpath("//*[#type=\"text\"]"));
WebElement aa = FV.get(i);
String cc = aa.getAttribute("value");
System.out.println(cc);
After I run this code it displays blank.

Related

Search bar in Selenium

I am unable to select an item if there is only 1 result showing(see image 1). If there are 2 results, desired item can be selected(see image 2). It just proceeds to the next scenario which inputs the date. it inputs the dates in the search bar instead of the date field. but works perfectly when there are 2 results.
Image 1
image 1
Image 2
image 2
Here is my code:
java.util.List<WebElement> substituterlist = getDriver.findElements(By.xpath("//*[#id='select2-drop']/ul/li"));
int list = substituterlist.size();
for (int i = 1; i <= list; i++) {
WebElement subUser = getDriver.findElement(By.xpath("//*[#id='select2-drop']/ul/li[" + i + "]/div"));
String subUsername = subUser.getText();
System.out.println(subUsername.indexOf("-"));
String selectUsr = subUsername.substring(0, subUsername.indexOf("-") - 1);
if (substituteUser.equals(selectUsr)) {
waitForMoreTime(10);
subUser.click();

How can select different drop down value everytime in for loop

My aim is to add the different drop down value 3 times in the script.
By using for I am able to select and add the same value 3 times but want to select and add the different drop down value every time.
Right now i am able add the same skill three times but i want to add three.
Please suggest how can different value every time the loop executes
here is the code which i am using right now.
for(int i=0; i<=2; i++){
Select skill = new Select(m1.findElement(By.xpath(".//select[#id='skill']")));
skill.selectByValue(skills);
Select proficiency = new Select(m1.findElement(By.xpath(".//select[#name='proficiency']")));
proficiency.selectByValue("3");
m1.findElement(By.xpath(".//button[#id='addskill']")).click();
Thread.sleep(2000);
If you want add diffrent dropdown value 3 times use dropdown.selectByIndex(index).
Select skill = new Select(m1.findElement(By.xpath(".//select[#id='skill']")));
Select proficiency = new Select(m1.findElement(By.xpath(".//select[#name='proficiency']")));
for(int i = 1; i <= 3; i++){
skill.selectByIndex(i);
proficiency.selectByIndex(i);
m1.findElement(By.xpath(".//button[#id='addskill']")).click();
Thread.sleep(2000);
}
OR
For random value selection in the drop down items.You need know how many items are their in the dropdown first.
Select skill = new Select(m1.findElement(By.xpath(".//select[#id='skill']")));
List<WebElement> listOptionDropdown = skill.getOptions();
int dropdownCount = listOptionDropdown.size();
Select proficiency = new Select(m1.findElement(By.xpath(".//select[#name='proficiency']")));
List<WebElement> listOptionDropdown1 = skill.getOptions();
int dropdownCount1 = listOptionDropdown1.size();
for(int i = 1; i <= 3; i++){
int random = (int)(Math.random());
random = random*dropdownCount +1;
skill.selectByIndex(random);
int random1 = (int)(Math.random());
random1 = random1*dropdownCount1 +1;
proficiency.selectByIndex(random1);
m1.findElement(By.xpath(".//button[#id='addskill']")).click();
Thread.sleep(2000);
}

Selecting Date on a Page using selenium Webdriver

This is the website I am testing https://www.sydneyairport.com.au/go/car-parking.aspx , I am almost done , but have been stuck in a single issue .
I have selected "date and time" from entry-date section but cannot select "date and time" from exit-date section.
I am clueless why I am not able to it since its the same structure for both and I was able to do it for entry-date ,I don't understand what changes for exit date section . I am new to selenium and will appreciate if anybody helps me out .
This is what I have written to select date and time in entry date section .
public void selectDate(WebDriver driver, String fromDate, String toDate) {
// selects from date
WebElement dateButton = driver.findElement(By.id("period_picker_0"));
dateButton.click();
WebElement datepicker = driver.findElement(By.xpath("//div[#class='period_picker_days']/table/tbody/tr/td[1]"));
selectDate(datepicker, fromDate);
WebElement timeBox = driver.findElement(By.xpath("//div[#class='period_picker_work']/div[2]/input"));
timeBox.sendKeys("");
WebElement time = driver
.findElement(By.xpath(".//*[#id='timepicker_box_start']/div/div[2]/div/div[1]/div[13]"));
time.click();
// Selects to date
WebElement dateButton2 = driver.findElement(By.id("period_picker_1"));
dateButton2.click();
// dateButton.click();
WebElement datepicker2 = driver
.findElement(By.xpath("//div[#class='period_picker_days']/table/tbody/tr/td[2]"));
selectDate(datepicker2, toDate);
WebElement timeBoxEnd = driver.findElement(By.xpath("//div[#class='period_picker_work']/div[2]/input"));
timeBoxEnd.sendKeys("");
WebElement timeEnd = driver
.findElement(By.xpath(".//*[#id='timepicker_box_end']/div/div[2]/div/div[1]/div[13]"));
timeEnd.click();
}
public int selectDate(WebElement datepicker, String date) {
int ele = 0;
List<WebElement> rows_table = datepicker.findElements(By.tagName("tr"));
int rows_count = rows_table.size();
for (int row = 0; row < rows_count; row++) {
// To locate columns(cells) of that specific row.
List<WebElement> Columns_row = rows_table.get(row).findElements(By.tagName("td"));
// To calculate no of columns(cells) In that specific row.
int columns_count = Columns_row.size();
// Loop will execute till the last cell of that specific row.
for (int column = 0; column < columns_count; column++) {
// To retrieve text from that specific cell.
if (Columns_row.get(column).getText().equals(date)) {
ele = column;
Columns_row.get(column).click();
}
}
}
return ele;
}
I did lot of work for this code, You are using tr and td. I would suggest there is no need of it.
See the code I'm using, i am able to select both date easily. Hope this help you..
driver.get("https://www.sydneyairport.com.au/go/car-parking.aspx");
driver.manage().window().maximize();
driver.findElement(By.xpath(".//*[#id='period_picker_0']")).click();
Actions a = new Actions(driver);
WebDriverWait wait = new WebDriverWait(driver,20);
WebElement entrydate= driver.findElement(By.xpath(".//*[#id='body']/div[1]/div[2]/div[1]/table/tbody/tr/td[1]/table/tbody/tr[5]/td[5]"));
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(".//*[#id='body']/div[1]/div[2]/div[1]/table/tbody/tr/td[1]/table/tbody/tr[5]/td[5]")));
a.moveToElement(entrydate).build().perform();
Thread.sleep(5000L);
entrydate.click();
WebElement entrytime= driver.findElement(By.xpath(".//*[#id='timepicker_box_start']/div/div[2]/div/div[1]/div[15]"));
a.moveToElement(entrytime).build().perform();
Thread.sleep(5000L);
entrytime.click();
WebElement exitdate= driver.findElement(By.xpath(".//*[#id='body']/div[2]/div[2]/div[1]/table/tbody/tr/td[1]/table/tbody/tr[6]/td[5]"));
a.moveToElement(exitdate).build().perform();
Thread.sleep(5000L);
exitdate.click();
WebElement exittime= driver.findElement(By.xpath(".//*[#id='timepicker_box_end']/div/div[2]/div/div[1]/div[15]"));
a.moveToElement(exittime).build().perform();
Thread.sleep(5000L);
exittime.click();
Please do reply, how you find this and reply back to me for further query.
Happy Learning. :-)

Incrementing value with Selenium IDE

How do I increment value of img path when said path looks like this?
//ab[x]/img
X value increasing by 1 and has a limit of 50.
Trying to write a test case on how to click on several images on website.
Edit: Just wanted to add that I'm just starting with Selenium IDE and using standart commands.
Solution 1: Format your xpath path selector
for(int i=1; i<=numberOfImages; i++) {
String path = String.format("//ab[%d]/img", i);
WebElement image = driver.findElement(By.xpath(path));
if(image != null) {
image.click();
}
}
Solution 2: Select all elements that "//ab/img" returns and iterate over them.
String path = "//ab/img";
List<WebElement> imgElements = driver.findElements(By.xpath(path)); //notice the plural
for(WebElement image : imgElements) {
image.click();
}

dojo "Object does not support this action " in IE

I faced a strange problem in IE with Dojo EnhancedGrid.
The data for the grid I get from server with AJAX, then in load() method i'm trying to go through all the elements in the grid. And here things go mad in IE. Here is the method that tries to get elements :
var grid = dijit.byId(prefix + "mySuperGrid");
for (var i = 0 , l = grid.getTotalRowCount(); i < l; i++) {
item = grid.getItem(i);
}
In the first iteration I get
object does not support this action
In FireFox the same code works perfectly.
Ok I found the bad guy. The thing was that IE needs var keyword before variable name when firefox does not. So the code should be :
var grid = dijit.byId(prefix + "mySuperGrid");
for (var i = 0 , l = grid.getTotalRowCount(); i < l; i++) {
var item = grid.getItem(i);
}