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?
Related
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();
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();
Hello everyone I'm learning Selenium with Java. How can I select forum threads with new posts (posts the user has not already posted in).
Example of my assignment:
Given the list of threads at: http://siteownersforums.com/forumdisplay.php?s=ea4353212d6e4bcb50abb3f5790fa97a&f=3
Open each new thread that you haven't posted in. Hint: You'll want to
select the elements first, and then use the .click() method.
and this is what I have so far:
package main;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class Test {
public static void main(String[] args) {
//Set the Driver and the Webpage.
System.setProperty("webdriver.chrome.driver", "./chromedriver");
WebDriver chromeDriver = new ChromeDriver();
chromeDriver.get("http://siteownersforums.com/forumdisplay.php?s=ea4353212d6e4bcb50abb3f5790fa97a&f=3");
//Confirmation
System.out.println("Successfully Opened up: "+ chromeDriver.getTitle());
//Select each of the threads and click it.
for (int i =0; i< 27; i++){
WebElement tempThread = chromeDriver.findElement(By.cssSelector(""));
tempThread.click();
}
}//Closes method test
}//Closes class
I am a beginner in Selenium WebDriver and I'm using this test on the StackOverflow homepage. These will be the steps of my test:
Firstly, go to the homepage of the StackOverflow.
Then, move the mouse on the Users button such that it will be focused. (We don't have to click on it just hover the mouse over it.)
Now, find the active element on the screen (i.e. the element which is
currently focused) and then click on it.
I want the Users button to be clicked because it is focussed currently but that's not happening. Instead the Questions button is clicked on.
Here is my code for the same test.
package insertCartridge;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
public class Practice {
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
System.setProperty("webdriver.gecko.driver", "D:\\SELENIUM\\geckodriver-v0.18.0-win64\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS) ;
String baseUrl = "https://stackoverflow.com/";
driver.get(baseUrl);
driver.manage().window().setSize(new Dimension(1920, 1080));
WebElement Users = driver.findElement(By.xpath("/html/body/header/div/div[1]/nav/ol/li[5]"));
Thread.sleep(3000);
Actions builder = new Actions(driver);
builder.moveToElement(Users).perform();
Thread.sleep(3000);
driver.switchTo().activeElement().click();
}
}
I am not able to understand why I am not getting the expected output. Please help.
I don't know why Actions class is not focusing the element. Its an issue or something as so many time i have trouble with Actions class in Firefox browser.
Still you have one alternative way to Focus on desired element and then perform click on focused element i.e. JavascriptExecutor
Here you is your code :
JavascriptExecutor jse = (JavascriptExecutor) driver;
jse.executeScript("document.getElementById('nav-users').focus();");
System.out.println(driver.switchTo().activeElement().getTagName());
driver.switchTo().activeElement().click();
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(...);