unable to select item from dropdown in selenium webdriver - selenium

I am trying to select a item from dropdown since in html tag is not used as Select for dropdown, then I used to select dropdown item through Action.
My question is this standard way to code to select dropdown item in this situation or should should i need to change my code.
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.Select;
public class test
{
static WebDriver driver;
public static void main(String[] args)
{
System.setProperty("webdriver.chrome.driver", "D:\\rakesh\\software\\selenium browser\\New folder\\chromedriver.exe");
driver=new ChromeDriver();
driver.get("https://app.crossover.com/x/marketplace/available-jobs");
driver.manage().window().maximize();
driver.findElement(By.cssSelector(".btn.btn-default.form-control.ui-select-toggle")).click();
WebElement dropdonw_ele = driver.findElement(By.xpath("/html/body/div[2]/div[3]/div/div/div/div[2]/form/div/div[2]/job-label-select/div/ul/li/div[5]/span/div/span"));
Actions act = new Actions(driver);
act.build();
act.moveToElement(dropdonw_ele).click();
act.perform();
}
}

if your dropdown is a native html dropdown take a look at the org.openqa.selenium.support.ui.Select class.
//simplify this xpath expression?
WebElement dropdonw_ele = driver.findElement(By.xpath("/html/body/div[2]/div[3]/div/div/div/div[2]/form/div/div[2]/job-label-select/div/ul/li/div[5]/span/div/span"));
Select dropDown = new Select(dropdonw_ele);
dropDown.selectByValue("your item value");
if it's no native html tag you either can use actions or you can click the value box direct without using an action. If this doesn't work because the dropdown value element is not visible you can click it with JavaScript.
private void clickWithJavaScript(WebElement target) {
JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript("arguments[0].click()", target);
}

Try this way.
Note: Use Relative xpath, not absolute xpath.
driver.get("https://app.crossover.com/x/marketplace/available-jobs");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
driver.findElement(By.xpath("//span[#class='ui-select-match-text pull-left']/span")).click(); //click on All job categories first
Thread.sleep(3500);
driver.findElement(By.xpath("//div[#class='ng-scope']/span[contains(text(), 'C++')]")).click(); //By using xpath method click on C++ from the dropdown selection.
Thread.sleep(3500);

Hi Rocky You may not need to use action class.
Below is the workable code you can try at your end and let me know.
public class test
{
static WebDriver driver;
public static void main(String[] args)
{
System.setProperty("webdriver.chrome.driver", "D:\\rakesh\\software\\selenium browser\\New folder\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://app.crossover.com/x/marketplace/available-jobs");
Thread.sleep(3000L);
driver.findElement(By.xpath(".//*[#ng-click='$select.toggle($event)']")).click();
Thread.sleep(3000L);
driver.findElement(By.xpath(".//*[contains(text(),'iOS and Android')]")).click();
Thread.sleep(3000L);
}
}
Please try at your end.
Happy Learning :-)

Related

how handle auto suggest in "from" and "destination" box for this website "https://www.goibibo.com/" in selenium

how handle auto suggest in "from" and "destination" box for this website "https://www.goibibo.com/" in selenium.
please help
I tired using the basic method but unable to get the X path of the auto suggestion drop down
Unable to click on the drop down
package basic;
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.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class goibibo {
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
WebDriver driver = new ChromeDriver();
driver.get("https://www.goibibo.com/");
new WebDriverWait(driver, 20)
.until(ExpectedConditions.elementToBeClickable(By.xpath("//input[#id='gosuggest_inputSrc']")))
.sendKeys("Mum");
List<WebElement> myList = new WebDriverWait(driver, 20).until(
ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath("//*[#id=\"react-autosuggest-1\"]")));
for (WebElement element : myList) {
if (element.getText().contains("Mumbai"))
;
element.click();
}
}
}
Chrome Browser
First how to Find XPATH of auto populate box in Chrome Browser open your website than click on Inspect element and click on source Tab now, click for opening your auto populate box and Press **F8** **Key for pause debugger**. Then click on your Element tab and you can easily get your xpath refer below snap for more information. so it will freeze your HTML.
Now click on Elements an Create your own xpath.
Fire Fox Browser
Second how to find xpath of Auto Populate box in Firefox - Open your Firefox and Right click and click on inspect elements on your website. there is option of animation so it will open all your DOM Expanded like below image. so by reading this dom structure you can create easily your XPATH.
Not how to find Elements from auto populate box. Refer below code snippet for that.
package com.software.testing;
import java.util.List;
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.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class Testingclass extends DriverFactory {
private static WebDriver driver = null;
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "your driver path");
driver = new ChromeDriver();
driver.get("https://www.goibibo.com/");
new WebDriverWait(driver, 20)
.until(ExpectedConditions.elementToBeClickable(By.xpath("//input[#id='gosuggest_inputSrc']")))
.sendKeys("A");
Thread.sleep(1000);
List<WebElement> myList = new WebDriverWait(driver, 20).until(ExpectedConditions.presenceOfAllElementsLocatedBy(
By.xpath("//div[#class='dib marginL10 pad0 textOverflow width90']/div/span")));
for (int i = 0; i < myList.size(); i++) {
System.out.println(myList.get(i).getText());
if (myList.get(i).getText().equals("Ahmedabad")) {
myList.get(i).click();
break;
}
}
}
}
Don't forgot to use break after your conditional statement else it
will thrown an exception.
So you can try one solution please find the below screenshot,
As you can see in screenshot if i type M in text box then dropdown shows the record respect to letter 'M' and if you see in source the <ul> which is dynamic as you see just below <input> so you need to handle that dropdown by it's locator it is dynamic hence first you need to pass some text in text box and after that you need to select the element from the drop down using Select in selenium you use selectByVisibleText("") or what ever or you can use List<Element> you can store all the respected sources (Mumbai, Mysore ,etc)coming from dropdown and use it wisely
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[#id='gosuggest_inputSrc'"]))).sendKeys("M");
List<WebElement> myList = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath("Xpath of the dynamic drop down")));
for (WebElement element:myList) {
if(element.getText().contains("Mumbai"));
element.click();
}
I gave you an idea let me know if you need any further help
I have automated it through selenium with python. Its collecting all suggested cities in a list and then clicking the required one.
from selenium import webdriver
driver = webdriver.Chrome()
driver.maximize_window()
driver.get("https://www.goibibo.com/")
driver.implicitly_wait(3)
listCity = []
driver.find_element_by_xpath("//input[#id='gosuggest_inputSrc']").send_keys("JA")
cities = driver.find_elements_by_xpath("//div[#class='mainTxt clearfix']//preceding-sibling::span")
for city in cities:
listCity.append(city.text)
for city in cities:
if "Jagdalpur" in city.text:
city.click()
break
print(listCity)
print(len(listCity))
Use below code it will work
Webelement ele=driver.findelement()
Actions ob = new Actions(driver);
ob.moveToElement(ele);
ob.click(ele);
Action action = ob.build();
action.perform();

How to handle calendar for "departure" box and "return" box in Selenium? [duplicate]

how handle auto suggest in "from" and "destination" box for this website "https://www.goibibo.com/" in selenium.
please help
I tired using the basic method but unable to get the X path of the auto suggestion drop down
Unable to click on the drop down
package basic;
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.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class goibibo {
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
WebDriver driver = new ChromeDriver();
driver.get("https://www.goibibo.com/");
new WebDriverWait(driver, 20)
.until(ExpectedConditions.elementToBeClickable(By.xpath("//input[#id='gosuggest_inputSrc']")))
.sendKeys("Mum");
List<WebElement> myList = new WebDriverWait(driver, 20).until(
ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath("//*[#id=\"react-autosuggest-1\"]")));
for (WebElement element : myList) {
if (element.getText().contains("Mumbai"))
;
element.click();
}
}
}
Chrome Browser
First how to Find XPATH of auto populate box in Chrome Browser open your website than click on Inspect element and click on source Tab now, click for opening your auto populate box and Press **F8** **Key for pause debugger**. Then click on your Element tab and you can easily get your xpath refer below snap for more information. so it will freeze your HTML.
Now click on Elements an Create your own xpath.
Fire Fox Browser
Second how to find xpath of Auto Populate box in Firefox - Open your Firefox and Right click and click on inspect elements on your website. there is option of animation so it will open all your DOM Expanded like below image. so by reading this dom structure you can create easily your XPATH.
Not how to find Elements from auto populate box. Refer below code snippet for that.
package com.software.testing;
import java.util.List;
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.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class Testingclass extends DriverFactory {
private static WebDriver driver = null;
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "your driver path");
driver = new ChromeDriver();
driver.get("https://www.goibibo.com/");
new WebDriverWait(driver, 20)
.until(ExpectedConditions.elementToBeClickable(By.xpath("//input[#id='gosuggest_inputSrc']")))
.sendKeys("A");
Thread.sleep(1000);
List<WebElement> myList = new WebDriverWait(driver, 20).until(ExpectedConditions.presenceOfAllElementsLocatedBy(
By.xpath("//div[#class='dib marginL10 pad0 textOverflow width90']/div/span")));
for (int i = 0; i < myList.size(); i++) {
System.out.println(myList.get(i).getText());
if (myList.get(i).getText().equals("Ahmedabad")) {
myList.get(i).click();
break;
}
}
}
}
Don't forgot to use break after your conditional statement else it
will thrown an exception.
So you can try one solution please find the below screenshot,
As you can see in screenshot if i type M in text box then dropdown shows the record respect to letter 'M' and if you see in source the <ul> which is dynamic as you see just below <input> so you need to handle that dropdown by it's locator it is dynamic hence first you need to pass some text in text box and after that you need to select the element from the drop down using Select in selenium you use selectByVisibleText("") or what ever or you can use List<Element> you can store all the respected sources (Mumbai, Mysore ,etc)coming from dropdown and use it wisely
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[#id='gosuggest_inputSrc'"]))).sendKeys("M");
List<WebElement> myList = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath("Xpath of the dynamic drop down")));
for (WebElement element:myList) {
if(element.getText().contains("Mumbai"));
element.click();
}
I gave you an idea let me know if you need any further help
I have automated it through selenium with python. Its collecting all suggested cities in a list and then clicking the required one.
from selenium import webdriver
driver = webdriver.Chrome()
driver.maximize_window()
driver.get("https://www.goibibo.com/")
driver.implicitly_wait(3)
listCity = []
driver.find_element_by_xpath("//input[#id='gosuggest_inputSrc']").send_keys("JA")
cities = driver.find_elements_by_xpath("//div[#class='mainTxt clearfix']//preceding-sibling::span")
for city in cities:
listCity.append(city.text)
for city in cities:
if "Jagdalpur" in city.text:
city.click()
break
print(listCity)
print(len(listCity))
Use below code it will work
Webelement ele=driver.findelement()
Actions ob = new Actions(driver);
ob.moveToElement(ele);
ob.click(ele);
Action action = ob.build();
action.perform();

How to handle login window in selenium for https://www.flipkart.com/

When open https://www.flipkart.com/ a window opens for login. How to handle this window in selenium?
System.setProperty("webdriver.gecko.driver", "F:\\Software_Sel\\GekoDriver\\geckodriver-v0.16.1-win64\\geckodriver.exe");
WebDriver wd = new FirefoxDriver();
wd.get("https://www.flipkart.com/")
WebElement e1= wd.findElement(By.className("_2AkmmA _29YdH8"));
e1.click();
I have also tried iframe. But cant handle.Kindly help.
The class name you are using _2AkmmA _29YdH8 contains two class. ClassName locators only work for single class name .Instead of that you should use css ._2AkmmA._29YdH8
WebElement e1=
wd.findElement(By.cssSelector("._2AkmmA._29YdH8"))
Hope this helps u
The login window and the elements within are part of the same HTML DOM so you need to induce WebDriverWait for the element to be clickable and you can use the following solution:
Code Block:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class Login_Window_Flipkart {
public static void main(String[] args) {
System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\\\geckodriver.exe");
WebDriver wd = new FirefoxDriver();
wd.get("https://www.flipkart.com/");
new WebDriverWait(wd, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//span[contains(.,'Enter Email/Mobile number')]//preceding::input[1]"))).sendKeys("Abhijit#Datta.com");
wd.findElement(By.xpath("//span[contains(.,'Enter Password')]//preceding::input[1]")).sendKeys("Abhijit#Datta.com");
}
}
Browser Snapshot:

Drag and Drop not working in selenium code

Drag and Drop is not working in my selenium code below, can anyone help me?
package selenium;
import java.util.concurrent.TimeUnit;
import org.apache.log4j.BasicConfigurator;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.WebElement;
public class draganddrop {
public static void main(String[] args){
BasicConfigurator.configure();
FirefoxDriver driver=new FirefoxDriver();
driver.get("http://jqueryui.com/droppable/");
driver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
WebElement from= driver.findElementByXPath("html/body/div[1]");
WebElement to=driver.findElementByXPath("html/body/div[2]");
new Actions(driver).dragAndDrop(from, to).build().perform();
//Actions mouseoveron=new Actions(driver);
//mouseoveron.click().dragAndDrop(from, to).build().perform();
}
}
Few things I observed in your code that
You need to first replace
WebElement from= driver.findElementByXPath("html/body/div[1]");
with
WebElement from = driver.findElement(By.xpath("html/body/div[1]"))
And
WebElement to=driver.findElementByXPath("html/body/div[2]");
with
WebElement to = driver.findElement(By.xpath("html/body/div[2]"));
You can use following code to work out:
driver.navigate().to("http://jqueryui.com/droppable/");
//Wait for the frame to be available and switch to it
WebDriverWait wait = new WebDriverWait(driver, 5);
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.cssSelector(".demo-frame")));
WebElement Sourcelocator = driver.findElement(By.cssSelector(".ui-draggable"));
WebElement Destinationlocator = driver.findElement(By.cssSelector(".ui-droppable"));
dragAndDrop(Sourcelocator,Destinationlocator);
Your drag and drop element present under iFrame tag. So first you need to switch into frame and then perform drag and drop.
Use below code :
System.setProperty("webdriver.chrome.driver","D:/Application/chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("http://jqueryui.com/droppable/");
driver.manage().timeouts().implicitlyWait(45, TimeUnit.SECONDS);
driver.switchTo().frame(0); // either use index or frame element
WebElement from= driver.findElement(By.id("draggable"));
WebElement to=driver.findElement(By.id("droppable"));
new Actions(driver).dragAndDrop(from, to).build().perform();
Please note : if element id is there then please use id selector instead of xpath
You can surely do it using below code. Just ensure that your xpath is correct or else you can't do it using any of the options.
WebElement from = driver.findElement(By.xpath("html/body/div[1]"));
WebElement to = driver.findElement(By.xpath("html/body/div[2]"));
Actions action1 = new Actions(driver);
action1.clickAndHold(from).moveToElement(to).release(from).build().perform();
Let me know in case of any issues but try this one it will work :)
Find your own working code block with some simple tweaks in it:
//BasicConfigurator.configure();
System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
WebDriver driver=new FirefoxDriver();
driver.get("http://jqueryui.com/droppable/");
driver.manage().timeouts().implicitlyWait(5,TimeUnit.SECONDS);
driver.switchTo().frame(driver.findElement(By.xpath("//iframe[#class='demo-frame']")));
WebElement from = driver.findElement(By.id("draggable"));
WebElement to = driver.findElement(By.id("droppable"));
new Actions(driver).dragAndDrop(from, to).build().perform();

How to Handle Selenium Frame

I am trying to login to this site
But when the page loads it shows a frame.
I tried to switch to frame but all in vain
public void logon(String Username,String Password,String trns)
{
Configuration.driver.get(Configuration.URL);
Configuration.driver.manage().timeouts().implicitlyWait(8,TimeUnit.SECONDS);
Configuration.driver.switchTo().
//Configuration.driver.findElement(By.xpath("//a[#text()='Close Window']")).click();
usrId.sendKeys("Username");
pswd.sendKeys("Password");
tranId.sendKeys("trns");
logOn.click();
}
You can easily get code for Frame switch. Please search for the question before posting it.
//Switch to Iframe
WebElement iframe = driver.findElement(By.xpath("Xpath of frame"));
driver.switchTo().frame(iframe);
//Perform your Task.
driver.switchTo().defaultContent();// Iframe is Switched to Main Again
Check this links for frame Switch:-
Click here
Hi Ketan.
The problem with your code might be wait issue. Once you click on continue button in the pop-up window, it requires some time interact with the next element. So for those you need to declare some with in your code.
Check this link for waits
Here is the working code. I tried it. please check this, it will resolve your problem.
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;
import org.openqa.selenium.support.ui.Select;
public class userId {
public static void main(String[] args) throws InterruptedException {
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
driver.manage().window().maximize();
driver.get("https://qlb21.resources.hewitt.com/cl77ybr5qc/ybr5cl772b/CsLogn010InptOpen.do?fTkn=539f4eddc99aef9eb1c8da11d13a3654&fWdw=intro&eWlmYBR5ClntId=00398&wdw=primary&fPg=%2FCsLogn005WelcOpen");
System.out.println("Entered Url");
WebElement frame=driver.findElement(By.xpath("//*[#id='lightbox_iframe_cookieBanner']"));
driver.switchTo().frame(frame);
driver.findElement(By.xpath("//*[#id='lightboxarea']/div/div/a[1]")).click();
driver.switchTo().defaultContent();
driver.findElement(By.xpath("//*[#id='usrId']")).sendKeys("hari");
System.out.println("Entered the userid");
driver.findElement(By.xpath("//*[#id='pswd']")).sendKeys("Password");
System.out.println("Entered the Password");
}
}