When running the code appears that the XPath for the Aprobă button isn't recognized and doesn't click on it even if when I inspect the element on the site the XPath seems to find the correct button.
The error is:
org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element {"method":"xpath","selector":"//a[contains(.,'')]/ancestor::div[contains(#class, 'js-answer-element')]//span[#class='sg-icon-as-button__hole']"}
My method approveAnswerByUsername(String username) should check if the answer has been approved or not. An approved answer is recognized by the 3 buttons near the username and the green circle under it.
The user can be the first one that answers, the second one or the only one. That's why I used "ancestor" in the XPath.
In some cases, the circles have been seen(even in the precedent case it was) and in others not. The same with the button "Aprobă".
The code below should do the following:
1. connect to the www.brainly.ro and login with the credentials
2. open the window of the user Lola1511
3. open all the answered questions and check if it has been approved or not. If isn't approved, then approve it.
Below I post my entire code (the link to the site and the credentials are all there)
import java.util.ArrayList;
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.chrome.ChromeDriver;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class classTest {
WebDriver driver;
#BeforeTest
public void invokeBrowser() {
try {
System.setProperty("webdriver.chrome.driver",
"C:\\Users\\sanduc\\Desktop\\Selenium\\Kits\\chromedriver_win32\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().deleteAllCookies();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.manage().timeouts().pageLoadTimeout(40, TimeUnit.SECONDS);
driver.get("");
} catch (Exception e) {
e.printStackTrace();
}
}
#Test(priority = 1)
public void log_in() {
try {
driver.findElement(
By.xpath("//div[#class='sg-content-box__actions']/nav[#class='brn-hero__navigation']/a[1]"))
.click();
driver.findElement(By.xpath("//form[#action='/login?entry=2&return=/']/div[2]/input"))
.sendKeys("my_emal#mail.com");
driver.findElement(By.xpath("//form[#action='/login?entry=2&return=/']/div[3]/input"))
.sendKeys("my_password");
driver.findElement(By.xpath("//button[#type='submit']")).click();
Thread.sleep(4000);
driver.get("");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
#Test(priority = 2)
public void click_pages() {
try {
int index_while = 1;
while (true) {
driver.get("" + index_while);
List<WebElement> demovar = driver.findElements(By.xpath("//div[#class='task-content']/a"));
System.out.println(demovar.size());
System.out.println("+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+");
System.out.println("Current page: " + driver.getCurrentUrl());
System.out.println("+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+");
ArrayList<String> hrefs = new ArrayList<String>(); // List for
// storing
// all href
// values
// for 'a'
// tag
for (WebElement var : demovar) {
System.out.println(var.getText()); // used to get text
// present between the
// anchor tags
System.out.println(var.getAttribute("href"));
hrefs.add(var.getAttribute("href"));
System.out.println("*************************************");
}
// Navigating to each link
int i = 0;
for (String href : hrefs) {
driver.navigate().to(href);
System.out.println((++i) + ": navigated to URL with href: " + href);
approveAnswerByUsername("Lola1511");
Thread.sleep(3000); // To check if the navigation is
// happening properly.
System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
}
index_while++;
if (demovar.size() < 5) {
break;
}
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void approveAnswerByUsername(String username) {
try {
if (driver.findElement(By.xpath("//a[contains(.,'" + username
+ "')]/ancestor::div[contains(#class, 'js-answer-element')]//span[#class='sg-icon-as-button__hole']")).isDisplayed()) {
System.out.println("The homework " + driver.getCurrentUrl() + " has ALREADY been approved");
} else {
driver.findElement(By.xpath("//a[contains(.,'" + username
+ "')]/ancestor::div[contains(#class, 'js-answer-element')]//div[contains(#class,'js-approve-button-text')][contains(.,'Aprobă')]")).click();
Thread.sleep(2000);
driver.navigate().refresh();
System.out.println("The homework " + driver.getCurrentUrl() + " has been approved NOW");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
I couldn't check the code due to time limitations but I would suggest a different approach for using selectors.
Use ID selectors or name selectors in the webpage instead of xpaths.
ID selectors are the best and fastest way for selecting elements. Xpaths are slower and sometimes unreliable.
My advice would be to try ID and name selectors and use xpath only if both are not found.
Related
i would like to build a crawler in Java that give me all cookies from a website. This crawler is believed to crawl a list of websites (and obviously the undersides) automatic.
I have used jSoup and Selenium for my plan.
package com.mycompany.app;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.openqa.selenium.Cookie;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.*;
public class BasicWebCrawler {
private static Set<String> uniqueURL = new HashSet<String>();
private static List<String> link_list = new ArrayList<String>();
private static Set<String> uniqueCookies = new HashSet<String>();
private static void get_links(String url) {
Connection connection = null;
Connection.Response response = null;
String this_link = null;
try {
connection = Jsoup.connect(url);
response = connection.execute();
//cookies_http = response.cookies();
// fetch the document over HTTP
Document doc = response.parse();
// get all links in page
Elements links = doc.select("a[href]");
if(links.isEmpty()) {
return;
}
for (Element link : links) {
this_link = link.attr("href");
boolean add = uniqueURL.add(this_link);
System.out.println("\n" + this_link + "\n" + "title: " + doc.title());
if (add && (this_link.contains(url))) {
System.out.println("\n" + this_link + "\n" + "title: " + doc.title());
link_list.add(this_link);
get_links(this_link);
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
get_links("https://de.wikipedia.org/wiki/Wikipedia");
/**
* Hier kommt Selenium ins Spiel
*/
WebDriver driver;
System.setProperty("webdriver.chrome.driver", "D:\\crawler\\driver\\chromedriver.exe");
driver = new ChromeDriver();
// create file named Cookies to store Login Information
File file = new File("Cookies.data");
FileWriter fileWrite = null;
BufferedWriter Bwrite = null;
try {
// Delete old file if exists
file.delete();
file.createNewFile();
fileWrite = new FileWriter(file);
Bwrite = new BufferedWriter(fileWrite);
// loop for getting the cookie information
} catch (Exception ex) {
ex.printStackTrace();
}
for(String link : link_list) {
System.out.println("Open Link: " + link);
driver.get(link);
try {
// loop for getting the cookie information
for (Cookie ck : driver.manage().getCookies()) {
String tmp = (ck.getName() + ";" + ck.getValue() + ";" + ck.getDomain() + ";" + ck.getPath() + ";" + ck.getExpiry() + ";" + ck.isSecure());
if(uniqueCookies.add(tmp)) {
Bwrite.write("Link: " + link + "\n" + (ck.getName() + ";" + ck.getValue() + ";" + ck.getDomain() + ";" + ck.getPath() + ";" + ck.getExpiry() + ";" + ck.isSecure())+ "\n\n");
Bwrite.newLine();
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
try {
Bwrite.close();
fileWrite.close();
driver.close();
driver.quit();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
I test this code on a wikipedia page and compare the result with a cookie scanner call CookieMetrix.
My code shows only four cookies:
Link: https://de.wikipedia.org/wiki/Wikipedia:Lizenzbestimmungen_Commons_Attribution-ShareAlike_3.0_Unported
GeoIP;DE:NW:M__nster:51.95:7.54:v4;.wikipedia.org;/;null;true
Link: https://de.wikipedia.org/wiki/Wikipedia:Lizenzbestimmungen_Commons_Attribution-ShareAlike_3.0_Unported
WMF-Last-Access-Global;13-May-2019;.wikipedia.org;/;Mon Jan 19 02:28:33 CET 1970;true
Link: https://de.wikipedia.org/wiki/Wikipedia:Lizenzbestimmungen_Commons_Attribution-ShareAlike_3.0_Unported
WMF-Last-Access;13-May-2019;de.wikipedia.org;/;Mon Jan 19 02:28:33 CET 1970;true
Link: https://de.wikipedia.org/wiki/Wikipedia:Lizenzbestimmungen_Commons_Attribution-ShareAlike_3.0_Unported
mwPhp7Seed;55e;de.wikipedia.org;/;Mon Jan 19 03:09:08 CET 1970;false
But the cookie scanner shows seven. I don't know why my code shows lesser than the CookieMetrix. Can you help me?
JavaDoc for java.util.Set<Cookie> getCookies():
Get all the cookies for the current domain. This is the equivalent of calling "document.cookie" and parsing the result
document.cookie will not return HttpOnly cookies, simply because JavaScript does not allow it.
Also notice that the “CookieMetrix” seems to list cookies from different domains.
Solutions:
To get a listing such as “CookieMetrix” (1+2) you could add a proxy after your browser and sniff the requests.
In case you want to get all cookies for the current domain, including HttpOnly (1), you could try accessing Chrome’s DevTools API directly (afair, it’ll also return HttpOnly cookies)
I want to compare three strings getting from three different pages.I am
using below code but unable to proceed. Please check code in the end.Can any
one help me please.
Main problem i am not getting idea how to compare three string from
different pages.
When i have written compare code in if block, getting error "The left-hand
side of an assignment must be a variable"
import java.util.Iterator;
import java.util.List;
import java.util.Set;
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.Select;
public class test {
static String Price;
static String InnerPrice;
static String CartPrice;
public static void main (String args[])
{
System.setProperty("webdriver.chrome.driver",
"./Drivers/chromedriver.exe");
WebDriver driver=new ChromeDriver();
driver.get("https://www.amazon.in/");
driver.manage().window().maximize();
driver.findElement(By.xpath("//input[#id='twotabsearchtextbox']")).
sendKeys("Iphone 7");
driver.findElement(By.xpath("//input[#class='nav-input' and
#value='Go']")).click();
//driver.findElement(By.xpath("//span[#class='a-expander-prompt' and
text()='See more']")).click();
//driver.findElement(By.xpath("//span[#class='a-size-small a-color-base'
and text()='Smartphones & Basic Mobiles']")).click();
try {
Thread.sleep(5000);
} catch (Exception e1) {
System.out.println(e1.getMessage());
}
driver.findElement(By.xpath("//span[contains(text(),'Smartphones')]")).
click();
Select select=new
Select(driver.findElement(By.xpath("//select[#id='sort']")));
select.selectByVisibleText("Price: Low to High");
try {
Thread.sleep(5000);
} catch (Exception e) {
System.out.println("Exception is"+e.getMessage());
}
List<WebElement> myElements =
driver.findElements(By.xpath("//span[#class='a-size-base a-color-price s-
price a-text-bold']"));
//System.out.println("Size of List: "+myElements.size());
int size=myElements.size();
for (int i=0;i<size;i++)
{
Price=myElements.get(i).getAttribute("innerText");
System.out.println("Price of Search Result Page is " +Price);
break;
}
myElements.get(0).click();
String parent=driver.getWindowHandle();
// This will return the number of windows opened by Webdriver and will
return Set of St//rings
Set<String>s1=driver.getWindowHandles();
// Now we will iterate using Iterator
Iterator<String> I1= s1.iterator();
while(I1.hasNext())
{
String child_window=I1.next();
// Here we will compare if parent window is not equal to child window then
we will close
if(!parent.equals(child_window))
{
driver.switchTo().window(child_window);
//System.out.println(driver.switchTo().window(child_window).getTitle());
String
InnerPrice=driver.findElement(By.xpath("//span[#id='priceblock_ourprice']"))
.getText();
System.out.println("Price of Product Details Page is
"+InnerPrice.substring(0,InnerPrice.length()-3));
//To Add product in cart
driver.findElement(By.xpath("//input[#id='add-to-cart-button']")).click();
try {
Thread.sleep(5000);
} catch (Exception e) {
System.out.println(e.getMessage());
}
String CartPrice=driver.findElement(By.xpath("//div[#id='huc-v2-order-row-
inner']//div[#class='a-row a-spacing-micro']//span[starts-with(#style,'text-
decoration:')]")).getText();
System.out.println("Cart Price is "+CartPrice.substring(0,
CartPrice.length()-3));
driver.close();
}
// once all pop up closed now switch to parent window
driver.switchTo().window(parent);
}
If (Price.equalsIgnoreCase(InnerPrice))&&
(InnerPrice.equalsIgnoreCase(CartPrice))
{
System.out.println("Equal");
}
}
}
In the if case, you have
If (Price.equalsIgnoreCase(InnerPrice))&& (InnerPrice.equalsIgnoreCase(CartPrice))
You are closing the if statement too early. No need to put the equalsIgnoreCase method call within braces. Try with
If (Price.equalsIgnoreCase(InnerPrice)&& InnerPrice.equalsIgnoreCase(CartPrice))
package testPackage;
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.chrome.ChromeDriver;
import org.testng.annotations.*;
public class AllLinkVerificationInAPage {
WebDriver driver;
#BeforeTest
public void OpenApp()
{
System.setProperty("webdriver.chrome.driver", "E:/Selenium/Webdriver /Softwares/chromedriver.exe");
driver = new ChromeDriver();
driver.navigate().to("http://ndtv.com/");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
WebElement popUp = driver.findElement(By.xpath("//*[#id='__cricketsubscribe']/div[2]/div[2]/a[1]"));
popUp.click();
}
#Test
public void clickLinks() throws InterruptedException
{
//extract the list of WenElements and its count
List<WebElement> linkElements = driver.findElements(By.tagName("a"));
int count = linkElements.size();
System.out.println("Total number of links = " + count );
//test each link
for(WebElement currentElement : linkElements)
{
String link = currentElement.getText();
System.out.println(link);
if(link !="")
{
currentElement.click();
System.out.println("Working Fine");
}
driver.navigate().back();
Thread.sleep(3000);
}
}
}
When I run this code I get following error:-
org.openqa.selenium.StaleElementReferenceException: stale element
reference: element is not attached to the page document
I tried with implicit wait as well but getting same issue.
Each time the DOM is changed or refreshed, like in going to different page, the driver loses the elements it previously located. You need to relocate the list each iteration
int count = driver.findElements(By.tagName("a")).size();
for (int i = 0 ; i < count ; ++i) {
List<WebElement> linkElements = driver.findElements(By.tagName("a"));
WebElement currentElement = linkElements.get(i);
String link = currentElement.getText();
System.out.println(link);
if(link != "")
{
currentElement.click();
System.out.println("Working Fine");
}
driver.navigate().back();
Thread.sleep(3000);
}
i am getting somthing like this
Hi i am scraping a web page using Selenium Webdriver an i am able to achieve my data but problem is that this directly interact with browser and i dont want to open a web browser and want to scrape all data as it is
How can i achieve my goal
Here is my code
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
public class GetData {
public static void main(String args[]) throws InterruptedException {
String sDate = "27/03/2014";
WebDriver driver = new FirefoxDriver();
String url="http://www.upmandiparishad.in/commodityWiseAll.aspx";
driver.get(url);
Thread.sleep(5000);
// select barge
new Select(driver.findElement(By.id("ctl00_ContentPlaceHolder1_ddl_commodity"))).selectByVisibleText("Jo");
driver.findElement(By.id("ctl00_ContentPlaceHolder1_txt_rate")).sendKeys(sDate);
// click buttonctl00_ContentPlaceHolder1_txt_rate
Thread.sleep(3000);
driver.findElement(By.id("ctl00_ContentPlaceHolder1_btn_show")).click();
Thread.sleep(5000);
//get only table tex
WebElement findElement = driver.findElement(By.id("ctl00_ContentPlaceHolder1_GridView1"));
String htmlTableText = findElement.getText();
// do whatever you want now, This is raw table values.
System.out.println(htmlTableText);
driver.close();
driver.quit();
}
}
My updated New code
import com.gargoylesoftware.htmlunit.BrowserVersion;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import org.openqa.selenium.support.ui.Select;
public class Getdata1 {
public static void main(String args[]) throws InterruptedException {
WebDriver driver = new HtmlUnitDriver(BrowserVersion.FIREFOX_3_6);
driver.get("http://www.upmandiparishad.in/commodityWiseAll.aspx");
System.out.println(driver.getPageSource());
Thread.sleep(5000);
// select barge
new Select(driver.findElement(By.id("ctl00_ContentPlaceHolder1_ddl_commodity"))).selectByVisibleText("Jo");
String sDate = "12/04/2014"; //What date you want
driver.findElement(By.id("ctl00_ContentPlaceHolder1_txt_rate")).sendKeys(sDate);
driver.findElement(By.id("ctl00_ContentPlaceHolder1_btn_show")).click();
Thread.sleep(3000);
//get only table tex
WebElement findElement = driver.findElement(By.id("ctl00_ContentPlaceHolder1_GridView1"));
String htmlTableText = findElement.getText();
// do whatever you want now, This is raw table values.
System.out.println(htmlTableText);
driver.close();
driver.quit();
}
}
Thanks in advance
Use HtmlUnit or HtmlUnitDriver by Selenium
WebDriver driver = new HtmlUnitDriver(BrowserVersion.FIREFOX_17);
driver.get("http://www.upmandiparishad.in/commodityWiseAll.aspx");
System.out.println(driver.getPageSource());
Thread.sleep(5000);
// select barge
new Select(driver.findElement(By.id("ctl00_ContentPlaceHolder1_ddl_commodity"))).selectByVisibleText("Jo");
String sDate = "12/04/2014"; //What date you want
driver.findElement(By.id("ctl00_ContentPlaceHolder1_txt_rate")).sendKeys(sDate);
driver.findElement(By.id("ctl00_ContentPlaceHolder1_btn_show")).click();
Thread.sleep(3000);
//get only table tex
WebElement findElement = driver.findElement(By.id("ctl00_ContentPlaceHolder1_GridView1"));
String htmlTableText = findElement.getText();
// do whatever you want now, This is raw table values.
System.out.println(htmlTableText);
driver.close();
driver.quit();
To get tabular output, you can try something like this..
String arrCells[] = htmlTableText.split(" ");
Boolean bIsANumber = false;
for(int i = 0; i < arrCells.length; i++) {
try {
int tmp = Integer.parseInt(arrCells[i]);
bIsANumber = true;
}
catch(Exception ex) {
bIsANumber = false;
}
if(bIsANumber) {
System.out.print("\n"+arrCells[i]+"\t");
}
else {
System.out.print(arrCells[i]+"\t");
}
}
My Problem in brief :
Through My selenium web driver right now i am testing list of elements in a list box , While using the following code i am getting the error
Testing Configuration :
Mozilla firefox
Eclipse Indigo
Selenium Webdriver 2
Scenario which i tested :
Open website
Select and display the list box items
Write to console
My error :
java.lang.Error: Unresolved compilation problems: The type List is not generic; it cannot be parameterized with arguments Iterator cannot be resolved to a type
My Code :
package com.example.tests;
import java.util.Iterator;
import java.util.regex.Pattern;
import java.util.concurrent.TimeUnit;
import org.junit.*;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;
import org.openqa.selenium.WebElement;
import org.openqa.jetty.html.List;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
import com.browsersetup.test.BrowserSetup;
import org.testng.*;
import org.testng.annotations.*;
import org.testng.annotations.Test;
#SuppressWarnings("unused")
public class sample2 extends BrowserSetup{
private boolean acceptNextAlert = true;
private StringBuffer verificationErrors = new StringBuffer();
#BeforeClass
public void setUp() throws Exception {
driver = new OperaDriver();
baseUrl = "http://www.ebay.com/";
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
System.out.println("Browser = " + driver);
System.out.println("Base URL = " + baseUrl);
}
#Test
public void testUntitled() throws Exception {
driver.get(baseUrl + "/");
Thread.sleep(10000);
WebElement element = driver.findElement(By.name("_sacat"));
Select dd= new Select(element);
List<WebElement> allOptions= dd.getOptions();
//To go through the list, we can use an Iterator.
//Iterator should be of the same type as the List
//which is WebElement in this case.
Iterator<WebElement> it = allOptions.iterator();
//Using while loop, we can iterate till the List has
//a next WebElement [hasNext() is true]
//number of items in the list
System.out.println(allOptions.size());
while(it.hasNext()){
//When you say it.next(), it points to a particular
//WebElement in the List.
WebElement el = it.next();
//Check for the required element by Text and click it
if(el.getText().equals("mango")){
System.out.println(el.getAttribute("value"));
el.click();
}
}
#AfterClass
public void tearDown() throws Exception {
driver.quit();
String verificationErrorString = verificationErrors.toString();
if (!"".equals(verificationErrorString)) {
fail(verificationErrorString);
}
}
private boolean isElementPresent(By by) {
try {
driver.findElement(by);
return true;
} catch (NoSuchElementException e) {
return false;
}
}
private boolean isAlertPresent() {
try {
driver.switchTo().alert();
return true;
} catch (NoAlertPresentException e) {
return false;
}
}
private String closeAlertAndGetItsText() {
try {
Alert alert = driver.switchTo().alert();
String alertText = alert.getText();
if (acceptNextAlert) {
alert.accept();
} else {
alert.dismiss();
}
return alertText;
} finally {
acceptNextAlert = true;
}
}
}
I dont know where it went wrong,guide me where it went wrong
thanks in advance
See the javadocs of org.openqa.jetty.html.List: http://www.jarvana.com/jarvana/view/org/seleniumhq/selenium/selenium-server/2.0b1/selenium-server-2.0b1-javadoc.jar!/org/openqa/jetty/html/List.html
and the one of the java.util.List: http://docs.oracle.com/javase/7/docs/api/java/util/List.html
The one you used doesn't support generics (as the error says).
The problem in your case seems to be the following import:
import org.openqa.jetty.html.List;
try to replace it with:
import java.util.List;
For more ideas see similar question: The type Collection is not generic; it cannot be parameterized with arguments <? extends E>