I am trying to open google.com first then type "selenium testing".
I only wanted to use className for webdriver using eclipse but I am getting the following error.
Exception in thread "main"
org.openqa.selenium.NoSuchElementException: Unable to locate element:
{"method":"class name","selector":"Tg7LZd"}
Command duration or timeout: 37 milliseconds
Here is my code:
package coreJava;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Training1 {
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com");
driver.findElement(By.className("gLFyf")).sendKeys("selenium testing");
driver.findElement(By.className("Tg7LZd")).click();
}
}
How do I fix this?
This error message...
org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"class name","selector":"Tg7LZd"}
...implies that the GeckoDriver was unable to find any element as per the Locator Strategy you have used.
Your main issue is the classNames you have used are based on JavaScript and are generated dynamically which we can't guess before they are generated.
As an alternative you can use the following solution:
package coreJava;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Training1 {
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com");
WebElement myElement = driver.findElement(By.name("q"));
myElement.sendKeys("selenium testing");
myElement.submit();
}
}
System.setProperty("webdriver.gecko.driver", "geckodriver");
FirefoxDriver driver = new FirefoxDriver();
driver.get("https://google.com");
Thread.sleep(3);
driver.findElement(By.className("gsfi")).sendKeys("selenium testing");
Thread.sleep(3);
driver.findElement(By.className("sbqs_c")).click();
Thread.sleep(3);
driver.close();
This is working code
.
These will open the google chrome and then write "selenium testing" in search box and then search it using the class.
Related
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class E2E {
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "D:\\Selenium\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://www.flygofirst.com/");
driver.findElement(By.xpath("//span[#id='onewaymodal-id']")).click();
Thread.sleep(25000);
driver.findElement(By.xpath("//div[#id = 'oneWaybd']//div[#class ='fromTo']/div[1]")).sendKeys("Ch");
}
}
I Tried to SendKeys in the From Text Box i was getting Element not Intractable.
And I provided Waiting time so that all the web elements will load. After that also i got the same exception Element not Intractable.
Can anyone help me with this
You have to modify the locator, try this one, its working:
// to handle the Accept Cookies button
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(15));
wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("#cookie-btn"))).click();
driver.findElement(By.xpath("//span[#id='onewaymodal-id']")).click();
Thread.sleep(1000);
// modified the below locator
driver.findElement(By.xpath("(.//div[#class='fromTo']//input[#id='roundTripbdFromView'])[2]")).sendKeys("ch");
I answered your previous question also, check that, if it works, mark that as the answer.
Developer Tools view of the popup
I have to select 2nd value in the list of addresses on amazon.com, using Selenium web driver
Here is my Code-
package TechniquesToAutomate;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;
public class Dropdown {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "C:\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://amazon.com");
driver.findElement(By.id("nav-link-accountList")).click();
WebElement element = driver.findElement(By.id("ap_email"));
element.sendKeys("dummy#gmail.com");
element = driver.findElement(By.id("ap_password"));
element.sendKeys("abcd123");
driver.findElement(By.id("signInSubmit")).click();;
driver.findElement(By.id("nav-global-location-slot")).click();
driver.switchTo().activeElement();
driver.findElement(By.cssSelector ("//input[#name=2OPOJECBKZWO3TPDJUPIG12Q1WNJDTIR2A2R2RITDJNW1Q6PXTQ2FQA2OXNA3KWU:1:milprpoqnin")
).click();
driver.findElement(By.name("glowDoneButton")).click();
}
}
But I'm getting an exception-
Exception in thread "main" org.openqa.selenium.NoSuchElementException:
no such element: Unable to locate element:
{"method":"xpath","selector":"//*[#id="a-autoid-2-announce"]/input"}
Any input if there is something else that I can try?
enter image description here
package demo;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class FacebookRegistration {
public static void main(String[] args) {
WebDriver driver = new FireFoxDriver();
driver.get("http://www.facebook.com");
}
}
Have you seen the typo in your IDE? You've imported FirefoxDriver and try to instantiate FireFoxDriver
You have exactly 2 problems in your code as follows :
import :
As you have used import org.openqa.selenium.firefox.FirefoxDriver; on similar lines you have to use :
WebDriver driver = new FirefoxDriver();
System.setProperty() :
While you work with Selenium v3.x.x you have to download the latest geckodriver binary from this link, save it in your system and provide the absolute path of the geckodriver binary through System.setProperty() line as follows :
System.setProperty("webdriver.gecko.driver", "C:\\path\\to\\geckodriver.exe");
Observing following error while executing my test script. Can someone help me in identifying the cause of failure.
org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"id","selector":"lid"}
Command duration or timeout: 20.45 seconds
Here is the snippet of my code. Please note that the element exist in same frame hence frame switch is not needed.
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
//import org.openqa.selenium.support.ui.ExpectedConditions;
//import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.Test;
public class LoginPage
{
#Test
public void testLoginFail()
{
WebDriver driver = new FirefoxDriver();
driver.get("https://www.zoho.com/crm/");
driver.manage().timeouts().implicitlyWait(20,TimeUnit.SECONDS);
driver.findElement(By.linkText("LOGIN")).click();
//WebDriverWait wait = new WebDriverWait(driver,40);
//wait.until(ExpectedConditions.visibilityOfElementLocated((By.id("lid"))));
driver.findElement(By.id("lid")).sendKeys("xyz#gmail.com");
HTML view of the element is:
<input name="lid" id="lid" class="input usrbx" value="" onkeypress="clearmsg()" type="email">
I have inspected the webpage "https://www.zoho.com/crm/lp/login.html" and i am able to see an iframe in it and the input text boxes are inside it. Below is the working code.
public static void main(String[] args)
{
System.setProperty("webdriver.chrome.driver", "C:\\Softwares\\Selenium\\Web Drivers\\chromedriver.exe");
WebDriver driver=new ChromeDriver();
driver.get("https://www.zoho.com/crm/");
driver.manage().timeouts().implicitlyWait(20,TimeUnit.SECONDS);
driver.findElement(By.linkText("LOGIN")).click();
//Switch to the frame
driver.switchTo().frame(0);
driver.findElement(By.id("lid")).sendKeys("xyz#gmail.com");
driver.quit();
}
Hope this helps you. Thanks.
Here is the Answer to your Question:
A few words about the Solution:
While working with Selenium 3.4.0 with geckodriver v0.16.1 & Mozilla Firefox 53.0 you need to download the latest geckodriver from here and provide the absolute path of the geckodriver through System.setProperty.
Try to avoid implicitlyWait as per recent updates implicitlyWait may die in fire soon.
The error you are seeing NoSuchElementException says it all. The id of the element is not traceable.
The element with id lid is within an iframe, so you need to switch to frame first.
Here is your own code with some simple tweaks in it:
#Test
public void testLoginFail()
{
System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("https://www.zoho.com/crm/");
driver.manage().timeouts().implicitlyWait(20,TimeUnit.SECONDS);
driver.findElement(By.linkText("LOGIN")).click();
driver.switchTo().frame("zohoiam");
driver.findElement(By.xpath("//input[#id='lid']")).sendKeys("xyz#gmail.com");
}
Let me know if this Answers your Question.
I am trying to download payslip as PDF from greytip web portal using chrome driver.i am trying to click on link salary by using "driver.findElement(By.linkText("Salary")).click();".But i am unable to click the link and failed with following exception.
Error
org.openqa.selenium.WebDriverException: Element is not clickable at point (198, 139). Other element would receive the click: ... (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 37 milliseconds
And also when ever i ran the script chrome open one extra tab for bit torrent.so here when i ran the program one tab is opened for "https://psdpl.greytip.in" and another tab is opened for bittorrent.How can i handle not to open another bittorrent tab when i ran the program.
Here i am attaching the code and screen shots.enter image description here
Code
package com.webdriver.tests;
import static org.junit.Assert.*;
import org.junit.Test;
import java.io.File;
import java.io.IOException;
import java.util.regex.Pattern;
import java.util.concurrent.TimeUnit;
import org.junit.*;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeDriverService;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
public class PaySlipPDF {
private WebDriver driver;
private String baseUrl;
private StringBuffer verificationErrors = new StringBuffer();
#Before
public void setUp() throws Exception {
System.setProperty("webdriver.chrome.driver", "C:\\soleti\\D-Drive\\Selenium\\chromedriver\\chromedriver.exe");
driver = new ChromeDriver();
baseUrl = "https://psdpl.greytip.in/";
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
#Test
public void testPayslip() throws Exception {
driver.get(baseUrl + "/login.do");
driver.findElement(By.id("j_username")).clear();
driver.findElement(By.id("j_username")).sendKeys("101786");
driver.findElement(By.id("j_password")).clear();
driver.findElement(By.id("j_password")).sendKeys("password");
driver.findElement(By.id("login-button")).click();
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
//driver.findElement(By.xpath("//*[#id='home-page']/div[1]/div[1]/ul/li[2]/a")).click();
WebElement elementToClick = driver.findElement(By.xpath("//*[#id='home-page']/div[1]/div[1]/ul/li[2]/a"));
System.out.println(elementToClick);
// Scroll the browser to the element's Y position
((JavascriptExecutor) driver).executeScript("window.scrollTo(0,"+elementToClick.getLocation().y+")");
// Click the element
elementToClick.click();
//driver.findElement(By.linkText("Salary")).click();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.findElement(By.linkText("View Payslips")).click();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
new Select(driver.findElement(By.id("payroll"))).selectByVisibleText("Mar 2012");
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
driver.findElement(By.className("btn btn-gts-print")).click();
}
#After
public void tearDown() throws Exception {
//driver.quit();
String verificationErrorString = verificationErrors.toString();
if (!"".equals(verificationErrorString)) {
fail(verificationErrorString);
}
}
private boolean isElementPresent(By by) {
try {
driver.findElement(by);
return true;
} catch (NoSuchElementException e) {
return false;
}
}
}
It is the BitTorrent toolbar that is causing the issue. It is launched into Chrome by an extension. Either uninstall it completely, or force Selenium to tell Chrome to disable all extensions. This can be done using the ChromeOptions class:
http://selenium.googlecode.com/svn/trunk/docs/api/java/org/openqa/selenium/chrome/ChromeOptions.html
Use the addArgument method and give it this arguement, which tells Chrome to disable all user extensions:
--disable-extensions
Check the browser settings and see if the default homepage has been changed by the bit torrent toolbar that is installed. If not needed, try to uninstall the bitTorrent toolbar from the browser and rerun your selenium program. Hope this helps.
This problem is specificaly related to chrome installed on your system..Uninstall the google chrome and re-install it..That will fix your problem..:)