I am trying to read object on a new tab but driver focus is not shifting on that. Here is a scenario:
1. Open Gmail.
2. Click on "term" link below Gmail site. New Tab will get open.
3. Read any object from new tab of term link.
Unable to do step 3. Here is code
WebDriver driver= new ChromeDriver();
driver.get("http://gmail.com");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
Thread.sleep(2000);
String Mainwindow = driver.getWindowHandle();
boolean Term = driver.findElement(By.xpath("//*[#id='footer-list']/li[3]/a")).isDisplayed();
if(Term){
driver.findElement(By.xpath("//*[#id='footer-list']/li[3]/a")).click();
}
else{
driver.findElement(By.xpath("html/body/footer/div/div[5]/div[1]/a[4]")).click();
// this is written for IE and Mozila
}
Set<String> set = driver.getWindowHandles();
System.out.println(set.size());
Iterator<String> it = set.iterator();
String main =it.next();
String maintab =it.next();
System.out.println(maintab);
driver.switchTo().window(maintab);
Thread.sleep(10000);
driver.findElement(By.xpath("//*[#id='maia-nav-y']/ul/li[1]")).sendKeys(Keys.PAGE_DOWN);
Thread.sleep(3000);
driver.findElement(By.xpath(".//*[#id='maia-nav-y']/ul/li[1]")).sendKeys(Keys.PAGE_UP);
Thread.sleep(3000);
driver.findElement(By.xpath(".//*[#id='maia-nav-y']/ul/li[1]")).sendKeys(Keys.BACK_SPACE);
driver.switchTo().window(main);
}}
Use Array List instead of Set. As Set stores the elements by using a mechanism called hashing. So String 'main' or 'mainTab' may not have right window handle to switch.
May be you need to try this:
driver.switchTo().window("windowName");
Webdriver is successfully able to switch to the new tab. However, you are trying to invoke sendKeys API on a control which is not an input element.
If you just want to scroll page down and up in the new tab, you can do that using JavascriptExecutor, as shown in the below code:
WebDriver driver= new ChromeDriver();
driver.get("http://gmail.com");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
Thread.sleep(2000);
String Mainwindow = driver.getWindowHandle();
boolean Term = driver.findElement(By.xpath("//*[#id='footer-list']/li[3]/a")).isDisplayed();
if(Term){
driver.findElement(By.xpath("//*[#id='footer-list']/li[3]/a")).click();
}
else{
driver.findElement(By.xpath("html/body/footer/div/div[5]/div[1]/a[4]")).click();
// this is written for IE and Mozila
}
Set<String> set = driver.getWindowHandles();
System.out.println(set.size());
Iterator<String> it = set.iterator();
String main =it.next();
String maintab =it.next();
System.out.println(maintab);
driver.switchTo().window(maintab);
Thread.sleep(10000);
//Using JavaScriptExecutor
//Scroll page down
JavascriptExecutor jscriptExec = (JavascriptExecutor)driver;
jscriptExec.executeScript("window.scrollBy(0,250)", "");
//Scroll page up
jscriptExec.executeScript("window.scrollBy(0,-250)", "");
//driver.findElement(By.xpath("//*[#id='maia-nav-y']/ul/li[1]")).sendKeys(Keys.PAGE_DOWN);
//Thread.sleep(3000);
//driver.findElement(By.xpath(".//*[#id='maia-nav-y']/ul/li[1]")).sendKeys(Keys.PAGE_UP);
//Thread.sleep(3000);
//driver.findElement(By.xpath(".//*[#id='maia-nav-y']/ul/li[1]")).sendKeys(Keys.BACK_SPACE);
driver.switchTo().window(main);
driver.quit();
Related
I am trying to take screen shot of the web page showing the tool tip text, but I can only see WebElement text being highlighted after mouse hover, but tool tip is not even shown up on the web page during script execution.
Here is the piece of code, that I am trying to execute. Need help to figure out what am I missing..
public static void main(String[] args) throws InterruptedException, IOException, AWTException
{
String URL = "https://www.rediff.com";
System.setProperty("webdriver.chrome.driver", "C:\\Users\\bhara\\eclipse-workspace\\Drivers\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get(URL);
driver.manage().window().maximize();
driver.manage().timeouts().pageLoadTimeout(40, TimeUnit.SECONDS);
String WebElementXpath = "//*[#class='mailicon']";
String toolTipText ;
WebElement ToolTipElement = driver.findElement(By.xpath(WebElementXpath));
Actions act1 = new Actions(driver);
act1.moveToElement(ToolTipElement).build().perform();
Robot robot = new Robot();
Point point;
point = ToolTipElement.getLocation();
int x = point.getX();
int y = point.getY();
robot.mouseMove(x,y);
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File("screenshot.png"));
System.out.println( ToolTipElement.getAttribute("title"));
}
I don't see any such issues in your code block as such. However if you observe the HTML DOM of the WebElement:
The text Lightning fast free email is the title attribute but not the tooltip as such. Hence through using Actions class to Mouse Hover over the element won't bring up the popup. However, I made some small changes and executed you code to get the same result as follows:
Code Block:
ChromeOptions options = new ChromeOptions();
options.addArguments("start-maximized");
options.setExperimentalOption("excludeSwitches", Collections.singletonList("enable-automation"));
options.setExperimentalOption("useAutomationExtension", false);
WebDriver driver = new ChromeDriver(options);
driver.get("https://www.rediff.com/");
new Actions(driver).moveToElement(new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//a[#class='mailicon']")))).build().perform();
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File(".\\Screenshots\\Rediffmail.png"));
System.out.println(driver.findElement(By.xpath("//a[#class='mailicon']")).getAttribute("title"));
driver.quit();
Console Output:
Lightning fast free email
Screenshot:
I am trying to open a new browser tab through Webdriver in Selenium. However the new tab opened is blank. I am trying to send a website link to the Webdriver object however the link is still not opening.
Here is my code:
// Assuming path is already set
WebDriver driver = new ChromeDriver();
String baseURL = "https://www.flipkart.com/";
driver.get(baseURL);
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
Robot r = new Robot();
r.keyPress(KeyEvent.VK_CONTROL);
r.keyPress(KeyEvent.VK_T);
r.keyPress(KeyEvent.VK_CONTROL);
r.keyPress(KeyEvent.VK_T);
Set<String> handles = driver.getWindowHandles();
Iterator itr = handles.iterator();
String parent_window = (String) itr.next();
System.out.println(parent_window + "....." + driver.getTitle());
String child_window = (String) itr.next();
driver.switchTo().window(child_window);
driver.navigate().to("https://www.flipkart.com/");
Try this.
ArrayList<String> tabs = new ArrayList<String> (driver.getWindowHandles());
driver.switchTo().window(tabs.get(1)); //switches to new tab
driver.get("https://www.facebook.com");
driver.switchTo().window(tabs.get(0)); // switch back to main screen
driver.get("https://www.news.google.com");
I have this code show below:
driver.findElement(By.id("submit")).sendKeys(Keys.ENTER);
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
driver.findElement(By.id("search-trigger")).sendKeys(Keys.ENTER);
driver.findElement(By.id("search-trigger")).sendKeys("Shampoo");
driver.findElement(By.id("search-trigger")).sendKeys(Keys.ENTER);
I want to search for a product and search option is before and after login page but here i want to do it after login page
I have used
driver.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS);
but it doesn't work and i cannot use
WebElement “”= driver.findElement(By.id(""));
as the search option is on both pages
and URL remains same after login also
Per my understanding, in short, you want to ensure user has logged in before searching for a product. However u will not be able to use WebElement “”= driver.findElement(By.id(""));
Did you try explicit wait for the presence of username/logout text...etc
WebElement element = wait.until(ExpectedConditions.elementIsVisible(By.id(>someid>)));
Below worked for me, tried searching Samsung after login in flipkart:
public static void f() throws InterruptedException
{
By signin = By.xpath(".//*[#id='container']/div/header/div[1]/div[1]/div/ul/li[9]/a");
Thread.sleep(2000);
By mobile = By.xpath("html/body/div[2]/div/div/div/div/div[2]/div/form/div[1]/input");
By password = By.xpath("html/body/div[2]/div/div/div/div/div[2]/div/form/div[2]/input");
By login = By.xpath("html/body/div[2]/div/div/div/div/div[2]/div/form/div[3]/button");
By myaccount = By.xpath(".//*[#id='container']/div/header/div[1]/div[1]/div/ul/li[8]/a");
By search = By.xpath(".//*[#id='container']/div/header/div[1]/div[2]/div/div/div[2]/form/div/div[1]/div/input");
//geko driver for firefox
System.setProperty("webdriver.gecko.driver", "path of geckodriver.exe");
System.setProperty("webdriver.chrome.driver", "path of chromedriver.exe");
driver = new ChromeDriver();
driver.get("https://www.flipkart.com/");
driver.findElement(signin).click();;
driver.findElement(mobile).sendKeys("xxxxxxxxx");
driver.findElement(password).sendKeys("xxxxxxxx");
driver.findElement(login).click();
WebDriverWait wait = new WebDriverWait (driver, 10);
wait.until(ExpectedConditions.elementToBeClickable(myaccount));
driver.findElement(search).sendKeys("samsung");
}
need to mouse hover on menu first >menu then >submenu1 then >submenu1 then > submenu1
>
System.setProperty("webdriver.ie.driver","E:\workplace\AutomationTemplateWrking1\src\resource\IEDriverServer.exe");
WebDriver driver = new InternetExplorerDriver();
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
driver.get("example.com/");
driver.findElement(By.id("user_login")).sendKeys("pmtest");
driver.findElement(By.id("user_password")).sendKeys("#123");
driver.findElement(By.className("login-button")).click();
driver.manage().window().maximize();
WebElement ele1=driver.findElement(By.id("menu"));
WebElement ele2=driver.findElement(By.xpath("//div[#class='head-fixed']/div/div/div/ul/li/ul/li/a[text()='Administration']"));
WebElement ele3=driver.findElement(By.xpath("(//a[text()='Charge Codes'])[1]"));
WebElement ele4=driver.findElement(By.xpath("(//a[text()='Charge Codes'])[2]"));
Actions a1 = new Actions(driver);
a1.moveToElement(ele1).build().perform();
Thread.sleep(1000);
a1.moveToElement(ele2).build().perform();
Thread.sleep(1000);
a1.moveToElement(ele3).build().perform();
Thread.sleep(1000);
ele4.click();
solution:
For me it worked with 64bit version of IEDriverServer. I added the property requireWindowFocus with "true" value: But Not able to perform on mousehovers error:stacktrace information is not available
Below Is the site page
I'm trying to click on Continue to Netbanking button. But I am unable to do that. I have used xpath but its not working. Here is xpath which I've tried :
driver.findElement(By.xpath(".//*[#id='wrapper']/div[6]/a/img")).click();
Steps:
Open URl http://www.hdfcbank.com
Click on Login button on website. New popup will get open.
Click on "Continue on Netbanking". THIS IS NOT WORKING
Here is code:
driver.findElement(By.id("loginsubmit")).click();
Thread.sleep(3000);
Set<String> set = driver.getWindowHandles();
Iterator<String> it = set.iterator();
System.out.println(set.size());
for( String windowTab : set){
if(!windowTab.equalsIgnoreCase(MainWindow)){
driver.switchTo().window(it.next());
driver.manage().window().maximize();
String Wdinw2 = driver.getWindowHandle();
Thread.sleep(10000);
System.out.println(driver.getTitle());
driver.findElement(By.xpath(".//*[#id='wrapper']/div[6]/a/img")).click();
break;
}
}
Console :
Exception in thread "main" org.openqa.selenium.NoSuchElementException:
no such element: Unable to locate element:
{"method":"xpath","selector":".//*[#id='wrapper']/div[6]/a/img"}
Try to use below code and let me know the result:
String winHandleBefore = driver.getWindowHandle();
for(String winHandle : driver.getWindowHandles()){
driver.switchTo().window(winHandle);}
WebDriverWait wait = new WebDriverWait(driver, 15);
wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("img[alt='continue']"))).click();
To switch back to main window (if you need):
driver.switchTo().window(winHandleBefore);