Getting time out exception when trying to identify the webelemnt - selenium

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.

Related

Unable to switch to iframe using Selenium Webdriver

I am trying to learn the feature drag and drop in the webpage
Link
The Section Books is inside the iframe.
But i am unable to access the iframe I am getting below error
no such element: Unable to locate element:
Below are the Xpath i tried
// WebElement frame =driver.findElement(By.xpath("//div[#id='root']//iframe[#class='st-preview-body']"));
// WebElement frame =driver.findElement(By.tagName("iframe"));
WebElement frame =driver.findElement(By.xpath("//iframe[#src='https://snippet.webixcode.com/snippet.html?0.0.3']"));
driver.switchTo().frame(frame);
I also tried using by.id and also xpath using id and class name
What will be the mistake i am doing? To my knowledge there is only one iframe present
The code for drag and drop. will this work?
WebElement fromdrag=driver.findElement(By.xpath("//span[#class='dhx_tree-list-item__text'][normalize-space()='Lawrence Block']"));
WebElement todrop=driver.findElement(By.id("treeTarget"));
Actions act=new Actions(driver);
act.dragAndDrop(fromdrag, todrop).build().perform();
Its a Nested iframe. We need to switch to those iframes one by one to access Elements from All Books.
The page takes time to load, better apply Explicit waits to find the Elements. And you also need to driver.switchTo().defaultContent(); to come out of the iframe and find Elements outside the iframe
public void iframequestion() {
System.setProperty("webdriver.chrome.driver","C:\\expediaproject\\Chromedriver\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
JavascriptExecutor js = (JavascriptExecutor)driver;
driver.get("https://dhtmlx.com/docs/products/dhtmlxTree/");
WebElement frame1 = driver.findElement(By.xpath("//iframe[#src='https://snippet.dhtmlx.com/3e0b5r57?mode=mobile']"));
js.executeScript("arguments[0].scrollIntoView(true);", frame1);
driver.switchTo().frame(frame1);
WebElement frame2 = driver.findElement(By.xpath("//iframe[#class='st-preview-body']"));
driver.switchTo().frame(frame2);
WebElement frame3 = driver.findElement(By.id("content"));
driver.switchTo().frame(frame3);
WebElement ele1 = driver.findElement(By.xpath("//span[text()='Fiction & Fantasy']"));
System.out.println(ele1.getText());
driver.switchTo().defaultContent();
WebElement ele2 = driver.findElement(By.xpath("//a[text()='View more demos']"));
System.out.println(ele2.getText());
driver.quit();
}
Fiction & Fantasy
View more demos

Unable to find element with xpath for Add -ons text (Correct xpath)

driver.manage().timeouts().pageLoadTimeout(40, TimeUnit.SECONDS);
driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
driver.get("http://www.spicejet.com");
Actions action = new Actions(driver);
action.moveToElement(driver.findElement(By.xpath("//a[contains(text(),'Add-Ons')]")))
.build().perform();
Thread.sleep(3000);
driver.findElement(By.linkText("Hot Meals ")).click();
driver.close();
Unable to locate element with xpath //a[contains(text(),'Add-Ons')
Its it regarding frames?
your xpath can find the correct element when I verify it by manual. I suspect the page not load completely, please add some sleep after dirver.get() for debug purpose.
driver.get("http://www.spicejet.com");
Thread.sleep(10*1000) // sleep 10 seconds to wait page open completely.
Actions action = new Actions(driver);
action.moveToElement(driver.findElement(By.xpath("//a[contains(text(),'Add-Ons')]")))
.build().perform();
Sometimes exact element can't be found using By.linkText()
You just use cssSelector like below:
driver.findElement(By.cssSelector("#header-addons > ul > li:nth-child(5) > a")).click();
Thread.sleep(3000);
instead of the line driver.findElement(By.linkText("Hot Meals ")).click();
You can also use xpath instead like below:
driver.findElement(By.xpath("//*[#id=\"header-addons\"]/ul/li[5]/a")).click();
Thread.sleep(3000);

Selenium webdriver wait element and click

InternetExplorerDriver driver = new InternetExplorerDriver();
driver.get("http://www.wiki-doctor.com");
WebElement element = (new WebDriverWait(driver, 10))
.until(ExpectedConditions.elementToBeClickable(By.id("btnSearch")));
element.click();
it waits for the element btnSearch to be displayed and click on it, however, it doesn' t seems to do anything, do you have any idea why it happens ?
Thanks
This adds United States as a locale, and then waits until a picture of the doctor is displayed.
driver.get("http://www.wiki-doctor.com");
//Enter united states into field
driver.findElement(By.id("field-findDoctor-town")).sendKeys("United States");
WebElement element = (new WebDriverWait(driver, 10))
.until(ExpectedConditions.elementToBeClickable(By.id("btnSearch")));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", element);
WebDriverWait wait = new WebDriverWait(driver,10);
//Wait for picture of doctor
wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("#dvparcticianlist > div.row > div > div.listing-row > div.doc-photo-container")));
System.out.println("Search Successful");
}
Once the page is loaded, first we need to wait for the intended WebElement i.e. the first Search Box to be clickable. Then we will send some text to both the Search Boxes and then invoke click() method on the Search Button as follows :
System.setProperty("webdriver.ie.driver", "C:\\Utility\\BrowserDrivers\\IEDriverServer.exe");
WebDriver driver = new InternetExplorerDriver();
driver.get("http://www.wiki-doctor.com");
WebElement locality = (new WebDriverWait(driver, 5))
.until(ExpectedConditions.elementToBeClickable(By.xpath("//input[#id='field-findDoctor-town']")));
locality.sendKeys("Pune");
driver.findElement(By.xpath("//input[#id='speciality']")).sendKeys("Doctor");
driver.findElement(By.xpath("//button[#id='btnSearch']")).click();
WebDriverWait wait =new WebDriverWait(driver, 90);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("xpath")));
driver.findElement(By.xpath("xpath")).click();

Xpath is not working on MouseOver(CatMenu) in Selenium

There is problem about selenium for all web driver which cannot open mouseover menu.Altough assing to Xpath of element web driver cannot open Mouse Over (CatMenu) and log is that "Must provide a location for a move action".
i want to go n11.com web adress and over on Kitap, Müzik, Film, Oyun and click Kitap but its not working.
Thank you
#Test
public void startWebDriver(){
System.setProperty("webdriver.chrome.driver", "C://chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("http://www.n11.com");
Actions act = new Actions(driver);
act.moveToElement(driver.findElement(By.xpath(".//*[#id='contentMain']/div/nav/ul/li[8]/a"))).perform();
}
Use following code to achieve the same -
System.setProperty("webdriver.chrome.driver", "C://chromedriver.exe");
driver =new ChromeDriver();
driver.get("http://www.n11.com/");
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
WebElement menu = driver.findElement(By.xpath("//li[#class='catMenuItem']/a[#title='Kitap, Müzik, Film, Oyun']"));
WebElement submenu = driver.findElement(By.xpath("//li[#class='subCatMenuItem']/a[#title='Kitap']"));
Actions action = new Actions(driver);
action.moveToElement(menu).moveToElement(submenu).click().build().perform();
Use some Implicit Wait to avoid timeout exceptions to find your web element
Use more specific xpath to find your web element.
In your case first you need to hover on Kitap, Müzik, Film, Oyun menu and then have to perform click on Kitap submenu
You can try to use ExplicitWait with more specific XPath:
WebDriverWait wait = new WebDriverWait(driver, 15);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("(//a[#title='Kitap, Müzik, Film, Oyun']")[2])));
// WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.linkText("Kitap, Müzik, Film, Oyun")));
Actions act = new Actions(driver);
act.moveToElement(element).perform();

Popup scroll not working in selenium webdriver

I am running my script in mozilla firefox I want to scroll popup I
applied so many methods but doesn't work for me
I used keys.tab to reach that element but it was unable to enter text in that textfield using senkeys("xyz#gmail.com)
I used scroll method
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("document.getElementById('youama-email').scrollIntoView(true);");
some exception occur
3.I used Moveto element method but got exception
WebElement element = driver.findElement(By.id("youama-email"));
Actions
actions = new Actions(driver);
actions.moveToElement(element);
actions.click();
actions.perform();
// Initialize Javascript executor
JavascriptExecutor js = (JavascriptExecutor) driver;
// Scroll inside web element vertically (e.g. 100 pixel)
js.executeScript("arguments[0].scrollTop =
arguments[1];",driver.findElement(By.id("<div-id>")), 100);
Please help me to scroll and enter into the email as well as other
fields that will appear after scroll [![enter image description
here][1]][1]
[1]: http://i.stack.imgur.com/D0hqI.png
Try this code. I think what you didn't do was to wait for the element to be visible which i did. See the below code. It is running correctly.
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.get("http://wyomingvapor.com/");
driver.findElement(By.xpath(".//*[#id='header']/div/div[2]/div/a[1]")).click();
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(".//*[#id='y-to-register']/input")));
driver.findElement(By.xpath(".//*[#id='y-to-register']/input")).click();
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(".//*[#id='youama-firstname']")));
driver.findElement(By.xpath(".//*[#id='youama-firstname']")).sendKeys("xyz#gmail.com");
Thread.sleep(2000L);
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_TAB);
robot.keyRelease(KeyEvent.VK_TAB);
robot.keyPress(KeyEvent.VK_TAB);
robot.keyRelease(KeyEvent.VK_TAB);
robot.keyPress(KeyEvent.VK_X);
robot.keyRelease(KeyEvent.VK_X);
robot.keyPress(KeyEvent.VK_Y);
robot.keyRelease(KeyEvent.VK_Y);
robot.keyPress(KeyEvent.VK_Z);
robot.keyRelease(KeyEvent.VK_Z);
If you still stuck then do reply to me, Jyotsana.
Happy Learning :-)