Select se = new Select(driver.findElement(By.xpath(".//*[#id='33629']/div/div[1]/div[2]/div[1]/select")));
se.selectByIndex(7);
driver.findElement(By.xpath(".//*[#id='33629']/div/div[1]/div[2]/div[1]/select/option[8]")).click();
Above code doesn't work,please help
Error returned:
Exception in thread "main" org.openqa.selenium.NoSuchWindowException: no such window: target window already closed from unknown error: web view not found
org.openqa.selenium.NoSuchWindowException: no such window
Means the browser is close when you are trying to interact with it. Remove driver.close() from your code and put it only after you have finished all you interactions with the browser.
Edit
If you need to return to parent window after closing child window use driver.switchTo() again
// get parent window ID
String parentHandle = driver.getWindowHandle();
// switch to the new window
for (String handle : driver.getWindowHandles()) {
if (!handle.equals(parentHandle))
{
driver.switchTo().window(handle);
}
}
//do something with the new window
// switch back to the old window
driver.close();
driver.switchTo().window(parentHandle);
windowIdbefore = driver.getWindowHandle();
System.out.println(windowIdbefore);
Set<String> windowid = driver.getWindowHandles();
for (String string : windowid) {
System.out.println(string);
driver.switchTo().window(string);
// enter code here
}
WebDriver driver=new FirefoxDriver();
Select s=new Select(driver.findElement(By.xpath("xpathExpression")));
s.selectByVisibleText("text");
s.selectByValue("value");
s.selectByIndex(1);
as i see here the dropdown box is present in div tag. i think with your code dropdown has been located but you are not able to select the value present in dropdown. Then follow below code
WebDriverWait wait = new WebDriverWait(d, 10);
Actions builder = new Actions(d);
WebElement selectvalue = wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("your drop down xpath value")));
builder.mouse.mouseMove(((Locatable)selectvalue).coordinates);
selectvalue.click();
WebElement option = wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("locator value of dropdown value(your dropdown value)")));
builder.mouse.mouseMove(((Locatable)option).coordinates);
option.click();
System.out.println("dropdown value slected...");
Related
I am trying to change my driver's focus to the new window that pops up as a result of clicking a link. The only problem is that both the parent window and the pop-up window have the same handle - for example - the title of both is " <Company Name - Abbreviation 1>
How would I use Selenium JAVA to get focus on the pop-up window a different way than what I have used?
`
String originalWindow = driver.getWindowHandle();
assert driver.getWindowHandles().size() == 1; driver.findElement(By.xpath("xpath goes here")).click(); // Training
// Loop through until we find a new window handle
for (String windowHandle : driver.getWindowHandles()) {
if (!originalWindow.contentEquals(windowHandle)) {
driver.switchTo().window(windowHandle);
driver.manage().window().maximize();
}
}
`
Thanks, Everyone!
`
String originalWindow = driver.getWindowHandle();
assert driver.getWindowHandles().size() == 1;
driver.findElement(By.xpath("xpath goes here")).click(); // Training
Thread.sleep(1000);
// Loop through until we find a new window handle
for (String windowHandle : driver.getWindowHandles()) {
if (!originalWindow.contentEquals(windowHandle)) {
driver.switchTo().window(windowHandle);
driver.manage().window().maximize();
}
}
`
I tried the above code, but both windows sadly have the same title. I want to switch focus to the new window so I can then interact with the new one.
New to automation and could use some help here.
I am using Selenium Webdriver and Java on this website - Webdriver University and so far this code has been throwing No Such Element exception at "element.click()" step (i.e., doesn't find the element on page):
driver.manage().window().maximize();
driver.get("http://webdriveruniversity.com");
Thread.sleep(3000);
// Follow the link to another page
WebElement link = driver.findElementByXPath("(//div[#class=\"section-title\"])[6]");
link.click();
Thread.sleep(3000);
// Click on the element
WebElement element = driver.findElementByXPath("(//button[#class='accordion'])[1]");
element.click();
However, when I go to the linked page directly, it finds the element just fine
driver.manage().window().maximize();
driver.get("http://webdriveruniversity.com/Accordion/index.html");
// Click on the element
WebElement element = driver.findElementByXPath("(//button[#class='accordion'])[1]");
element.click();
I've used wait for element visibility and Thread sleeps, same results.
Any idea what could be the issue here?
Did you notice that when you click the link, page is opened in new tab? That is your issue.
You need to switch to new tab.
ArrayList<String> tabs = new ArrayList<String> (driver.getWindowHandles());
driver.switchTo().window(tabs.get(1)); //here you are switch to second tab
Hope the below code will solve your issues.
Used the getWindowHandles() to capture handle of newly opened tab and switch to the
tab
// Follow the link to another page
WebElement link = driver.findElement(By.xpath("(//div[#class=\"section-title\"][6]"));
link.click();
Set<String> allWindow = driver.getWindowHandles();
Iterator<String> itr = allWindow.iterator();
while (itr.hasNext()) {
String wind = itr.next().toString();
driver.switchTo().window(wind);
}
Thread.sleep(3000);
// Click on the element
WebElement element = driver.findElement(By.xpath("(//button[#class='accordion'][1]"));
element.click();
Thread.sleep(3000);
driver.close();
}
I am having problem with locating WebElement using different locators. In the below html tag I tried locating the "write a review" WebElement with different locators like linkText,xpath,classname but still getting NoSuchElementException
-->url https://www.tripadvisor.in/-->search for Club Mahindra-->click on Club Mahindra-->click on write a review.
<a href="/UserReview-g641714-d1156207-Club_Mahindra_Madikeri_Coorg-
Madikeri_Kodagu_Coorg_Karnataka.html" target="_blank" class="ui_button
primary">Write a review</a>
Locators used
By.xpath("//*[#id="component_12"]/div/div[2]/div/div[2]/div/div[1]/a")
By.xpath("//a[#href='/UserReview-g641714-d1156207-
Club_Mahindra_Madikeri_Coorg-Madikeri_Kodagu_Coorg_Karnataka.html']")
By.className("ui_button primary")
By.linkText("Write a review")
I am really confused. What am I doing wrong?
I have tired to analyse and implement the same. Below are my findings:
-> Application waiting time is more as there are lots of dynamic loads applicable for the page.
-> Proper waits needs to be implemented
-> Check whether all the pages are getting opened in the same tab or clicking on each link is redirecting to new tabs, if so then we have to switch to that particular window.
-> Below code works like a pro for me.
driver.get("https://www.tripadvisor.in/");
WebDriverWait wait = new WebDriverWait(driver, 120);
WebElement ele1 =
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[text()='Where to?']")));
ele1.click();
WebElement ele2= wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[#placeholder='Where to?']")));
ele2.sendKeys("club mahindra, india");
WebElement ele3= wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//span[contains(text(),'Search for ')]")));
ele3.click();
WebElement ele4= wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//span[contains(text(),'Club Mahindra Madikeri, Coorg')]")));
ele4.click(); //this click leads to a new tab
Set<String> winHandles = driver.getWindowHandles();
for(String str : winHandles) {
driver.switchTo().window(str);
}
System.out.println(driver.getTitle());
WebElement ele;
int i=1;
while(true) {
try {
ele = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[text()='Write a review']")));
break;
}catch(Exception e) {
System.out.print(i++);
}
}
System.out.println();
Actions action = new Actions(driver);
action.moveToElement(ele);
ele.click();
System.out.println("Clicked on the 'Write a review button'");
you can try
//a[contains(text(),'Write a review')]
In an specific Env. the workflow is as following:
Click on the "Main Menu"->Drop Down list opens
Click "Build" from drop down->another sub menu opens beside
Click "Edit" then
Now the following given selenium code is correctly executing on Chrome and Firefox but not in IE11.
//Main Menu opens then-->
WebElement build = driver.findElement(By.linkText("Build"));
Actions actions = new Actions(driver);
actions.moveToElement(build);
actions.click();
actions.build().perform();
Thread.sleep(2000);
WebElement edit = driver.findElement(By.linkText("Edit"));
edit.click();
Now problem is as follows:
In IE11, the moveToElement(build) not actually performing. So after clicking the "Main Menu" it stops at that position only. The main menu keeps opening there but not clicking the next option that is "Build"
Had the problem like this one
InternetExplorerOptions options = new InternetExplorerOptions();
options.EnablePersistentHover = false;
IWebDriver driver = new InternetExplorerDriver(options);
The key is:
options.EnablePersistentHover = false;
Regarding the problem with holding on the menu item to display child menu:
Seems, you've been using this approach:
WebElement edit = driver.findElement(By.linkText("Edit"));
edit.click();
Try the same. For example:
WebElement build = driver.findElement(By.linkText("Build"));
Thread.sleep(1000);
build.click();
Thread.sleep(1000);
WebElement edit = driver.findElement(By.linkText("Edit"));
edit.click();
After try to remove Thread::sleep rows. It is like a hardcode and you may use this approach:
WebDriverWait wait = new WebDriverWait(driver, timeoutInSeconds);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.linkText("Edit"));
I am trying to switch to another pop-up window,but is giving some sort of error. I am not able to figure out why the code in not working for switching the window
Error it is giving is
java.lang.NullPointerException: null value in entry: name=null
Here is the code....
String parentWindowHandle = driver.getWindowHandle();
System.out.println("parentWindowHandle......"+parentWindowHandle);
//click on "register"
Actions act = new Actions(driver);
act.click(driver.findElement(By.id("ctl00_ContentPlaceHolder1_btnRegisterMe")));
act.perform();
sleep(26000);
String popUpWindowHandle=null;
Set<String> openWindowsList = driver.getWindowHandles();
for (String windowHandle : openWindowsList) {
System.out.println("windowHandle......"+windowHandle);
if (!windowHandle.equals(parentWindowHandle))
{popUpWindowHandle = windowHandle;
break;}
}
// Switching control to newly opened window
driver.switchTo().window(popUpWindowHandle);
If you are able to print window handles then try
if (!windowHandle.equalsIgnoreCase(parentWindowHandle)){
popUpWindowHandle = windowHandle;
break;
}
instead of
if(!windowHandle.equals(parentWindowHandle)){}