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
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();
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?
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(...);
I have two classes called AdminLogin and CreateCustomer in the same package 'testing. I written a method commonLogin() in AdminLogin.java for LOGIN purpose.So i need to call the commonLogin() in CreateCustomer class,instead of writing the same login code.How can i do this? Any one please tell me.
AdminLogin.java:
package testing;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class AdminLogin {
public static void commonLogin()
{
// Create a new instance of the Firefox driver
// Notice that the remainder of the code relies on the interface,
// not the implementation.
WebDriver driver = new FirefoxDriver();
// And now use this to visit BluBilling
driver.get("http://testing.blubilling.in");
//Fetching the username and password
WebElement element = driver.findElement(By.id("j_username"));
// Enter something to search for
element.sendKeys("xxxx");
WebElement element1 = driver.findElement(By.id("j_password"));
element1.sendKeys("zzzz");
// Entering into bluBilling application
element.submit();
}
public static void main(String[] args) {
AdminLogin .commonLogin();
}
}
and CreateCustomer.java
package testing;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class CreateCustomer {
public static void main(String[] args) {
// Create a new instance of the Firefox driver
// Notice that the remainder of the code relies on the interface,
// not the implementation.
WebDriver driver = new FirefoxDriver();
// And now use this to visit BluBilling
driver.get("http://testing.blubilling.in");
//Fetching the username and password
WebElement element = driver.findElement(By.id("j_username"));
// Enter something to search for
element.sendKeys("xxxx");
WebElement element1 = driver.findElement(By.id("j_password"));
element1.sendKeys("zzzz");
// Entering into bluBilling application
element.submit();
//Creating a Customer
driver.navigate().to("https://testing.blubilling.in/customer /createCustomer2");
//Entering the details of the Customer
WebElement element4 = driver.findElement(By.id("name"));
element4.sendKeys("");
WebElement element5 = driver.findElement(By.id("firstName"));
element5.sendKeys("chris");
WebElement element6 = driver.findElement(By.id("lastName"));
element6.sendKeys("broad");
WebElement element7 = driver.findElement(By.id("emailPrimary"));
element7.sendKeys("chris#blusyn.com");
WebElement element8 = driver.findElement(By.id("username"));
element8.sendKeys("");
WebElement element9 = driver.findElement(By.id("password"));
element9.sendKeys("Chris1234");
WebElement element10 = driver.findElement(By.id("confirm"));
element10.sendKeys("Chris1234");
driver.manage().timeouts().implicitlyWait(1000, TimeUnit.SECONDS);
//Saving the Customer details
WebElement element11 = driver.findElement(By.cssSelector("input[value='Save']"));
element11.click();
}
}
In CreateCustomer:
AdminLogin.commonLogin();
is what you need.
But, I would recommend that CreateCustomer and AdminLogin be 2 separate functions in one single class, say Admin, since they are the actions that an admin could perform (if I understand your task right).
Goodluck.
I'm trying to use Selenium to retrieve all the car makes from the Autotrader website. There is a drop-down box that changes depending on which cars are available for sale at a given time.
I have tried many of the solutions listed on stackoverflow, but my code doesn't return anything.
Any help would be greatly appreciated!
Here is my code...
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import org.openqa.selenium.support.ui.Select;
public class AutotraderScraper {
public static void main(String[] args) throws InterruptedException
{
WebDriver driver = new HtmlUnitDriver();
// Visit autotrader website
driver.get("http://www.autotrader.co.uk/");
// Look for car make box
Select select = new Select(driver.findElement(By.id("searchVehiclesMake")));
// Get all options
List<WebElement> allOptions = select.getOptions();
// Iterate through available options
java.util.Iterator<WebElement> i = allOptions.iterator();
// Print options
while(i.hasNext()) {
WebElement row = i.next();
System.out.println("Found an option!");
System.out.println(row.getText());
}
}
}
This is a duplicate id and is being used in two different places of this page.
I suggest you use xpath to find the select element The following xpath can be used
//h1[.='Find new & used cars']/..//*[#id='searchVehiclesMake']
Target element in Find new & used cars section