Unable to break the loop | Selenium Forum
M
Posted on 18/07/2016
in module 17, video 7, the loop keeps going and i dont know how to break it. What is wrong with my code below?

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Dice {
static WebDriver driver;
public static void main(String[] args) {
driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("http://www.dice.com");

driver.findElement(By.xpath("//*[@id='monetate_lightbox_contentMap']/area[1]")).click();
driver.findElement(By.id("search-field-keyword")).sendKeys("selenium");
driver.findElement(By.xpath(".//*[@id='search-form']/fieldset/div[4]/div/div[1]/button")).click();

driver.findElement(By.xpath(".//*[@id='myModal']/div/div/div[1]/button")).click();

int i =1;
String part1 = "//*[@id='dice_paging_top']/ul/li[";
String part2 = "]/a";


while(isElementPresent(part1 + i + part2)){
driver.findElement(By.xpath(part1 + i + part2)).click();
i++;
}

}
public static boolean isElementPresent(String xpathExp){
int count = driver.findElements(By.xpath(xpathExp)).size();
if(count==0)
return false;
else
return true;

}

}

M
Replied on 18/07/2016

[quote:2olk2i3l]while(isElementPresent(part1 + i + part2)){
driver.findElement(By.xpath(part1 + i + part2)).click();
i++;
}
[/quote:2olk2i3l]

as long as element is present loop will keep on running.

how many times do you want to run the loop?


M
Replied on 19/07/2016

there are 4 pages present and it keeps clicking page number 1, then page number 2,...until page 4 then it clicks on page 1, then 2, then so on then repeat the cycle...

I want to click until the last page then stops there. I also want to print out all the job titles from each page. Below code doesnt work and i dont know how to make it work. I really appreciate your help!

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Dice {
static WebDriver driver;
public static void main(String[] args) {
driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("http://www.dice.com");

driver.findElement(By.xpath("//*[@id='monetate_lightbox_contentMap']/area[1]")).click();
driver.findElement(By.id("search-field-keyword")).sendKeys("selenium");
driver.findElement(By.xpath(".//*[@id='search-form']/fieldset/div[4]/div/div[1]/button")).click();

driver.findElement(By.xpath(".//*[@id='myModal']/div/div/div[1]/button")).click();

int i =1;
String part1 = "//*[@id='dice_paging_top']/ul/li[";
String part2 = "]/a";
String part3 = "//*[@id='position";
String part4 = "']";

while(isElementPresent(part1 + i + part2)){
driver.findElement(By.xpath(part1 + i + part2)).click();
String text = driver.findElements(By.xpath(part3+ i + part4)).get(i).getText();
System.out.println("links are "+text);
i++;
}

}
public static boolean isElementPresent(String xpathExp){
int count = driver.findElements(By.xpath(xpathExp)).size();
if(count==0)
return false;
else
return true;

}

}


M
Replied on 19/07/2016

I had the problem too but solved Responsive image Used below code
---------------------------------------------------------------------------------------
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;


public class LinkNavigation_isElementPresent {
static WebDriver driver;
public static void main(String[] args) {

driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.manage().window().maximize();
driver.get("http://dice.com");

driver.findElement(By.xpath("//*[@id='search-field-location']")).clear();
driver.findElement(By.xpath("//button[@type='submit' and text()='Find Tech Jobs']")).click();


int i=2;
while(isElementPresent("//*[@id='dice_paging_top']/ul/li/a[text()='"+i+"']")){
driver.findElement(By.xpath("//*[@id='dice_paging_top']/ul/li/a[text()='"+i+"']")).click();
driver.findElement(By.xpath("//*[@id='dice_paging_top']/ul/li[8]/a")).click();

// extract/click all search results

i++;
}

}

public static boolean isElementPresent(String xpathExpression){
List<WebElement> allElements = driver.findElements(By.xpath(xpathExpression));
int count = allElements.size();
if(count==0)
return false;
else
return true;

}

}


M
Replied on 20/07/2016

this line of code where its supposed to click the next page button doesn't work for me.

driver.findElement(By.xpath("//*[@id='dice_paging_top']/ul/li[8]/a")).click();

does it work for you?


M
Replied on 21/07/2016

it is working yuri.


M
Replied on 21/07/2016

I want to extract all job tittles from all pages but it only gives me title for page 1, 2 and 4 then its stuck at page 4. Can you please look at my below code and see whats im doing wrong?

------------------------------------------------


import java.util.List;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Dice {
static WebDriver driver;
public static void main(String[] args) {
driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("http://www.dice.com");

//driver.findElement(By.xpath("//*[@id='monetate_lightbox_contentMap']/area[1]")).click();
driver.findElement(By.id("search-field-keyword")).sendKeys("selenium");
driver.findElement(By.xpath(".//*[@id='search-form']/fieldset/div[4]/div/div[1]/button")).click();
driver.findElement(By.xpath(".//*[@id='myModal']/div/div/div[1]/button")).click();

int i = 1;
while(isElementPresent("//*[@id='dice_paging_top']/ul/li/a[text()='"+i+"']")){
//driver.findElement(By.xpath("//*[@id='dice_paging_top']/ul/li/a[text()='"+i+"']")).click(); //click the page number to extract the job titles

List<WebElement> titles = driver.findElements(By.xpath("//h3/a[contains(text(),position)]"));

for (int k = 1; k< titles.size(); k++){
String text = titles.get(k).getText();
System.out.println(text);
}
driver.findElement(By.xpath("//*[@id='dice_paging_top']/ul/li[6]/a")).click(); // click the >, next button
i++;
}
}

public static boolean isElementPresent(String xpathExp){
int count = driver.findElements(By.xpath(xpathExp)).size();
if(count==0)
return false;
else
return true;

}

}


M
Replied on 22/07/2016

your code is giving me error right here
[code:3vqoh3we]
driver.findElement(By.xpath(".//*[@id='myModal']/div/div/div[1]/button")).click();[/code:3vqoh3we]

are you having the same problem?

where is this xpath pointing to?
[color=#FF0000:3vqoh3we].//*[@id='myModal']/div/div/div[1]/button[/color:3vqoh3we]


M
Replied on 22/07/2016

that line of code closes the sign up for job alert box. please refer to attached screenshot. can you please clear your cache and cookies to see if you have the alert box?

My issue is it does not print out all the job titles of all pages.

Responsive image

M
Replied on 22/07/2016

i'm not getting that alert box.

but see if it is in an iframe.


M
Replied on 23/07/2016

please comment out the line of code that closes the alert since you dont have it. Can you please check the logic? it does not print out all titles for all pages. thank you!


M
Replied on 25/07/2016

there are no jobs for selenium and hence you're getting empty result.


M
Replied on 25/07/2016

There are plenty of jobs for selenium. over 1000 jobs return for selenium search. Can you please run my code and see how it works for you?


M
Replied on 25/07/2016

for me it return 0 jobs.

leave this example try some other question. there seems to be a bug in website.


M
Replied on 26/07/2016

How about giving the location to be california and see if it returns you any job?


M
Replied on 29/07/2016

it is printing all the titles there are just empty spaces in the array.


M
Replied on 29/07/2016

My code works for you? why doesn't it work for me?


M
Replied on 31/07/2016

it is working there is nothing wrong with your code.

move on to next exercise.