Nosuchelementfound exception in Selenium Webdriver - selenium

As a new learner for Selenium, I am trying to identify a basic element on the Google page for the Google Search button. However I am getting a NoSuchElementException even though the element is perfectly visible on the page.
Code Snippet :
String driverPath = "C:\\Users\\nchaudhary006\\IEDriverServer.exe";
System.setProperty("webdriver.ie.driver",driverPath);
DesiredCapabilities capabilities = DesiredCapabilities.internetExplorer();
capabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
InternetExplorerDriver driver = new InternetExplorerDriver(capabilities);
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
driver.get("https://google.com");
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
driver.findElement(By.name("btnK"));
Solutions Tried :
Element until visible option
Scroll down so that page is in view
Identifying element by xpath, id , name, etc.
All possible wait combinations.
What can I be possibly doing wrong?

I am not sure to which element you exactly referred by name as btnK but on https://google.com there is a Search field by name as q
Using Selenium 3.4.0, IEDriverServer 3.4.0 & IE 10.x (& above) the below code will put "Neha Chaudhary" in the Search field.
A few words about the Solution:
Once you set ImplicitlyWait at the begining, it remains valid throughout your execution. So you don't have to mention again & again.
Instead of InternetExplorerDriver implementation you must use the WebDriver interface.
While you search for an element driver.findElement(By.name("btnK")); either store it as a WebElement for future use or perform some action by click() or sendKeys("Neha Chaudhary")
Here is the working code snippet:
String driverPath = "C:\\Utility\\BrowserDrivers\\IEDriverServer.exe";
System.setProperty("webdriver.ie.driver",driverPath);
DesiredCapabilities capabilities = DesiredCapabilities.internetExplorer();
capabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
WebDriver driver = new InternetExplorerDriver(capabilities);
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("https://google.com");
driver.findElement(By.name("q")).sendKeys("Neha Chaudhary");
Let me know if this Answers your Question.

Related

How to find the control present in an iframe using selenium?

I want to find the control present in an iFrame in a webpage using selenium to perform actions like click and setting text in textbox. What is the best way to do that using c# or java language.
By using below code you get controls inside iframe in chrome driver. Install latest selenium chromedriver and place it at C:\chromedriver.exe to below code
System.setProperty("webdriver.chrome.driver", "C:\\chromedriver.exe");
// Initialize browser
WebDriver driver=new ChromeDriver();
//navigate to url
driver.get("https://demoqa.com/frames");
//Switch to Frame using Index
driver.switchTo().frame(0);
//Identifying the heading in webelement
WebElement frame1Heading= driver.findElement(By.id("sampleHeading"));
//Finding the text of the heading
String frame1Text=frame1Heading.getText();
//Print the heading text
System.out.println(frame1Text);
//closing the driver
driver.close();

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

PartialLink text for selecting value in selenium

Scenario: Search google with word 'Selenium' and click on the first link.
I have written this below code:
`WebDriver driver = new FirefoxDriver();
driver.get("https://www.google.com/");
driver.manage().window().maximize();
driver.findElement(By.id("lst-ib")).sendKeys("Selenium");
List<WebElement> alist = driver.findElements(By.partialLinkText("Selenium"));
System.out.println(alist.size());`
but it is giving me size as zero. Why?
Actually when you are going to find list of all links with partial text Selenium, all links are not present in the DOM due to fast execution. You should try using WebDriver to wait until all elements visible in the DOM as below :-
WebDriver driver = new FirefoxDriver();
driver.get("https://www.google.com/");
driver.manage().window().maximize();
driver.findElement(By.id("lst-ib")).sendKeys("Selenium");
WebDriverWait wait = new WebDriverWait(driver, 10);
List<WebElement> alist = wait.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.partialLinkText("Selenium")))
System.out.println(alist.size());
Output:-13
Hope it helps...:)
ur problem occurs because u don't wait after searching. just wait a little moment so that browser can search and show the result. use the below code :
driver.findElement(By.id("lst-ib")).sendKeys("Selenium");
Thread.sleep(5000);
though this kind of wait is not recommended to use. Try to use wait by using until like:
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("element css path")));
u can use by xpath or id or class here.

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

Mouse hover on element

http://www.franchising.com/ ---> Mouse over on (Franchises A-Z) ---> need to click Q
I have tried with the following
WebElement we1=driver.findElement(By.cssSelector("a[href='/franchises/']"));
WebElement we2=driver.findElement(By.cssSelector("a[href='/franchises/q.html']"));
String js = "arguments[0].style.height='auto'; arguments[0].style.visibility='visible';";
((JavascriptExecutor) driver).executeScript(js, we2); // I have used the script since the we2 is not visible
Actions builder=new Actions(driver);
builder.moveToElement(we1).perform();
Thread.sleep(5000);
we2.click();
could any one try and share me the code... Still I'm getting "ElementNotVisibleException"
With firefoxdriver, a lot would depend on what version of driver you are using and what version of Firefox you have on your system since native support would differ based on that.
Following works on Chrome :
WebElement link1 = driver.findElementByLinkText("Franchises A-Z");
Actions action = new Actions(driver);
action.moveToElement(link1).click(driver.findElementByXPath("//a[contains(#href,'franchises/b')]")).perform();
Before going in to the code i just want to you to ensure the version of Selenium server you are using. Please make it to the updated version of 2.28.x
Code:
driver = new FirefoxDriver();
driver.get("http://www.franchising.com/franchises/");
Thread.sleep(5000);
WebElement element=driver.findElement(By.xpath("//tr[3]/td/table/tbody/tr/td[4]/a"));
Actions builder = new Actions(driver);
builder.moveToElement(element).build().perform();
Thread.sleep(5000);
it works fine for me. Try this code. I hope this will work.