Getting NoSuchElementException while trying Datepicker in jquery - java

Tring to click on the input box, with script able to open the URL-https://jqueryui.com/datepicker/.In locator the id is available still getting No element foun.
Tried with thread.sleep as well
when i am running the script getting exception
package SeleniumWebDriver;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class HandlingCalender {
public static void main(String[] args) throws InterruptedException {
WebDriver driver =new ChromeDriver();
driver.get("https://jqueryui.com/datepicker/");
//driver.manage().window().maximize();
Thread.sleep(1000);
driver.findElement(By.id("datepicker")).click();

Element you trying to access is inside an iframe.
So, to access it you need first to switch into that iframe, as following:
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.class("demo-frame")));
wait.until(ExpectedConditions.elementToBeClickable(By.id("datepicker"))).click();

Related

Selenium non-deterministic loop over List<WebElement>

I am trying to do some quick tests to learn selenium for web scraping purposes. I am trying to loop over the menu items of the taco bell website. What I find confusing is that the first element of the List, isn't what is selected by the first or second click. The actual selection is usually the 2nd or 3rd element. It is non-deterministic. What am I doing wrong?
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.List;
public class Main {
static WebDriver driver;
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "/Applications/chromedriver");
driver = new ChromeDriver();
driver.get("https://www.tacobell.com/food");
List<WebElement> listOfMenuCategories = driver.findElements(By.cssSelector(".cls-category-card-item-card"));
for(WebElement webElement : listOfMenuCategories){
scanTacoBellMenuCategory(webElement);
}
System.out.println("1: "+listOfMenuCategories.size());
driver.quit();
}
public static void scanTacoBellMenuCategory(WebElement webElement){
webElement.click();
List<WebElement> listOfSubMenuCategories = driver.findElements(By.cssSelector(".product-item"));
for(WebElement submenuCategory : listOfSubMenuCategories){
scanTacoBellSubMenuCategory(submenuCategory);
}
}
public static void scanTacoBellSubMenuCategory(WebElement webElement){
webElement.click();
}
}
Thanks.
UPDATE-------------------------------
I now realize that my example was unnecessarily complicated and intent was not obvious. Here is a more direct example:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.List;
public class MainTwo {
static WebDriver driver;
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "/Applications/chromedriver");
driver = new ChromeDriver();
driver.get("https://www.tacobell.com/food");
List<WebElement> listOfMenuCategories = driver.findElements(By.cssSelector(".cls-category-card-item-card"));
for(WebElement webElement : listOfMenuCategories){
webElement.click();
break;
}
driver.quit();
}
}
The tacobell menu (https://www.tacobell.com/food) has 16 categories in the following order: New, Favorites, Combos, Specialties, Tacos, Burritos, Quesadillas, Nachos, Value Menu, Sweets, Sides, Drinks, Power Menu, Party, Packs, Vegetarian, Breakfast.
When I loop over these items, I "click" the first one in the forEach loop. I would expect that to be the "New" category. I would also expect the result to be repeatable. However, neither of those statements are true. It usually opens one of the "Favorites", "Combos", or "Specialties" menus. However, it can truly open pretty much anything.
It seems as if the Webdriver is non-blocking in some way. In particular, the webElement.click() event doesn't seem to stop the execution of the forEach loop. It is almost as if the the webElement was in another thread.
Why doesn't the "New" menu get displayed when I run the above code and why isn't this deterministic?

Why does Selenium testing website not work on more than one page?

The first part of the code work will work. But the second part doesn't work, and no error appears, and I don't know where the problem is. So please help.
First part is the login page, and the second part is home page.
package Test;
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.chrome.ChromeDriver;
public class test {
public static void main(String[] args) throws InterruptedException {
WebDriver driver;
System.setProperty("webdriver.gecko.driver",
"E:\\Quality\\drivers\\geckodriver.exe");
driver =new FirefoxDriver();
driver.get("https://www.linkedin.com/uas/login");
// first part//
driver.findElement(By.xpath("//*[#id=\"session_key-login\"]")).click();
driver.findElement(By.xpath("//[#id=\"session_keylogin\"]")).sendKeys("Email");
driver.findElement(By.xpath("//*[#id=\"session_password-login\"]")).click();
driver.findElement(By.xpath("//*[#id=\"session_password-login\"]")).sendKeys("*******");
driver.findElement(By.xpath("//*[#id=\"btn-primary\"]")).click();
// second part//
WebElement test = null ;
test.findElement(By.xpath("/html/body/div[5]/div[5]/aside/div/header")).click();
}
}
You have putted a wrong id for xpath of email textbox.
You should use session_key-login instead of session_keylogin.
Just use the below revised code where you're using sendKeys() method:
driver.findElement(By.xpath("//*[#id=\"session_key-login\"]")).sendKeys("Email");
2nd part Solution
Skip WebElement declaration, thus comment the line //WebElement test = null;
Use the line using driver object
driver.findElement(By.xpath("/html/body/div[5]/div[5]/aside/div/header")).click();
You can also use xpath //*[#id=\"msg-overlay\"]/div/header
thus for the revised code is:
driver.findElement(By.xpath("//*[#id=\"msg-overlay\"]/div/header")).click();

Selenium WebDriver -- XPath

Trying to get the script to select the filter button on the top but can't seem to figure out how to input the XPath. I believe it has something to do with it be in a separate iframe.
package chromebrowser;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class JavaClass {
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "C:\\Newfolder\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://mlvtwrew73.consilio.com/Relativity/");
driver.manage().window().maximize();
//Thread.sleep(5000); this can be used as a wait command before moving on to the next function
WebElement objWE;
Thread.sleep(9000);
// objWE = driver.findElement(By.linkText("User Status"));
// objWE.click();
driver.switchTo().defaultContent();
driver.findElement(By.xpath("id(\"ctl00_ctl00_itemList_FilterSwitch\")")).click();
// objWE = driver.findElement(By.id("1"));
// driver.close(); will be used to close the site once all testing completes
}
}
Use ID locator -- it's more appropriate here (and faster than XPath):
WebDriverWait wait= new WebDriverWait(driver, 5);
wait.until(ExpectedConditions.elementToBeClickable(By.id("ct‌l00_ctl00_itemList_F‌​ilterSwitch")));
driver.findElement(By.id("ctl00_ctl00_itemList_FilterSwitch")).click();
Looks like you're passing in an incorrect XPath. Try this one:
driver.findElement(By.xpath("//a[#id='ctl00_ctl00_itemList_FilterSwitch']")).click();
If there is then why you are using xpath.
Use id like this
driver.findElment(By.id("ctl00_ctl00_itemList_FilterSwitch"). click ();
I need more clarity regarding your question.
As I have understand, I think you need a requirement of XPath of the Filters image. If i am right, try this:
//div[#class='actionCellContainer']//a/img[#class='itemListActionImage']

Selenium cannot find element on website (chrome/Java)

Trying to test/learn selenium to login
the error - Exception in thread "main" org.openqa.selenium.ElementNotVisibleException: element not visible
package com.indeed.tests;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class test1 {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver",
"C:\\Users\\****\\Desktop\\neww\\trainingfiles\\chromedriver.exe.exe");
WebDriver driver = new ChromeDriver();
driver.get("http://www.neopets.com/login/index.phtml");
driver.findElement(By.name("username")).sendKeys("test1");
}
private static void sleep(int i) {
}
}
I had a look at that web page. The problem is that there are two input fields with the name "username". One of them is not visible. Probably Selenium is getting that one. What you should do is:
List<WebElement> elements = driver.findElements(...);
and then get the second one (or the first, whatever), then try:
elements.get(1).sendKeys(...);

Selenium testcase execution speed issues

I have a issue with selenium where i am able to pass the testcase, but the issue is the execution of the testcase is very quick. Is there any way or attribute through which i can control the speed of the execution. I have been facing this problem big time. Any help will be greatly appreciated.
Below is my script for reference.
package test.selenium;
import java.util.concurrent.TimeoutException;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class test{
WebDriver driver;
public TrafficGroupUpdateTestNG() {
}
/**
* #throws java.lang.Exception
*/
#BeforeTest
public void setUp() throws Exception {
// Use Internet Explorer and set driver;
System.setProperty("webdriver.ie.driver",
"D:\\IBM\\Selenium\\IEDriverServer.exe");
driver = new InternetExplorerDriver();
// And now use this to visit URL
driver.get("URL Of JSP");
}
/**
* #throws java.lang.Exception
*/
#AfterTest
public void tearDown() throws Exception {
driver.close();
}
#Test
public final void test() {
final WebElement formElement = driver.findElement(By.id("search"));
final Select drelement = new Select(drelement.findElement(By.id("my_input")));
drelement.selectByIndex(0);
final WebElement submit = driver.findElement(By.id("Submit"));
submit.click();
}
}
It sounds like elements from AJAX calls are not visible or ready when document.ready status is updated. Working with my webdevs, I had them add a waiting class while they loaded things and remove it when they're done.
As a result, I was able to create this "WaitForPageLoad" method that waits until AJAX is finished. It's in C# but simple enough to translate into Java.
I used
Thread.sleep(2000);
A convinient way for making the page wait.
There are many ways you can control speed of the execution..
Implicity wait
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
by providing this imlicity wait, webdriver waits until 20 seconds before it returns object not found
explicit wait
WebDriverWait wait = new WebDriverWait(driver, timeoutInSeconds);
wait.until(ExpectedConditions.visibilityOfElementLocated(locator));
This will wait explicitly for given element..

Categories