Actions expected out of the code below :
User successfully logs in.
User moves to the top right corner of the website and clicks on the greeting link "Hi ....!".
Step 2 is not happening because the greeting hyperlink is not identified by WebDriver. What am I doing wrong?
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.get("http://flipkart.com");
driver.findElement(By.xpath(".//*[#id='container']/div/div/header/div[2]/div/div[1]/ul/li[8]/a")).click();
driver.findElement(By.xpath("//input[#class='fk-input login-form-input user-email']")).sendKeys("emailid");
driver.findElement(By.xpath("//input[#class='fk-input login-form-input user-pwd']")).sendKeys("password");
driver.findElement(By.xpath(".//*[#id='fk-mainbody-id']/div/div/div[1]/div/div[4]/div[7]/input")).click();
driver.findElement(By.linkText("Greeting _link")).click();
Error message:
Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"link text","selector":"Greeting _link"}
The HTML is:
<li class="_2sYLhZ _2mEF1S" data-reactid="26">
<a class="_1AHrFc _2k0gmP" data-reactid="27" href="#">Hi Neha!</a>
<ul class="_1u5ANM" data-reactid="28">
As I seeing after login there is no link which looks like as Hi username..!, but accroding to your comment I observe that you are talking about My account link which is visible in my case, I'm just rewriting your code which will automate from login to logout as below :-
driver.get("http://www.flipkart.com/");
driver.manage().window().maximize();
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.elementToBeClickable(By.linkText("Log In"))).click(); //it will click on login button
wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("input.user-email"))).sendKeys("user name"); //it will fill user name
wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("input.user-pwd"))).sendKeys('password'); //it will fill password
wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("input.login-btn"))).click(); //it will click on login button
WebElement myAccount = wait.until(ExpectedConditions.elementToBeClickable(By.partialLinkText("My Account")));
Mouse mouse = ((HasInputDevices)driver).getMouse();
mouse.mouseMove(((Locatable)hoverElement).getCoordinates()); //it will perform mouse over on My Account link, if in your case it show as 'Hi Neha!' you can replace it.
wait.until(ExpectedConditions.elementToBeClickable(By.partialLinkText("Log Out"))).click(); //it will click on logout click after mouse over
Hope it helps..:)
Related
I have a Selenium script to execute the following steps:
Launch the website - https://vusevapor.com/
Hit "I am 21+" button
Move to element devices.
Right-click on ciro complete kit.
The issue is that the right-click menu remains as is, and the element is getting clicked on the same page.
Here is my code:
//website
driver.get("https://vusevapor.com/");
//clicking on i am 21+ button
driver.findElement(By.xpath("/html/body/aside/div[2]/div/div/div[2]/div/a[1]/span")).click();
Thread.sleep(5000);
//xpath of devices menu
WebElement devices = driver.findElement(By.xpath("//*[#id=\"store.menu\"]/nav/ul/li[2]/a/span"));
//move to element devices
Actions act = new Actions(driver);
act.moveToElement(devices).build().perform();
Thread.sleep(3000);
//xpath of ciro complete kit
WebElement ciroKit = driver.findElement(By.xpath("//*[#id=\"store.menu\"]/nav/ul/li[2]/ul/li[2]/ul/li[1]/a/span"));
//right click on ciro complete kit
//*****Issue********right click is happening but the element is getting clicked on the same tab and right click menu remains as is
act.contextClick(ciroKit).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ENTER).build().perform();
try this:
option 1:
act.contextClick(ciroKit).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.RETURN).build().perform();
option 2-
Use Action and Robot class:
act.contextClick(ciroKit).build().perform();
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_DOWN);
robot.keyPress(KeyEvent.VK_ENTER);
Hope this helps.
I am learning to hover over a menu and click on a link int he sub-menu.
The scenario is go to the URL "https://www.amazon.in", hover over "Hello Sign in" and then click on the link "Start here".
I can hover over "Hello Sign in" using moveToElement() and sub menu is opening, but couldn't click on the link "Start here".
Here is my code.
WebElement signUp = driver.findElement(By.id("nav-link-yourAccount"));
Actions action = new Actions(driver);
action.moveToElement(signUp).build().perform();
WebElement startHere =
wait.until(ExpectedConditions.visibilityOfElementLocated(By.linkText("Start here")));
startHere.click();
The link text includes a dot at the end, you missed it in your code, Try By.linkText("Start here.") or By.partialLinkText("Start here")
Please try the below modified code and it's working as expected. First you can find the new Customer Div and then directly access the start here link as below.
WebElement signUp = driver.findElement(By.id("nav-link-yourAccount"));
Actions action = new Actions(driver);
action.moveToElement(signUp).build().perform();
WebElement newCustomer=driver.findElement(By.id("nav-flyout-ya-newCust"));
newCustomer.findElement(By.xpath(".//a")).click();
To access the URL https://www.amazon.in then hover over Hello Sign in and then click on the link Start here. you have to induce WebDriverWait for the element with text as Hello Sign in to be visible, then Mouse Hover over it and then induce WebDriverWait for the element with text as Start here. to be clickable and you can use the following solution :
Code Block :
System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("https://www.amazon.in/");
WebElement signUp = new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOfElementLocated(By.id("nav-link-yourAccount")));
new Actions(driver).moveToElement(signUp).build().perform();
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.linkText("Start here."))).click();
Browser Snapshot :
I logged in home page and from homepage to Invoice page
I Could notable access the Radio drop down in Invoice page .
Tried to get Page Url and title . its showing Home page and home url but the code is in Invoice page.
Actions Inv=new Actions(driver);
WebElement invconfg=driver.findElement(By.xpath(".//*[#id='Invoice ConfigurationToggle']"));
Inv.moveToElement(invconfg).click(invconfg).build().perform();
WebElement Vwopt=driver.findElement(By.xpath(".//*[#id='Invoice ConfigurationDiv']/ul/li[5]/a"));
Inv.moveToElement(Vwopt).click(Vwopt).build().perform();
WebElement filetpe=driver.findElement(By.xpath(".//*[#id='appDetail']/table[1]/tbody/tr[1]/td[2]/img"));
Select filetype=new Select(filetpe);
filetype.selectByValue("ALL");
List<WebElement> howold=driver.findElements(By.xpath(".//input[#type='radio']"));
System.out.println(howold.size());
howold.get(4).click();
System.out.println(driver.getTitle());
System.out.println(driver.getCurrentUrl());'
driver.getTitle() : will give you Title of web page which Webdriver has currently focus on ! same goes with driver.getCurrentUrl()
Your code :
List<WebElement> howold=driver.findElements(By.xpath(".//input[#type='radio']"));
System.out.println(howold.size());
howold.get(4).click();
System.out.println(driver.getTitle());
System.out.println(driver.getCurrentUrl());
howold is a reference of a list that contains WebElements.
howold.get(4).click(); // may open a page in different tab, for that you need to switch the focus of webdriver to that required page.
Code you can use is :
ArrayList<String> tabs = new ArrayList<String>(driver.getWindowHandles());
driver.switchTo().window(tabs.get(1));
// performs some operations on Invoice page like :
System.out.println(driver.getTitle());
System.out.println(driver.getCurrentUrl());
Then again you have to switch back to page from where you went to Invoice Page, if you want that.
driver.close();
driver.switchTo.windows(tabs.get(0));
Does anyone know how to click log out button? I tried using driver.findElement(By.linkText("Log out"));
But it returned an error saying element is not found. Is it because the list is dynamically generated?
You should try using WebDriverWait to wait until elementToBeClickable it works for me as below :-
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement accountSettings = wait.until(ExpectedConditions.elementToBeClickable(By.linkText("Account Settings")));
accountSettings.click() //this will click on setting link to open menu
WebElement logOut = wait.until(ExpectedConditions.elementToBeClickable(By.linkText("Log Out")));
logOut.click() // this will click on logout link
Hope it helps...:)
I assume, after click on arrow button, Log Out button appers in ur code. So to click on that log Out button, use the below portion as cssSelector:
a[data-gt*='menu_logout']>span>span._54nh
driver.findElement(By.cssSelector("a[data-gt*='menu_logout']>span>span._54nh"));
I'm new to automation testing. I'm using twitter as a test website to learn selenium.
I want to create a new tweet and for doing that I'm using the below scenario. I'm able to open the 'New tweet' pop-up but still can't enter text in its text box. Please help.
Pre-Requisite: Login into Twitter
Click on the button (for creating new tweet) on top right side of screen.
The pop up opens up
Click on the text box present on pop-up to enter text.
The cursor gets highlighted in text box and save button enables
Enter some text
Click on Save button
For me it's working till point 2 and after that it doesn't enter text in textbox
See my code below:
String popUpHandle = driver.getWindowHandle();
driver.switchTo().window(popUpHandle);
driver.findElement(By.id("global-new-tweet-button")).click();
driver.findElement(By.xpath("(//button[#type='button'])[123]")).click();
driver.findElement(By.xpath("(//button[#type='button'])[123]")).sendKeys("test tweet");
driver.findElement(By.xpath("//button[#type='button'])[106]")).click();
#user3241182
I just tested this myself. You do not need to use any window handling.
Code Below is a provided solution and works great.
WebDriver driver = new FirefoxDriver();
driver.get("https://twitter.com/");
WebElement username = driver.findElement(By.id("signin-email"));
username.clear();
username.sendKeys("your_twitter_handle");
WebElement pass = driver.findElement(By.id("signin-password"));
pass.clear();
pass.sendKeys("your_twitter_passwd");
WebElement signInButton = driver.findElement(By.xpath("/html/body/div/div[2]/div/div[2]/div[2]/div[2]/form/table/tbody/tr/td[2]/button"));
signInButton.click();
WebElement tweetButton = driver.findElement(By.id("global-new-tweet-button"));
tweetButton.click();
WebElement tweetBox = driver.findElement(By.id("tweet-box-global"));
tweetBox.clear();
tweetBox.sendKeys("This is an automated tweet!");
WebElement tweetSubmit = driver.findElement(By.xpath("/html/body/div[9]/div[2]/div[2]/div[2]/form/div[2]/div[2]/button"));
tweetSubmit.click();