How to import select class in webdriver? - java

I tried importing select class in my program using selenium webdriver,
but I am not able to import the predefined package.
Can anyone please guide me on this?
package com.siri.dev;
import org.apache.bcel.generic.Select;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.Wait;
import org.openqa.selenium.support.ui.WebDriverWait;
public class MyntraTests {
private WebDriver driver;
#Before
public void setup() {
intializedriver("firefox");
}
#Test
public void get() {
driver.get("http://www.myntra.com");
driver=waitForPageLoaded(driver);
driver.manage().window().maximize();
System.out.println("Page opened successfully");
WebElement element = driver.findElement(By.className("tab"));
org.openqa.selenium.support.ui.Select elem = new Select(element);
elem.selectByVisibleText("BIBA");
}
private void intializedriver(String browser) {
// TODO Auto-generated method stub
driver = new FirefoxDriver();
driver.manage().window().maximize();
}

import org.openqa.selenium.support.ui.Select;
Use
Select selectElement = new Select(driver.findElement(By.cssSelector("")));

The reason you are having this
org.openqa.selenium.support.ui.Select elem = new Select(element);
is cause you have imported a wrong class imported already
import org.apache.bcel.generic.Select;
remove that import by deleting it and then
import org.openqa.selenium.support.ui.Select
If you are using Eclipse you can always remove unused imports by ctrl+shift+o.

Select is a class of package org.openqa.selenium.support.ui
So you are supposed to do a import statement as follows:
import org.openqa.selenium.support.ui.Select;
and then you can do your task as:
Select elem = new Select(element);
elem.selectByVisibleText("BIBA");
U have imported a wrong package : import org.apache.bcel.generic.Select;

If you use Maven, you will want to know that packages under org.openqa.selenium.support are in artifact selenium-support. That does not get pulled in along with selenium-api or any of the selenium-*-driver artifacts. You can refer to the Selenium Maven information.

Related

Failing to get log entries from chrome console

Also some text to make stackoverflow allow me to post this sadkjghalksjdblkasnfblkjsfhavlkjdhaflkjansdgkjabslvknacdjlhasd;lfkhasojhgabsd;lvscogkansd;lfiyojgf;cl znxcgiuaerpagisdj;cvljahdfoiaguhsdf[lahdsisudzfgpasdhgoiuaefhblka;gkjahfg
My code:
package packege;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.logging.LogEntries;
import org.openqa.selenium.logging.LogEntry;
import org.openqa.selenium.logging.LogType;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.FluentWait;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.time.Duration;
import java.util.List;
import java.util.concurrent.TimeUnit;
public class Main {
public static void main(String[] args){
WebDriver driver = new ChromeDriver();
driver.get("https://hordes.io/players");
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("console.log(\"Hello Console!\")");
LogEntries entry = driver.manage().logs().get(LogType.BROWSER);
List<LogEntry> logs= entry.getAll();
System.out.println("Logs:");
for(LogEntry e: logs)
{
System.out.println("Message is: " +e.getMessage());
System.out.println("Level is: " +e.getLevel());
}
}
}
Console:
Output of my code:
Please tell me what is wrong? I am doing the same things as in guides.
Edit: I noticed that it caught error messages but not mine... Interesting.

I have issue while automating Odoo application

i am automating Odoo pos application. but while adding product to the cart, i got error like "Element is not clickable at point (659,166)". i have already created 1 item and trying to add that in cart but element is not found. need help.
package odoo1;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.NoAlertPresentException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.Wait;
import org.openqa.selenium.support.ui.WebDriverWait;
public class odoo {
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver","C:\\Chrome\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("start-maximized");
options.addArguments("disable-infobars");
WebDriver driver = new ChromeDriver(options);
driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);
// for Log in functionality
driver.get("https://www.odoo.com/trial?selected_app=point_of_sale");
driver.findElement(By.id("username")).sendKeys("mark");
driver.findElement(By.id("email")).sendKeys("mark#gmail.com");
driver.findElement(By.id("company-name")).sendKeys("odooo");
driver.findElement(By.id("phone")).sendKeys("561234897");
driver.findElement(By.id("country-id")).sendKeys("India");
driver.findElement(By.name("lang")).sendKeys("English");
Select sell=new Select(driver.findElement(By.name("company_size")));
sell.selectByIndex(2);;
Select sell1=new Select(driver.findElement(By.id("plan")));
sell1.selectByIndex(2);
driver.findElement(By.xpath(".//*[#id='wrapwrap']/main/div/div/div[2]/div[2]/div/form/input[8]")).click();
// for creating product
driver.findElement(By.xpath("html/body/div[3]/div[3]/div[1]/a[2]/div[2]")).click();
driver.findElement(By.xpath("html/body/nav/div/ul[1]/li[2]/a")).click();
driver.findElement(By.xpath("html/body/nav/div/ul[1]/li[2]/ul/li[3]/a/span")).click();
driver.findElement(By.xpath("html/body/div[3]/div[2]/div[1]/div/button")).click();
driver.findElement(By.xpath(".//*[#id='o_field_input_4']")).sendKeys("iphone");
driver.findElement(By.xpath(".//*[#id='o_field_input_4']")).click();
driver.findElement(By.xpath(".//*[#id='o_field_input_13']")).sendKeys("abcd");
driver.findElement(By.xpath(".//*[#id='o_field_input_14']")).sendKeys("123456");
driver.findElement(By.xpath(".//*[#id='o_field_input_15']"));
driver.findElement(By.xpath(".//*[#id='o_field_input_16']")).clear();
driver.findElement(By.xpath(".//*[#id='o_field_input_16']")).sendKeys("25000");
driver.findElement(By.xpath(".//*[#id='o_field_input_20']")).clear();
driver.findElement(By.xpath(".//*[#id='o_field_input_20']")).sendKeys("25000");
Thread.sleep(1000);
driver.findElement(By.xpath("html/body/div[3]/div[2]/div[1]/div/div[2]/button[1]")).click();
Thread.sleep(900);
driver.findElement(By.xpath("html/body/nav/div/ul[1]/li[1]/a/span")).click();
driver.findElement(By.xpath("html/body/div[4]/div/div/div[1]/div[2]/div/div[1]/button")).click();
Thread.sleep(500);
driver.findElement(By.xpath("html/body/div[1]/div[2]/div/div[2]/div[1]/div/div/div/div[2]/div[2]/table/tbody/tr[2]/td/div/div/div/div/span[2]/div[1]")).click();
Thread.sleep(700);
driver.findElement(By.xpath("//button")).click();
Thread.sleep(5000);
driver.findElement(By.xpath("html/body/div[1]/div[2]/div/div[2]/div[1]/div/div/div/div[2]/div[2]/table/tbody/tr[2]/td/div/div/div/div/span[2]/div[1]/img")).click();
}
}
By looking at the exception, it looks like the right path is not selected.
I have just checked the website which you are trying to automate.
Steps I have followed:
Click on Open Cart tab.
Select Shipping option.
Click on Add to Cart for the product which you want to add in your path.
Xpath Used for Add To Cart button:
//h3[normalize-space()="Opencart Marketplace"]/../div[3]//button
I have selected the product "Opencart Marketplace". You can select any product you want. Just replace Opencart Marketplace with the product which you want to select.
Code:
System.setProperty("webdriver.gecko.driver", "src/geckodriver");
WebDriver driver = new FirefoxDriver();
driver.get("https://store.webkul.com/OpenCart-Modules.html");
Thread.sleep(2);
driver.findElement(By.xpath("//h3[normalize-space()='Opencart Marketplace']/../div[3]//button")).click();
driver.findElement(By.xpath("//div[#class='product_cart_container']/button")).click();

"cannot instantiate the type select" while selecting an item from drop down

package Saradhi;
import java.util.concurrent.TimeUnit;
import org.apache.bcel.generic.Select;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class LoginAndNavigate {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.setProperty("webdriver.chrome.driver", "E:ChromeDriver\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
driver.get("http://opensource.demo.orangehrmlive.com/");
WebElement frmtime = driver.findElement(By.id("workShift_workHours_from"));
Select se = new Select(frmtime); // error line
I'm getting an error message in the above line.
I have tried importing related packages but still didn't work.
You are importing a wrong package. Try
import org.openqa.selenium.support.ui.Select;
instead of
import org.apache.bcel.generic.Select;
Use this import:
import org.openqa.selenium.support.ui.Select;
And remove this one:
import org.apache.bcel.generic.Select;

Install Selenium in Ubuntu 16.04 and check it using java code

I want to make a java program(GUI) which will auto-fill the username and password in a website on click of a button. I am new to networking concepts in java. A friend told to use selenium because that will make it easy but I am not able to use it successfully (tried code is shown below). I am using Ubuntu 16.04 OS and using gedit to write the source code. Any help would be highly appreciated.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
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.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;
public class pextax implements ActionListener {
JFrame frame;
JButton button;
.
.
.
public void GUI() throws Exception {
button = new JButton("Click to open PEXTAX official website");
button.addActionListener(this);
.
.
.
}
public void actionPerformed (ActionEvent event) {
if(event.getSource()==button) {
try {
DesiredCapabilities capabilities = DesiredCapabilities.firefox();
capabilities.setCapability("marionette", true);
WebDriver driver = new FirefoxDriver(capabilities);
driver.get("https://www.pextax.com");
driver.manage().window().maximize();
WebElement username = driver.findElement(By.name("username"));
WebElement password = driver.findElement(By.name("password"));
username.sendKeys("123456789");
password.sendKeys("1234");
}catch(Exception e) { }
}
}
}
vivek#vivek-HP-Notebook:~$ javac pextax.java
pextax.java:4: error: package org.openqa.selenium does not exist
import org.openqa.selenium.By;
^
pextax.java:5: error: package org.openqa.selenium does not exist
import org.openqa.selenium.WebDriver;
^
pextax.java:6: error: package org.openqa.selenium does not exist
import org.openqa.selenium.WebElement;
^
pextax.java:7: error: package org.openqa.selenium.firefox does not exist
import org.openqa.selenium.firefox.FirefoxDriver;
^
pextax.java:8: error: package org.openqa.selenium.support.ui does not exist
import org.openqa.selenium.support.ui.ExpectedCondition;
^
pextax.java:9: error: package org.openqa.selenium.support.ui does not exist
import org.openqa.selenium.support.ui.WebDriverWait;

Error :Cannot instantiate the type Select in selenium webdriver

I am testing a website with selenium webdriver.
I have imported all the jar files also, but still could not use Select Class in eclipse. It gives me an error: Select class cannot be instantiated.
I have also imported org.openqa.selenium.support.ui.Select
Following is my source code
import org.apache.bcel.generic.Select;
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 Dropdown {
public static void main(String[] args) {
// TODO Auto-generated method stub
WebDriver driver = new FirefoxDriver();
driver.get("http://www.makemytrip.com");
WebElementaddress=driver.findElement(By.xpath(".//[#id='to_typeahead1']"));
Select sc = new Select (address); // ERROR LINE
sc.selectByIndex(5);
}
}
You may try the below. Lets break it down into parts.
Select sc = new Select(driver.findElement(By.xpath("your Xpath match case")));
**//this will get the dropdown into sc object**
List<WebElement> we = sc.getOptions(); **//to get the options values into list**
System.out.println(we.size()); **//to print the size in console, this and
previous lines for debug/cross checking**
sc.selectByIndex(5); **//this will select the 5th index and 6th value(indexing starts from 0)**
Cheers!
Import org.openqa.selenium.support.ui.Select; package in your project, instead of org.apache.bcel.generic.Select;
Error will go off.
I see that the Xpath you have provided is not correct.
It should be .//*[#id='to_typeahead1']
Moreover, this element is not a select box. Its an input box.
What you can probably try is to click on the element and then try to click on the option you want to select.
I have used the org.openqa.selenium.support.ui.Select package and it worked fine.
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;
import org.testng.annotations.Test;
public class NewClassTest {
WebDriver driver = new FirefoxDriver();
#Test
public void selectOption() {
driver.get("http://www.w3schools.com/tags/tryit.asp?filename=tryhtml_select");
WebElement address = driver.findElement(By.tagName("select"));
Select ab = new Select(address);
}
}

Categories

Resources