Cannot open the URLs on the page using Selenium webdriver and Java - selenium

I have written a code using Selenium webdriver and Java to open the URL listed in one particular div on the webpage.
The code opens the first URL goes back to the webpage and the throws the error "Element not found in the cache" - perhaps the page has changed since it was looked up.
Please find the code for reference.
import java.util.*;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class OpenLinks {
public static void OpenLinkAs(String username, String password) throws Exception
{
WebDriver driver;
driver = new FirefoxDriver();
driver.get("http://stg.n**w.le****ine.com/");
driver.findElement(By.name("username")).sendKeys(username);
driver.findElement(By.name("password")).sendKeys(password);
driver.findElement(By.xpath("/html/body/div/div/div[2]/div/form/p[3]/input")).submit();
String content = driver.getPageSource();
if (content.contains("Create new Project"))
{
List<WebElement> elementsList = driver.findElements(By.xpath("//div[#class='recent-project block-link']"));
int RowCount = elementsList.size();
System.out.println(RowCount);
System.out.println(elementsList);
for (int i = 0; i < RowCount; i++)
{
//String oldTab = driver.getWindowHandle();
WebElement url = elementsList.get(i);
//String text= url.getText();
System.out.println(url);
// System.out.println(text);
url.click();
Thread.sleep(5*1000);
driver.navigate().back();
//driver.switchTo().window(oldTab);
}
}
}
public static void main(String[] args) throws Exception
{
OpenLinkAs("username", "password");
}
}

If the element is not found on the page its likely to have been dropped in the DOM. Declare t again and attempt to click it.
What is being returned in the stacktrace, i.e. at what point in the code is the error thrown at?

Related

Hiii team, why I'm getting this ClassCastException below mentioned exception?

// this is the exception I am getting
Exception in thread "main" java.lang.ClassCastException: class sun.net.www.protocol.mailto.MailToURLConnection cannot be cast to class java.net.HttpURLConnection (sun.net.www.protocol.mailto.MailToURLConnection and java.net.HttpURLConnection are in module java.base of loader 'bootstrap')
// this is what I tried
package brokenLinks;
import java.io.IOException;
import java.net.HttpURLConnection;
//import java.net.MalformedURLException;
import java.net.URL;
import java.time.Duration;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import io.github.bonigarcia.wdm.WebDriverManager;
public class ValidateBrokenLinksTest {
public static void main(String[] args) throws IOException {
//set up the webdriver driver
WebDriverManager.chromedriver().setup();
//launch chrome driver
WebDriver driver = new ChromeDriver();
//maximize the window
driver.manage().window().maximize();
//load the url
driver.get("https://testerscafe.in/");
//implicit wait for page to load
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(30));
//get all the links in the webpage using tagName called "a" and store them inside list collection
List<WebElement> elements = driver.findElements(By.tagName("a"));
//get the total number of links available
int size = elements.size();
System.out.println("Number of links available in the webpage :"+size);
//iterate from each link and get the attribute value of each link
for(WebElement links : elements) {
String link = links.getAttribute("href");
//load all the links to URL class
URL url = new URL(link);
//set up the connection
HttpURLConnection connection = (HttpURLConnection)(url.openConnection());
connection.connect();
//validatation
if(connection.getResponseCode()>=400) {
System.out.println(link+" ==> is a broken link");
}
else {
System.out.println(link+" ==> is a valid link");
}
}
//close the webdriver
driver.quit();
}
}
Some of the links on that page are email addresses (info#testerscafe.in). For email links url.openConnection() helpfully returns a MailToURLConnection instead of an HttpURLConnection.
You can exclude the email links and get just the http links with xpath.
Instead of
List<WebElement> elements = driver.findElements(By.tagName("a"));
use
List<WebElement> elements = driver.findElements(By.xpath("//a[starts-with(#href,'http')]"));
See this post regarding how the starts-with xpath works.

Selenium Ebay Script

I have logged into Ebay and want to click the hyperlink of 'My collections' which is under "G'day [username]". The issue is now I can not find the element of 'My collections'. The error message is "Unable to locate element: (//li[#id='gh-ucol']/a)
Please refer to the below steps how I replicate:
Open Ebay via Firefox
Click Log in hyperlink
Enter the user name password then click log in button
Click "G'day [username]"
Select 'My Collection' from the droplist
This is my Selenium Java Script:
import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class KevinTest {
public static void main(String[] args) throws InterruptedException{
WebDriver driver = new FirefoxDriver();
driver.get("http://www.ebay.com.au");
driver.manage().window().maximize();
Thread.sleep(20);
driver.findElement(By.linkText("Sign in")).click();
Thread.sleep(100);
driver.findElement(By.xpath("(//input[#placeholder='Email or username'])[2]")).sendKeys("my#username");
driver.findElement(By.xpath("(//input[#placeholder='Password'])[1]")).sendKeys("mypassword");
driver.findElement(By.id("sgnBt")).click();
boolean tf;
try {
driver.findElement(By.id("errf")).getText();
tf = true;
}catch(NoSuchElementException e) {
tf = false;
}
if (tf == true) {
System.out.println("Incorrect Password");
driver.close();
}else {
System.out.println("Log in successfully");
}
driver.findElement(By.id("gh-eb-u")).click();
Thread.sleep(100);
driver.findElement(By.xpath("(//*[#id='gh-ucol']/a)")).click();
}}
Its a good practice to use implicit wait after initiating the driver.
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
Use the below id for the 'Click "G'day [username]'.
driver.findElement(By.id("gh-ug")).click();
Use the below xpath for collection.
driver.findElement(By.id("//*[#id="gh-ucol"]/a")).click();
Since we have used implicit wait you can remove all other thread.sleeps. Hope this helps. Thanks.

Why the driver.getTitle does not matches with the expected result in Selenium?

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
public class LoginAndSearch {
public static void main(String[] args) {
System.setProperty("webdriver.gecko.driver","C:\\Users\\puren\\Downloads\\Compressed\\geckodriver-v0.11.1-win64\\geckodriver.exe");
DesiredCapabilities capabilities=DesiredCapabilities.firefox();
capabilities.setCapability("marionette", true);
WebDriver driver = new FirefoxDriver(capabilities);
driver.get("http://www.linkedin.com");
String a = driver.getTitle();
if (a=="LinkedIn: Log In or Sign Up")
System.out.print("Pass");
else
System.out.println("Fail");
driver.close();
}
}
In this code, the "If condition" does not match with the expected result.
You are comparing for references of String objects. To compare string values change your code snippet as follows:
String a = driver.getTitle();
if (a.equals("LinkedIn: Log In or Sign Up"))
System.out.print("Pass");
else
System.out.println("Fail");
Follow the link for more:
What is the difference between == vs equals() in Java?

Stale Element reference error on finding the element -> when navigates back to previous page

My WebDriver script is simply finding elements on the page 1(Product display page) and clicking on 1st element to see if its working, then navigates back to Product display page.
It throws Stale Element Reference error and does not click the second element on the page, says element is not attached to the page.
Code is :
public class EcommerceSearchResult {
public static WebDriver driver ;
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "C:\\chromedriver_win32\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("http://store.demoqa.com/");
WebElement searchBox = driver.findElement(By.xpath("//input[contains(#class,'search')]"));
searchBox.sendKeys("iphone"+"\n");
List<WebElement> gridrow
= driver.findElements
(By.xpath(".//*[#id='grid_view_products_page_container']/div/div"));
int count = gridrow.size();
System.out.print(count);
for(int i = 0 ; i<count ; i++)
{
List<WebElement> listingelementinloop = driver.findElements(By.xpath(".//*[#id='grid_view_products_page_container']/div/div"));
System.out.println(gridrow.get(i).getText());
listingelementinloop.get(i).click();
driver.navigate().back();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
}
}
In the loop you need to replace following line:
System.out.println(gridrow.get(i).getText());
With
System.out.println(listingelementinloop.get(i).getText());
As, elements in the gridrow list will become stale once you click on 'Back' button of browser.
Alternatively, following is another way of performing the same task:
public static WebDriver driver ;
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "C:\\chromedriver_win32\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("http://store.demoqa.com/");
WebElement searchBox = driver.findElement(By.xpath("//input[contains(#class,'search')]"));
searchBox.sendKeys("iphone"+"\n");
List<WebElement> gridrow
= driver.findElements
(By.xpath(".//*[#id='grid_view_products_page_container']/div/div"));
int count = gridrow.size();
System.out.print(count);
for(int i = 1 ; i<=count ; i++)
{
WebElement element =
driver.findElement(By.xpath(".//*[#id='grid_view_products_page_container']/div/div[" + i + "]"));
System.out.println(element.getText());
element.click();
driver.navigate().back();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
}
}
Let me know, if you have any further queries.
The problem is that you are looping through a collection of elements that you collected from the page. Once you leave that page or reload it, the element references you were storing become stale, thus the error. One way to get around this is to use an index into the collection and loop over that. I've rewritten your code to make use of functions to take care of repeated actions. I've tested this code and it works.
Part of main
driver.get("http://store.demoqa.com/");
Search("iphone");
for (int i = 0; i < GetProductCount(); i++)
{
ClickProduct(i);
driver.navigate().back();
}
and supporting functions
public void Search(String searchTerm)
{
driver.findElement(By.cssSelector("input[value='Search Products']")).sendKeys(searchTerm + "\n");
}
public void ClickProduct(int index)
{
driver.findElements(By.cssSelector("h2 > a")).get(index).click();
}
public int GetProductCount()
{
return driver.findElements(By.cssSelector("h2 > a")).size();
}
Below is the solution code for your problem.Tested it in my machine and attached console screenshot.
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class EcommerceSearchResult {
public static WebDriver driver ;
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "C:\\chromedriver_win32\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("http://store.demoqa.com/");
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement searchBox = driver.findElement(By.xpath("//input[contains(#class,'search')]"));
searchBox.sendKeys("iphone"+"\n");
List<WebElement> gridrow = driver.findElements(By.xpath(".//*[#id='grid_view_products_page_container']/div/div"));
int count = gridrow.size();
System.out.print(count+"\n");
for(int i = 0 ; i<count ; i++)
{
WebElement listingelementinloop = driver.findElement(By.xpath(".//*[#id='grid_view_products_page_container']/div/div["+(i+1)+"]"));
System.out.println(listingelementinloop.getText());
listingelementinloop.findElement(By.xpath(".//div/a")).click();
wait.until(ExpectedConditions.urlContains("products-page/product-category"));
driver.navigate().back();
wait.until(ExpectedConditions.titleIs("iphone | Search Results | ONLINE STORE"));
}
}
}

Cannot Print names of all result links on first 5 pages of Google.com in Selenium

I am trying to print names of all result links on the first 5 pages on Google.com
Scenario is,
1) Go to www.google.com and search for something.
2) Print names of all result links on first 5 pages
I am able to print all the result links of first page and click next page link. But second page links are not printing. I think the reason may be with the xpath changing in second page. If so, how do I print all the links of 2nd 3rd 4th and 5th page.
Kindly help me in solving this solution.
package com;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.firefox.internal.ProfilesIni;
public class Exercise2 {
static WebDriver d;
public static void main(String[] args) throws InterruptedException {
ProfilesIni prop = new ProfilesIni();
FirefoxProfile seleniumProfile = prop.getProfile(("Selenium"));
d = new FirefoxDriver();
d.manage().window().maximize();
d.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
d.get("http://www.google.com");
Thread.sleep(5000);
d.findElement(By.name("q")).sendKeys("Selenium");
d.findElement(By.name("q")).sendKeys(Keys.ENTER);
Thread.sleep(5000);
//Print names of all result links on first page.
String part1 = "//div[#id='rso']/div[2]/div[";
String part2 = "]/div/h3/a";
for(int i=2;i<=5;i++){
String part3 = "//div[#id='navcnt']/table/tbody/tr/td[";
String part4 = "]";
int a = 1;
while(isElementPresent(part1+a+part2)){
String text = d.findElement(By.xpath(part1+a+part2)).getText();
System.out.println(text);
a++;
}
Thread.sleep(5000);
System.out.println("*************Next Page**********");
Thread.sleep(5000);
d.findElement(By.xpath(part3+i+part4)).click();
}
}
public static boolean isElementPresent(String xpathexp){
List<WebElement> allList = d.findElements(By.xpath(xpathexp));
//WebElement nextPages = d.findElement(By.xpath(nextPage1));
if(allList.size()==0)
{
return false;
}else{
return true;
}
}
}
List<WebElements> links = driver.findElements(By.tagName("a"));
for(int i = 0 ; i < 5 ; i ++)
{
string firstLink = links[i].getAttribute("href");
}
try following code.
package javaselenium;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
/**
*
* #author Muhammad USman
*/
public class JavaSelenium {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
System.out.println("Hello Wrold");
GetUrls("Selenium");
}
public static ArrayList GetUrls(String keyWord)
{
FirefoxDriver driver;
driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
driver.get("https://www.google.com/?gws_rd=ssl");
WebElement qElement;
qElement = driver.findElement(By.name("q"));
qElement.sendKeys(keyWord);
qElement.sendKeys(Keys.ENTER);
try {
Thread.sleep(3*1000);
} catch (InterruptedException ex) {
Logger.getLogger(JavaSelenium.class.getName()).log(Level.SEVERE, null, ex);
}
//give number how mange pages you want to crawl
int nPageToCrawl=5;
List links;
links=new ArrayList();
for (int i = 0; i < nPageToCrawl; i++) {
if(i>0)
{
System.out.println("***Click on Next Page***");
//Click on Next Page
driver.findElement(By.id("pnnext")).click();
try {
Thread.sleep(3*1000);
} catch (InterruptedException ex) {
Logger.getLogger(JavaSelenium.class.getName()).log(Level.SEVERE, null, ex);
}
}
//it will get all links of pageLinks
List<WebElement> pageLinks = driver.findElements(By.cssSelector(".r>a"));
for (WebElement pageLink : pageLinks) {
links.add(pageLink.getAttribute("href"));
System.out.println(pageLink.getText());
System.out.println(pageLink.getAttribute("href"));
}
}
//close browser
driver.quit();
return (ArrayList) links;
}
}
if any issue then let me know.