I'm trying to automate a functionality where I have to open a new tab and start to work on this page, but my code still looking the WebElements on the last Tab. I try to switch the tabs with the follow code but its not working.
public void DataManager() throws InterruptedException {
ArrayList<String> tabs2 = new ArrayList<String> (driver.getWindowHandles());
driver.switchTo().window(tabs2.get(0));
driver.close();
driver.switchTo().window(tabs2.get(1));
WebElement tes = driver.findElement(By.xpath("//*[#id=\"lui-popover-3\"]/div/ng-transclude/ul/li[2]/button[1]/span[2]"));
tes.click();
}
What's going on, and how can I fix this?
1st time, You have focus driver on 1st tab which is on 0 index and you have close it. After you close it, Your 1 index become 0 index but you have invoke it on 1 index.
You may Refer sample example:
ArrayList<String> tabs= new ArrayList<String>(driver.getWindowHandles());
js.executeScript("window.open()");
driver.switchTo().window(tabs.get(1));
Here, by this lines New tab will be open by keeping current Tab. And driver will invoke for Tab-1.
I found the solution, I had a problem to find Web Elements in my page, so when I used the following code to change the selenium focus:
ArrayList<String> tabs2 = new ArrayList<String> (driver.getWindowHandles());
driver.switchTo().window(tabs2.get(1));
And I searched the Web Element using press Tab I found it.
Thanks!
Related
I'm currently working on Selenium tests for a web platform, and I've noticed that the test have problems finding elements on a certain page which will make the test fail. The problem occurs once the test have pressed a button, which opens up a new tab, so my question is whether or not the reason for this happening is due to the webdriver being set to the first tab in the webbrowser or is it something else?
PS.The test has no problem finding elements if the driver is set to start on the second page.
You have to switch to a new tab, because driver would find only the actual tab WebElements.
Switch to a new tab by using:
ArrayList<String> tabs = new ArrayList<>(driver.getWindowHandles());
driver.close(); //to close actual tab
driver.switchTo().window(tabs.get(1)); //then switch to new tab
Your suggestion sounds correct, to work with the new tab you have to switch driver to it.
This can be simply done like this:
public void switchToNewWindow(){
wait(500);
List<String> tabs = new ArrayList<>(driver.getWindowHandles());
driver.switchTo().window(tabs.get(tabs.size()-1));
}
to switch back to the first window you can use this:
public void switchToFirstWindow(){
List<String> tabs = new ArrayList<>(driver.getWindowHandles());
driver.switchTo().window(tabs.get(0));
}
In case there are more than 2 tabs you can switch to any tab as shown above switching to tab with the corresponding index.
I am trying to type a string in capital letters (using KeyDown) into amazon.in website search bar using sendKeys() but I don't see the text on the search bar. I don't see any error. I use debug mode then I also I can find any error.
Question:
How can I resolve this?
How can I debug it myself and find issue?
For debug I put a breakpoint on below line and then use step over option to run each line.
mouseAction.moveToElement(elementLocation).build().perform();
public class MouseActions {
public static void main (String [] args){
System.setProperty ("webdriver.chrome.driver","C:\\Users\\tokci\\Documents\\Selenium\\drivers\\chromedriver_win32\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("http://www.amazon.in/");
Actions mouseAction = new Actions(driver);
//this mouse action works
WebElement elementLocation = driver.findElement(By.xpath("//a[#id='nav-link-yourAccount']"));
mouseAction.moveToElement(elementLocation).build().perform();
//below code does not work
WebElement keysLocation = driver.findElement(By.xpath("//input[#id='twotabsearchtextbox']"));
mouseAction.keyDown(Keys.SHIFT).moveToElement(keysLocation).sendKeys("shoes").build().perform();
}
}
keysLocation is a input element here you can use .sendKeys() without using mouseAction as below and it works :-
keysLocation.sendKeys(Keys.SHIFT, "shoes");
Hope it will help you..:)
You can try using click before you use sendKeys. moveToElement function just moves the cursor over and .click() function will ensure that the element has been selected
Try this
WebElement keysLocation = driver.findElement(By.xpath("//input[#id='twotabsearchtextbox']"));
mouseAction.keyDown(Keys.SHIFT).moveToElement(keysLocation).click().sendKeys("shoes").build().perform();
firefox driver does not respond to keydown command.
so my work around was not use actions class. Here's example of code:
driver.findElement(By.name("q")).sendKeys(Keys.SHIFT + "big");
I have tried the following code but it is throwing the exception (ElementNotVisibleException)
FirefoxDriver dr = new FirefoxDriver();
dr.get("http://54.169.235.143/book.html?v=0.03");
System.out.println("First Testcase");
System.out.println(dr.findElement(By.id("user_name")));
dr.findElement(By.id("user_name"));
dr.findElement(By.id("user_name")).click();
dr.findElement(By.id("user_name")).getAttribute("user_name");
dr.findElement(By.id("user_name")).clear();
dr.findElement(By.id("user_name")).sendKeys("student100");
What am I doing wrong and how to fix it?
Actually your page taking time to load so web driver need wait until element gets visible , Below code will solve your issue :
WebDriverWait wait= new WebDriverWait(dr,30);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("user_name")));
dr.findElement(By.id("user_name")).clear();
dr.findElement(By.id("user_name")).sendKeys("test");
wait= new WebDriverWait(dr,30);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("pass_word")));
dr.findElement(By.id("pass_word")).clear();
dr.findElement(By.id("pass_word")).sendKeys("test");
I have just added wait for elements.
n software testing services this can be achieved by many ways some of the options are displayed above remaining are as follow.
Using java script
driver.executeScript("document.getElementByXpath('element').setAttribute('value', 'abc')");
Using action class Actions actions = new Actions(driver);
actions.click(driver.findElement(element) .keyDown(Keys.CONTROL).sendKeys("a").keyUp(Keys.CONTROL).sendKeys(Keys.BACK_SPACE).build().perform());
I have an element on a web page that only becomes visible after clicking its parent element. So after clicking a demo in a list of demo's, a row of icons which represent actions for the selected demo is revealed. The following code works fine with both webdriver and chromedriver:
demo.click(); //click demo
waitForElementIsDisplayed(demoReservation_btn); //wait until reservation icon is displayed
demoReservation_btn.click(); //click icon
Originally i was getting a StaleElementReferenceException and i attempted to fix this by having a try/catch block within a while loop that would continue looping until the icon was clicked. This caused IEDriverServer to crash after a couple of loops.
I have also tried wrapping it up in an Action like so:
Action action = new Action(driver);
action.click(demo).click(demoReservation_btn).build().perform()
This results in a NoSuchElementException.
I know there are some problems mentioned in the documentation about browser focus and hovering over elements, but i dont believe this is the problem. I have tried a couple of other things like adding moverToElement to the action, hovering over the element but have had no success with these. I believe one possible solution is to use a javascript executor, but i would like to avoid this approach if possible, any other suggestions?
EDIT
IEDriverServer setup:
File file = new File("IEDriverServer.exe");
System.setProperty("webdriver.ie.driver", file.getAbsolutePath());
driver = new InternetExplorerDriver();
driver.manage().window().maximize();
return driver;
Try disabling Native events of IE
DesiredCapabilities cap = DesiredCapabilities.internetExplorer();
cap.setCapability("nativeEvents",false);
driver = new InternetExplorerDriver(cap);
I had better result using that in C# version. Read this to learn why you may need to do this.
The following code tests an autocomlete box of a webpage:
public class Test {
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver","chromedriver\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("http://www..............com");
driver.switchTo().frame("mainFrame");
WebDriverWait waitst = new WebDriverWait(driver, 120);
waitst.until(ExpectedConditions.visibilityOfElementLocated(By.name("sourceTitle")));
WebElement sourceTitle = driver.findElement(By.name("sourceTitle"));
WebElement small = driver.findElement(By.cssSelector("li#nameExampleSection label + small"));
sourceTitle.sendKeys("Times");
Thread.sleep(5000);
Actions actions = new Actions(driver);
actions.click(small).perform();
}
}
Why doesn't the autosuggest box load? IMPORTANT: try to type in "..........." manually ... the autocomplete box will load perfectly fine!!! So, why does not cssSelector work, why doesn't it load the autocomplete box?
How come an automated input does not allow for autocomplete options BUT a manual input does???
PS: I also tried fireEvent, sendKeys but nothing works.
I tried your code, it does exactly what the manual walkthough does. "Associated Press, The" returns only a "No Match, please try sources". In your code you then try to click on the next form list item, not the results popup. The autosuggest popup is dynamically populated at the top of your html page positioned under the input form. The following code does select the first option on your drop down.
#Test
public void test() throws InterruptedException {
WebDriver driver = new ChromeDriver();
driver.get("http://www.lexisnexis.com/hottopics/lnacademic/?verb=sf&sfi=AC00NBGenSrch");
driver.switchTo().frame("mainFrame");
WebDriverWait waitst = new WebDriverWait(driver, 0);
waitst.until(ExpectedConditions.visibilityOfElementLocated(By.name("sourceTitle")));
WebElement sourceTitle = driver.findElement(By.name("sourceTitle"));
sourceTitle.sendKeys("Times");
Thread.sleep(5000);
WebElement firstItem = driver.findElement(By.xpath("//*[#class='auto_suggest']/*[#class='title_item']"));
firstItem.click();
}
I found a workaround about this. My problem was:
Selenium inputted 'Mandaluyong' to an auto-suggest location field
The auto-suggest field appears together with the matched option
Then selenium left the auto-suggest drop-down open not selecting the matched option.
What I did was:
driver.findElement(By.name("fromLocation")).sendKeys("Mandaluyong");
driver.findElement(By.name("fromLocation")).sendKeys(Keys.TAB);
This is because on a manual test, when I try to press TAB key, two things were done by the system:
Picks the matched option from the auto-suggest drop-down
Closes the auto-suggest drop-down
Hope this helps.
// allow autopuplation value to fill the text box.
// wait for 6 sec make sure auto value is inserted
thread.sleep(6000L);
// clear auto filled value
driver.findElement(By.name("txtBox")).clear();
driver.findElement(By.name("txtBox")).sendKeys("value");
Try first clicking on the Input textbox.
This will trigger the auto populating dropdown box and then enter the required value using sendKeys
Either you can use tab or enter to Escape the scenario or,it is not possible by selenium if it is mandatory fields. (sad)