Selenium Ebay Script - selenium

I have logged into Ebay and want to click the hyperlink of 'My collections' which is under "G'day [username]". The issue is now I can not find the element of 'My collections'. The error message is "Unable to locate element: (//li[#id='gh-ucol']/a)
Please refer to the below steps how I replicate:
Open Ebay via Firefox
Click Log in hyperlink
Enter the user name password then click log in button
Click "G'day [username]"
Select 'My Collection' from the droplist
This is my Selenium Java Script:
import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class KevinTest {
public static void main(String[] args) throws InterruptedException{
WebDriver driver = new FirefoxDriver();
driver.get("http://www.ebay.com.au");
driver.manage().window().maximize();
Thread.sleep(20);
driver.findElement(By.linkText("Sign in")).click();
Thread.sleep(100);
driver.findElement(By.xpath("(//input[#placeholder='Email or username'])[2]")).sendKeys("my#username");
driver.findElement(By.xpath("(//input[#placeholder='Password'])[1]")).sendKeys("mypassword");
driver.findElement(By.id("sgnBt")).click();
boolean tf;
try {
driver.findElement(By.id("errf")).getText();
tf = true;
}catch(NoSuchElementException e) {
tf = false;
}
if (tf == true) {
System.out.println("Incorrect Password");
driver.close();
}else {
System.out.println("Log in successfully");
}
driver.findElement(By.id("gh-eb-u")).click();
Thread.sleep(100);
driver.findElement(By.xpath("(//*[#id='gh-ucol']/a)")).click();
}}

Its a good practice to use implicit wait after initiating the driver.
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
Use the below id for the 'Click "G'day [username]'.
driver.findElement(By.id("gh-ug")).click();
Use the below xpath for collection.
driver.findElement(By.id("//*[#id="gh-ucol"]/a")).click();
Since we have used implicit wait you can remove all other thread.sleeps. Hope this helps. Thanks.

Related

I cannot select from city in MakeMyTrip wesite with Selenium WebDriver

I cannot select from city in MakeMyTrip wesbite with Selenium WebDriver. It does not select the specified city. It should click on the specified city and display the city in the "from city" field.
Here is my code:
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.interactions.Actions;
import org.openqa.selenium.support.ui.Select;
public class Makemytrip {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "D:\\Selenium\\chromedriver_win32\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("https://www.makemytrip.com/");
String actualTitle=" ";
actualTitle=driver.getTitle();
String url=driver.getCurrentUrl();
System.out.println(url);
String expectedTitle=actualTitle;
if(actualTitle.contentEquals(expectedTitle)){
System.out.println("Test pass");
}
else{
System.out.println("Test fail");
}
WebElement roundtrip=driver.findElement(By.xpath(".//label[#class='label_text flight-trip-type']"));
roundtrip.click();
System.out.println("Select one way option");
//driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.findElement(By.xpath(".//div/section/div/div/input[#id='hp-widget__sfrom']")).click();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
List<WebElement> dd_menu=driver.findElements(By.xpath(".//ul[#id='ui-id-1']/li/div/p/span"));
for(int i=0;i<dd_menu.size();i++){
WebElement element=dd_menu.get(i);
String innerhtml=element.getAttribute("innerHTML");
if(innerhtml.contentEquals("Hyderabad, India HYD" )){
element.click();
break;
}
System.out.println("Values in list " +innerhtml);
}
}
}
Input field is kind of autosuggestive dropdown here...you should use sendKeys here...one more thing use explicit wait here...I've used Thread.sleep() here...you can use WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.id("someid"))); Modify your code with following snippet..e.g.
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "D:\\Selenium\\chromedriver_win32\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("https://www.makemytrip.com/");
String actualTitle=" ";
actualTitle=driver.getTitle();
String url=driver.getCurrentUrl();
System.out.println(url);
String expectedTitle=actualTitle;
if(actualTitle.contentEquals(expectedTitle)){
System.out.println("Test pass");
}
else{
System.out.println("Test fail");
}
WebElement roundtrip=driver.findElement(By.xpath(".//label[#class='label_text flight-trip-type']"));
roundtrip.click();
System.out.println("Select one way option");
WebElement we = driver.findElement(By.xpath(".//div/section/div/div/input[#id='hp-widget__sfrom']"));
we.clear();
Thread.sleep(3000);
we.sendKeys("hyde");
Thread.sleep(3000);
we.sendKeys(Keys.RETURN);
}
I was facing the same issue during testing. Whenever you try to open "makemytrip.com" site.
Click here to check whether you are getting the same window
Because of this menu, it block the entire WebElement to get access. To resolve this problem, follow the steps given below: -
Press Ctrl+Shift+i
Click on Inspect Pointer from the left top window of inspect tools.
Drag the pointer on window screen.
Select and copy the selected tagName's Xpath available on the inspect tool.
Here is the image of intermediate help for new learner
Paste it on your respective working IDE as shown below. Here is the working code to resolved the issue
Hope this will help you.
Please use the below code to solve the issue.
String driverPath = "C:\\geckodriver.exe";
System.setProperty("webdriver.gecko.driver", driverPath);
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.get("https://www.makemytrip.com/flights/");
Thread.sleep(2000);
Actions act = new Actions(driver);
WebElement ele= driver.findElement(By.xpath("//label[#for=\"fromCity\"]"));
act.doubleClick(ele).perform();

Controls not recognizing inside a SignIn form using Selenium WebDriver?

I'm trying to automate Sign In functionality in www.sears.com, but could not recognize the Email text field using the below code
package com.bigbasket.framework.lab;
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.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class Sears {
public static void main(String[] args) {
WebDriver browser = new FirefoxDriver();
browser.get("http://www.sears.com/en_us.html");
browser.manage().window().maximize();
browser.findElement(By.xpath("//*[#id='gnf_01_tree_item_5']/span/a")).click();
WebElement currentElement = browser.findElement(By.xpath("//*[#id='myProfiles']/div"));
Actions actions = new Actions(browser);
actions.moveToElement(currentElement).build().perform();
browser.findElement(By.xpath("//*[#id='subnavDD_myProfile']/ul/li[1]/p/a")).click();
try{
WebElement formElement = browser.findElement(By.id("loginFormDisplay"));
currentElement = formElement.findElement(By.xpath("//*[#id='email']"));
}catch(Exception e){
System.out.println(e.getMessage());
}
WebDriverWait wait = new WebDriverWait(browser, 30);
wait.until(ExpectedConditions.visibilityOf(currentElement));
currentElement.sendKeys("srinimarva#gmail.com");
browser.quit();
}
}
Could you please help me in resolving this? Apologize I'm an amateur in Selenium WebDriver for asking a silly question.
your email field is in iframe so to access it you need to switch to frame first, following is the working code for your issue :
WebDriver browser = new FirefoxDriver();
browser.get("http://www.sears.com/en_us.html");
browser.findElement(By.xpath("//*[#id='gnf_01_tree_item_5']/span/a")).click();
WebElement currentElement = browser.findElement(By.xpath("//*[#id='myProfiles']/div"));
Actions actions = new Actions(browser);
actions.moveToElement(currentElement).build().perform();
WebDriverWait wait = new WebDriverWait(browser, 10);
wait.until(ExpectedConditions.visibilityOf(browser.findElement(By.xpath(".//*[#id='subnavDD_myProfile']/ul/li[1]/p/a"))));
browser.findElement(By.xpath(".//*[#id='subnavDD_myProfile']/ul/li[1]/p/a")).click();
try{
WebElement frameElement = browser.findElement(By.xpath(".//iframe[contains(#name, 'easyXDM_default')]"));
browser.switchTo().frame(frameElement);
}
catch(Exception e){
System.out.println(e.getMessage());
}
browser.findElement(By.name("email")).sendKeys("srinimarva#gmail.com");
browser.quit();

Selenium sendkeys ctrl+5 is not working

I have a requirement wherein
I need to invoke F12 developer tool in IE.
Navigate to profiler tab.
start profiler
I am using IE 9. In the below code, I am trying to navigate by entering ctrl5. It is not working.
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.interactions.HasInputDevices;
import org.openqa.selenium.interactions.Keyboard;
import org.testng.annotations.Test;
import org.openqa.selenium.remote.DesiredCapabilities;
public class InvokeIEbrowser {
#Test
public void IEdriver() {
System.setProperty("webdriver.ie.driver",
"D:\\2015\\softwares\\IEDriverServer_x64_2.48.0\\IEDriverServer.exe");
DesiredCapabilities capabilities = DesiredCapabilities.internetExplorer();
capabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
capabilities.setCapability(InternetExplorerDriver.IE_ENSURE_CLEAN_SESSION, true);
capabilities.setCapability("ACCEPT_SSL_CERTS", true);
WebDriver driver = new InternetExplorerDriver();
/*
Keyboard keyboard = ((HasInputDevices) driver).getKeyboard();
keyboard.sendKeys(Keys.F12);
*/
/*
driver.get("http://mail.yahoo.com");
System.out.println(driver.getTitle());
*/
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//driver.FindElement(By.XPath("String")).SendKeys(Keys.NumberPad5);
//keyboard.sendKeys(Keys.CONTROL);
//getKeyboard().sendKeys(Keys.CONTROL, Keys.F5);
Actions actionObject = new Actions(driver);
actionObject.sendKeys(Keys.F12).perform();
//actionObject.sendKeys(Keys.CONTROL).sendKeys(Keys.NUMPAD5).keyUp(Keys.CONTROL).perform();
actionObject.sendKeys(Keys.CONTROL.NUMPAD5).perform();
System.out.println("done");
}
}
You could try using Keys.chord() (more about it here):
driver.FindElement(By.XPath("String")).SendKeys(Keys.chord(Keys.CONTROL, Keys.NUMPAD5 ));
or by using the Action class and the unicode representation:
Actions action = new Actions();
action.keyDown(Keys.CONTROL).sendKeys(String.valueOf('\u0035')).perform();
You can find more references about the unicode representation here.
Are you just trying to send the hotkey to the page? Try an extension of driver...
public static void sendCtrl5(this IWebDriver Driver)
{
Driver.FindElement(By.TagName("body")).Click(); // make window active
new Actions(Driver)
.SendKeys(Keys.Control + Keys.NumberPad5)
.Perform();
}
Use: driver.sendCtrl5();

Cannot open the URLs on the page using Selenium webdriver and Java

I have written a code using Selenium webdriver and Java to open the URL listed in one particular div on the webpage.
The code opens the first URL goes back to the webpage and the throws the error "Element not found in the cache" - perhaps the page has changed since it was looked up.
Please find the code for reference.
import java.util.*;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class OpenLinks {
public static void OpenLinkAs(String username, String password) throws Exception
{
WebDriver driver;
driver = new FirefoxDriver();
driver.get("http://stg.n**w.le****ine.com/");
driver.findElement(By.name("username")).sendKeys(username);
driver.findElement(By.name("password")).sendKeys(password);
driver.findElement(By.xpath("/html/body/div/div/div[2]/div/form/p[3]/input")).submit();
String content = driver.getPageSource();
if (content.contains("Create new Project"))
{
List<WebElement> elementsList = driver.findElements(By.xpath("//div[#class='recent-project block-link']"));
int RowCount = elementsList.size();
System.out.println(RowCount);
System.out.println(elementsList);
for (int i = 0; i < RowCount; i++)
{
//String oldTab = driver.getWindowHandle();
WebElement url = elementsList.get(i);
//String text= url.getText();
System.out.println(url);
// System.out.println(text);
url.click();
Thread.sleep(5*1000);
driver.navigate().back();
//driver.switchTo().window(oldTab);
}
}
}
public static void main(String[] args) throws Exception
{
OpenLinkAs("username", "password");
}
}
If the element is not found on the page its likely to have been dropped in the DOM. Declare t again and attempt to click it.
What is being returned in the stacktrace, i.e. at what point in the code is the error thrown at?

While running selenium script my csv download file script get skipped due to switch to other area

Run the below script properly that time not moved any other area but if perform any action I mean move to any other area that time my download file code not work.
Is the issue related to focus or need some changes. using firefox 21 version browser and selenium 2.33. Please suggest.
Here is the code:
package mypackage;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import org.junit.Before;
import org.junit.Test;
import Data.Function;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.junit.After;
public class Report extends Function {
Object Open_a_popup_window;
String Positioned_Popup ;
Object JavaScript_Popup_Windows ;
#Before
public void setUp() throws Exception {
driver = new FirefoxDriver();
baseUrl = "my url";
}
#Test
public void Report1() throws Exception {
Function object = new Function();
object.Login();
/* Enter Report Details */
driver.get(baseUrl + "//");
driver.get(baseUrl + "//");
/*Run*/
driver.findElement(By.xpath("")).click();
Thread.sleep(30000);
WebElement Action = driver.findElement(By.xpath(""));
for (String winHandle : driver.getWindowHandles()) {
driver.switchTo().window(winHandle);
((JavascriptExecutor) driver).executeScript("window.focus();");}
String linktxt = Action.getText();
if (linktxt.equalsIgnoreCase("Record not found.")) {
System.out.println("Report contains No Data:");
}
if (linktxt.equalsIgnoreCase("FAILED")) {
System.out.println("Report failed");
} else {
driver.findElement(By.xpath("/img")).click();
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyPress(KeyEvent.VK_ENTER);
Thread.sleep(10000);
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyPress(KeyEvent.VK_ENTER);
Thread.sleep(10000);
System.out.println("Report Passed");
}
}
}
#After
public void teardown() {
driver.close();
//System.exit(0);
}
}
I see you've used Robot with pressing Enter, and of course it'll work on the window which has the focus!
So once you moves out, the enters are fired on the newly focused window.
The pop up window was browser pop which was not handled by robot keys, so i used third party app "auto it" but it has limitation only used for windows operating system , not supported by unix or linux .