selenium Webdriver Page Navigation - selenium

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));

Related

How to click on save button in chrome print privew page using selenium Java

I am currently looking for a solution to click on Sava button in chrome print preview window with selenium Java.
Is there any way we can handel chrome print preview page?
I have tried with the Robot class, but it seems not reliable/stable for my application.
Could you please someone help me to achieve this with selenium Java.
I fail to see any "Save" button on print preview page for Chrome browser
Here is how you can click "Cancel" button, I believe you will be able to amend the code to match your requirements:
First of all make sure to wait for the preview page to be available using Explicit Wait
Change the context to the print preview window via WebDriver.switchTo() function
All the elements at the print preview page are hidden in ShadowDom therefore you will need to:
Locate the element which is the first parent of the element you're looking for
Get its ShadowRoot property and cast the result to a WebElement
Use the WebElement.findElement() function to locate the next parent
repeat above steps until you reach the desired button
Example code just in case:
new WebDriverWait(driver, 10).until(ExpectedConditions.numberOfWindowsToBe(2));
driver.switchTo().window(driver.getWindowHandles().stream().skip(1).findFirst().get());
WebElement printPreviewApp = driver.findElement(By.tagName("print-preview-app"));
WebElement printPreviewAppConten = expandShadowRoot(printPreviewApp, driver);
WebElement printPreviewSidebar = printPreviewAppConten.findElement(By.tagName("print-preview-sidebar"));
WebElement printPreviewSidebarContent = expandShadowRoot(printPreviewSidebar, driver);
WebElement printPreviewHeader = printPreviewSidebarContent.findElement(By.tagName("print-preview-header"));
WebElement printPreviewHeaderContent = expandShadowRoot(printPreviewHeader, driver);
printPreviewHeaderContent.findElements(By.tagName("paper-button")).get(1).click();
where expandShadowRoot function looks like:
private WebElement expandShadowRoot(WebElement parent, WebDriver driver) {
return (WebElement) ((JavascriptExecutor) driver).executeScript("return arguments[0].shadowRoot", parent);
}
Save button will not work because page is not part of webpage. selenium only support web based application.
But you can use SIKULI to handle above scenario.

Trying to hover on a menu and click on a link in the sub-menu but no luck

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 :

Getting time out exception when trying to identify the webelemnt

Below is the Code:
WebElement Username=d1.findElement(By.xpath("//*[#id='username']"));
Username.sendKeys("aadmin");
WebElement Password=d1.findElement(By.xpath("//*[#id='login_form']/tbody/tr/td/table/tbody/tr[12]/td[2]/input"));
Password.sendKeys("admin");
WebElement signin=d1.findElement(By.xpath("//*[#id='submit_']"));
signin.click();
System.out.println("User admin has logged in "+ d1.getTitle());
w1.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[#id='spaces-menu']/ul/li[1]"))).click();
System.out.println("User Admin clicks on Record button");
//Thread.sleep(5000);
d1.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
//Switching the control
int size2=d1.findElements(By.tagName("iframe")).size();
System.out.println("iframe size is---" + size2);
WebDriverWait w2= new WebDriverWait (d1, 15);
//w2.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(0));
w2.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.tagName("iframe")));
System.out.println("Page title is "+d1.getTitle());
//d1.switchTo().frame("iframe-page-container");
//d1.switchTo().frame(d1.findElement(By.tagName("iframe")));
WebDriverWait w3= new WebDriverWait (d1,30);
WebElement New=w3.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[#id='capTypePopup']")));
//WebElement New=w3.until(ExpectedConditions.visibilityOfElementLocated(By.className("menu-middle-normal-button")));
//WebElement New=w3.until(ExpectedConditions.visibilityOfElementLocated(By.id("menuButtonContain-6")));
//New.click();
//d1.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
//JavascriptExecutor js = (JavascriptExecutor) d1;
//WebElement element = d1.findElement(By.id("menuButtonContain-6"));
//js.executeScript("arguments[0].setAttribute('type', '')",element);
//System.out.println(d1.findElement(By.id("menuButtonContain-6")).getAttribute("value"));
//Actions a1= new Actions(d1);
//a1.moveToElement(New).click(New).build().perform();
//d1.findElement(By.xpath("//*[#id='tr_menubar']/td"));
//d1.findElement(By.xpath("//*[#id='capTypePopup']")).click();
//w1.until(ExpectedConditions.elementToBeClickable(By.className("portlet-menu-item"))).click();
//w2.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[#id='capTypePopup']/font"))).click();
System.out.println("Click on the new button");
Below is the DOM of the page, i am trying to click on New button which inside the iframe.
enter image description here
The xpath:
//div[#id='capTypePopup']
you used will find more than one elements,selenium will use the first find element, which may be not the one your expected.
From your screenshot, i noticed there are already two, I guess there should be another one ahead of the div your desired and it's not visible.
Use Dev tool to test the //div[#id='capTypePopup'], and confirm the first found div is visible or not.
If not visible, use more strict xpath which can find the div you desired. After that your problem should gone.

Selenium WebDriver Log Out from Facebook

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"));

Unable to identify greeting link after logging into the flipkart application

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..:)