3rd page is not click able in pagination - java

I have used below code for pagination, 2nd page is able to click but next pages are not able to click
List<WebElement> pagination = driver.findElements(By.tagName("i"));
List<WebElement> pagination1 = driver.findElements(By.xpath(".//[#id='ctl00_cpHFooter_PageUC1_rptrPager_ctl05_lnkNext']/i"));
pagination.size();
System.out.println("Total pages :" + pagination.size());
if(pagination .size()>0)
{
System.out.println("pagination exists");
//click on pagination link
for(int i=0; i<pagination1.size(); i++)
{
pagination1.get(i).click();
JavascriptExecutor jse1 = (JavascriptExecutor)driver;
jse1.executeScript("scroll(0, 2000);");
}
}
else
{
System.out.println("pagination not exists");
}

You have to start from page 2 and use some wait or sleep()
List<WebElement> pagination = driver.findElements(By.tagName("i"));
List<WebElement> pagination1 = driver.findElements(By.xpath(".//[#id='ctl00_cpHFooter_PageUC1_rptrPager_ctl05_lnkNext']/i"));
pagination.size();
System.out.println("Total pages :" + pagination.size());
if(pagination .size()>0)
{
System.out.println("pagination exists");
//click on pagination link
for(int i=2; i<pagination1.size(); i++)
{
pagination1.get(i).click();
Thread.sleep(5000);
JavascriptExecutor jse1 = (JavascriptExecutor)driver;
jse1.executeScript("scroll(0, 2000);");
}
}
else
{
System.out.println("pagination not exists");
}

Related

How to select multiple checkboxes in selenium(java)?

I am trying to select all checkboxes whose value is matched to my value.
Code is working fine when the web page has no vertical scroll. But if web page has some more data then the checkbox is not selected as I want.
Here is my code-
List<WebElement> rselect = tagdis1.findElements(By.className("row-selection-checkbox"));
System.out.println("Row selection Size- " + rselect.size());
List<WebElement> record = driver.findElements(By.id("$ctrl.item.id"));
System.out.println("Size- " + record.size());
int DocNameCount = 0;
for (int j = 0; j < record.size(); j++) {
String Pname = record.get(j).getText();
System.out.println("Pdf name- " + Pname);
if (Pname.equals(docName + ".pdf")) {
// here total 4 records i get but able to click only on 3 records
System.out.println(j + " " + Pname);
rselect.get(j).click();
Thread.sleep(2000);
}
}
Please use scroll option in your operation,
/*
* By
* scroll to the element and wait
*/
public void scroll(By element){
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].scrollIntoView(true);",driver.findElement(element));
log.info("Scrolling down");
}

Extract text and web links with the selenium WebDriver

I'm studying selenium and I want to extract the texts and links from Sympla's events, but when I click on the "more events" button, I can't extract the next events, it is always extracting the same initial events from the page.
Complete class for easy reproduction.
public static void main(String[] args) throws InterruptedException {
WebDriverManager.firefoxdriver().setup();
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.get("https://www.sympla.com.br/eventos?ts=online_mais-de-3-mil-eventos-online");
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
// If have captcha, close the page and exit.
boolean captcha = driver.getPageSource().contains("Não sou um robô");
if (captcha == true) {
System.out.println("O Captcha apareceu, acabou a brincadeira!");
driver.close();
driver.quit();
}
// load more button
WebElement CarregarMais = driver.findElement(By
.xpath("//button[#id='more-events']"));
// Number of events counter
List<WebElement> eventos = (List<WebElement>) driver.findElements(By
.cssSelector("div.event-name.event-card"));
System.out.println("Number of links: " + eventos.size());
// Number of links counter
List<WebElement> eventos_link = (List<WebElement>) driver
.findElements(By.cssSelector("a.sympla-card.w-inline-block"));
// iterating over the button more events
for (int j = 0; j < eventos.size(); j++) {
CarregarMais.click();
#SuppressWarnings("deprecation")
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(ExpectedConditions
.elementToBeClickable(By
.xpath("//button[#id='more-events']")));
// Iterating over event links
for (int i = 0; i < eventos_link.size(); i++) {
System.out.println(i + " " + eventos.get(i).getText() + " - "
+ eventos_link.get(i).getAttribute("href"));
Thread.sleep(500);
}
}
}
It's because you don't read the links again. With every click on the button a new page is created, so you need to read them again.
Furthermore you would need to store the last fetched link.
So after waiting for the button to be clickable again you need to reread eventos and eventos_link. And maybe you use a global variable like lastFetchedLinkIndex.
This would be my approach (adjusted your code):
WebDriverManager.firefoxdriver().setup();
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.get("https://www.sympla.com.br/eventos?ts=online_mais-de-3-mil-eventos-online");
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
// If have captcha, close the page and exit.
boolean captcha = driver.getPageSource().contains("Não sou um robô");
if (captcha == true) {
System.out.println("O Captcha apareceu, acabou a brincadeira!");
driver.close();
driver.quit();
}
// load more button
WebElement CarregarMais = driver.findElement(By
.xpath("//button[#id='more-events']"));
// Number of events counter
List<WebElement> eventos = (List<WebElement>) driver.findElements(By
.cssSelector("div.event-name.event-card"));
System.out.println("Number of links: " + eventos.size());
// Number of links counter
List<WebElement> eventos_link = (List<WebElement>) driver
.findElements(By.cssSelector("a.sympla-card.w-inline-block"));
int lastEventScraped = 0;
// iterating over the button more events
for (int j = 0; j < eventos.size(); j++) {
CarregarMais.click();
#SuppressWarnings("deprecation")
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(ExpectedConditions
.elementToBeClickable(By
.xpath("//button[#id='more-events']")));
eventos = (List<WebElement>) driver.findElements(By
.cssSelector("div.event-name.event-card"));
eventos_link = (List<WebElement>) driver
.findElements(By.cssSelector("a.sympla-card.w-inline-block"));
// Iterating over event links
for (int i = lastEventScraped; i < eventos_link.size(); i++, lastEventScraped++) {
System.out.println(i + " " + eventos.get(i).getText() + " - "
+ eventos_link.get(i).getAttribute("href"));
Thread.sleep(500);
}
}

How to make faster the loop to find ids

Have a scenario that was to collect all the div ids and loop them one by one to complete the iteration. I have done the scenario but it takes more time to pass all the ids.
Can you please suggest how to make it faster.
Below is my code snippet.
List<WebElement> listoftab = driver.findElements(by.xpath(".//*[contains (#id, 'tabZ')]/div/div[1]"));
Thread.sleep(1000);
String clas1 = "tablist";
String clas2 = "tabView";
for(int i =1; i<=110;i++){
boolean present;
try {
driver.findElement(By.xpath(".//*[#id='tabZ"+i+"']/div/div[1]"));
present = true;
if(clas1.equalsIgnoreCase(driver.findElement(By.xpath(".//*[#id='tabZ"+i+"']/div/div[1]")).getAttribute("class"))) {
tabloop:
for(int j=1;j<=15;j++) {
if(clas2.equalsIgnoreCase(driver.findElement(By.xpath(".//*[#id='tabZ"+i+"']/div/div[1]/div["+j+"]")).getAttribute("class"))) {
String ls = driver.findElement(By.xpath(".//*[#id='tabZ"+i+"']/div/div[1]/div["+j+"]")).getAttribute("id");
System.out.println(ls);
driver.findElement(By.xpath(".//*[#id='"+ls+"']/div[1]/div[2]/canvas[2]")).click();
Thread.sleep(3000);
break tabloop;
}
}
}
} catch (NoSuchElementException e) {
present = false;
continue;
}
}
Try this code as you are using driver.findElement() multiple times.Try to avoid finding element instead store them in a variable.
List<WebElement> listoftab = driver.findElements(By
.xpath(".//*[contains (#id, 'tabZ')]/div/div[1]"));
Thread.sleep(1000);
String clas1 = "tablist";
String clas2 = "tabView";
for (int i = 1; i <= 110; i++) {
boolean present;
try {
WebElement element=driver.findElement(By.xpath(".//*[#id='tabZ" + i
+ "']/div/div[1]"));
present = true;
if (clas1.equalsIgnoreCase(element.getAttribute("class"))) {
tabloop: for (int j = 1; j <= 15; j++) {
WebElement element1=driver.findElement(
By.xpath(".//*[#id='tabZ" + i
+ "']/div/div[1]/div[" + j + "]"));
if (clas2.equalsIgnoreCase(element1
.getAttribute("class"))) {
String ls = element1.getAttribute("id");
System.out.println(ls);
driver.findElement(
By.xpath(".//*[#id='" + ls
+ "']/div[1]/div[2]/canvas[2]"))
.click();
break tabloop;
}
}
}
} catch (NoSuchElementException e) {
present = false;
continue;
}
}
Try to avoid hard wait also.Better go with fluentwait.

StaleElementReferenceException: Element not found in the cache - perhaps the page has changed since it was looked up Command duration or timeout

i got this exception.please resolve it.org.openqa.selenium.StaleElementReferenceException: Element not found in the cache - perhaps the page has changed since it was looked up
Command duration or timeout: 5.10 seconds
CODE:
List<WebElement> select_year = driver.findElements(By.xpath("//ul[#class='uib-datepicker-popup dropdown-menu ng-scope']/li/div/table/tbody/tr/td/button/span"));
for(WebElement ele: select_year)
{
String fyear=ele.getText();
if((syear).equals(fyear))
{
System.out.println(syear);
System.out.println(fyear);
ele.click();
List<WebElement>select_month=driver.findElements(By.xpath("//ul[#class='uib-datepicker-popup dropdown-menu ng-scope']/li/div/table/tbody/tr/td/button/span"));
for(WebElement ele2:select_month)
{
String fmonth=ele2.getText();
if((smonth).equals(fmonth))
{
ele2.click();
List<WebElement>select_day=driver.findElements(By.xpath("//ul[#class='uib-datepicker-popup dropdown-menu ng-scope']/li/div/table/tbody/tr/td/button/span"));
for(WebElement ele3:select_day)
{
String fday=ele3.getText();
Thread.sleep(3000);
if((sday).equals(fday))
{
ele3.click();
}
}
}
}
}
}
No need to looping here. You need to find just Single element with text and select as below :-
//To select year
driver.findElement(By.xpath("//ul[#class='uib-datepicker-pop‌​up dropdown-menu ng-scope']/li/div/table/tbody/tr/td/button/span[text() = " + syear+"]")).click();
//To select month
driver.findElement(By.xpath("//ul[#class='uib-datepicker-pop‌​up dropdown-menu ng-scope']/li/div/table/tbody/tr/td/button/span[text() = " + smonth+"]")).click();
//To select day
driver.findElement(By.xpath("//ul[#class='uib-datepicker-pop‌​up dropdown-menu ng-scope']/li/div/table/tbody/tr/td/button/span[text() = " + sday+"]")).click();
Edited :- If you want loop here, you just need to break the loop when if condition becomes true as below :-
//To select year
List<WebElement> select_year = driver.findElements(By.xpath("//ul[#class='uib-datepicker-popup dropdown-menu ng-scope']/li/div/table/tbody/tr/td/button/span"));
for(WebElement ele: select_year)
{
String fyear=ele.getText();
if((syear).equals(fyear))
{
ele.click();
break;
}
}
//To select month
List<WebElement> select_month = driver.findElements(By.xpath("//ul[#class='uib-datepicker-popup dropdown-menu ng-scope']/li/div/table/tbody/tr/td/button/span"));
for(WebElement ele: select_year)
{
String fmonth = ele.getText();
if((smonth).equals(fmonth))
{
ele.click();
break;
}
}
//To select day
List<WebElement> select_day = driver.findElements(By.xpath("//ul[#class='uib-datepicker-popup dropdown-menu ng-scope']/li/div/table/tbody/tr/td/button/span"));
for(WebElement ele: select_year)
{
String fday = ele.getText();
if((sday).equals(fday))
{
ele.click();
break;
}
}

After selecting the second option from dropdown, the WebElement is still showing the record of first option

There is a dropdown list where each selection has a different URL under the dropdown buttons. Suppose when I select first option then it shows 10 hyperlink and select the second option it shows 5 hyperlinks, etc.
Problem - When I select the second option, it is still showing 10 hyperlinks instead of 5 and shows
org.openqa.selenium.StaleElementReferenceException: Element not found
in the cache - perhaps the page has changed since it was looked up
Select select = new Select(selectdropdown);
List<WebElement> options = select.getOptions();
int isize = options.size();
for (int i = 0; i < isize; i++)
{
String value = select.getOptions().get(i).getText();
driver.manage().timeouts().implicitlyWait(100, TimeUnit.SECONDS);
WebElement WebElementer = driver.findElement(By.xpath("//*[#id='content-inner']"));
List<WebElement> elementList = new ArrayList<>();
elementList = WebElementer.findElements(By.cssSelector("a[href]"));
System.out.println("Total number of links found" + elementList.size());
System.out.println("to check wheather link is working or not");
for (WebElement element : elementList)
{
try
{
System.out.println("URL: " + element.getAttribute("href").trim() + " returned "
+ islinkBroken(new URL(element.getAttribute("href").trim())));
}
catch (Exception exp)
{
System.out.println("At " + element.getAttribute("innerHTML")
+ " Exception occured -> " + exp.getMessage());
}
}
}
where you selecting the element ?? (C# syntax example)
IList<IWebElement> accountsDDL = driver.FindElements(By.XPath("//select[#id='yourSelectId']/option"));
for (int i = 1; i < accountsDDL.Count; i++)
{
new SelectElement(driver.FindElement(By.Name("yourSelectId"))).SelectByText(accountsDDL[i].Text); // Selecting the element
}
In java
I spent a little time cleaning up your code and added a few things. See if this works. As Leon said, I think one of the issues was that you didn't have code that actually changed the selected option.
Select select = new Select(selectdropdown);
for (int i = 0; i < select.getOptions().size(); i++)
{
select.selectByIndex(i); // you were missing this line?
// String value = select.getFirstSelectedOption().getText(); // this variable is never used
// driver.manage().timeouts().implicitlyWait(100, TimeUnit.SECONDS); // this doesn't do what you think it does
// I think this next line should work. I combined the two locators into one.
List<WebElement> elementList = driver.findElements(By.cssSelector("#content-inner a[href]"));
System.out.println("Total number of links found" + elementList.size());
System.out.println("to check wheather link is working or not");
for (WebElement element : elementList)
{
try
{
String href = element.getAttribute("href").trim();
System.out.println("URL: " + href + " returned " + islinkBroken(new URL(href)));
}
catch (Exception exp)
{
System.out.println("At " + element.getAttribute("innerHTML") + " Exception occured -> " + exp.getMessage());
}
}
}
Suggestion: It might be useful to you to add the selected option text to your exception message.

Categories

Resources