selenium webdriver method findElement give Unable to locate element: - selenium

I'm new in testing with selenium (IDE and WebDriver) and Junit i'm facing this problem:
First , i'v learnt how to use selenium IDE with firefox plugin
I create a maven project in Eclipse
Then i used selenium IDE to export the code as java/junit4/webDriver
In eclipse i create a class with code crated by selenium IDE :
public class PB01_TTT {
private WebDriver driver;
private String baseUrl;
private boolean acceptNextAlert = true;
private StringBuffer verificationErrors = new StringBuffer();
WebElement element;
#Before()
public void setUp() throws Exception {
driver = new FirefoxDriver();
baseUrl = "https://w8a.prv:9431";
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
}
#Test
public void testPBO1TTT() throws Exception {
driver.get(baseUrl + "/ProcessPortal/login.jsp");
driver.findElement(By.id("username")).clear();
driver.findElement(By.id("username")).sendKeys("user");
driver.findElement(By.id("password")).clear();
driver.findElement(By.id("password")).sendKeys("password");
driver.findElement(By.cssSelector("span.submit-text")).click();
WebDriverWait wait = new WebDriverWait(driver, 15);
String columnToDisplayXpath = "//html/body/div[2]/div/div/div[1]/div/div/div[2]/div[3]/div/div[1]/div[2]/div[2]/div/div[2]/div[1]/div/div[2]/a";
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(columnToDisplayXpath)));
element = driver.findElement(By.xpath(columnToDisplayXpath));
Assert.assertNotNull(element);
Now when i run my test as junit test all code works until the method --> driver.findElement . It gives to me this error
org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"xpath","selector":"//html/body/div[2]/div/div/div[1]/div/div/div[2]/div[3]/div/div[1]/div[2]/div[2]/div/div[2]/div[1]/div/div[2]/a"}
In debug the By.xpath retrieve the element, i'v tried also the By.cssSelector ,but nothig changed. It's the driver findelement that doesn't work.
Any help will be really appraciated

Related

Unable to locate element for simple node

Using latest Selenium -server-4.7.0 jar and latest jdk , selenium is frequently giving no such element found error.
URL
Sample Code:
public static void main(String[] args) throws InterruptedException
{
System.setProperty(
"webdriver.chrome.driver",
"C:\\Users\\USER\\Downloads\\chromedriver_win32\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://msqabaseappsa.z13.web.core.windows.net/#/");
WebElement b = driver.findElement(By.xpath("//button/span[text()='Microsoft Sign In']"));
b.click();
System.out.print("Click Succeess");
driver.close();
}
Problem : Need to click on "Microsoft Sign In"
Add some wait:
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(15));
driver.get("https://msqabaseappsa.z13.web.core.windows.net/#/");
WebElement b = driver.findElement(By.xpath(".//*[#class='btn btn-success btn-block']"));
b.click();

.NET Tests with Selenium, headless Chrome error

I've been trying to implement headless browser to my tests and I'm getting an error message that shows me this: "Unable to locate element: {"method":"id","selector":"my_id"}". This is the code that I'm working with:
[TestFixture]
class ClientesSystemTest
{
private ChromeOptions options;
private NewClientesPage page;
private IWebDriver driver;
public ClientesSystemTest()
{
options = new ChromeOptions();
options.AddArgument("--headless");
driver = new ChromeDriver(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), options);
page = new NewClientesPage(driver);
}
[Test]
public void CadastraCliente()
{
page.Visita();
page.Cadastra("Paulo", "Guedes", "00870021087", "Rua abcdwxyz, 14", 15);
driver.Close();
}
}
and this is the Cadastra method.:
public void Cadastra(string nome, string sobrenome, string cpf, string endereco, int idade)
{
IWebElement nomeCliente = driver.FindElement(By.Id("Nome"));
IWebElement sobrenomeCliente = driver.FindElement(By.Id("Sobrenome"));
IWebElement cpfCliente = driver.FindElement(By.Id("CPF"));
IWebElement enderecoCliente = driver.FindElement(By.Id("Endereco"));
IWebElement idadeCliente = driver.FindElement(By.Id("Idade"));
IWebElement estadoCivilCliente = driver.FindElement(By.Name("EstadoCivil"));
driver.FindElement(By.CssSelector("[value=Divorciado]")).Click();
nomeCliente.SendKeys(nome);
sobrenomeCliente.SendKeys(sobrenome);
cpfCliente.SendKeys(cpf);
enderecoCliente.SendKeys(endereco);
idadeCliente.SendKeys(idade.ToString());
nomeCliente.Submit();
}
I've tried everything by this point. The test runs normally without the headless feature. Does anyone have a solution for this error? Thanks.
My guess would be that your site shows different elements depending on the browser resolution. Typically the headless browser is a smaller setting so I'd make sure it's set to the same size you use when you test non-headless.

No such element exception | Internet explorer

*** Updated the question with relevant html code.
I'm facing error while trying to select any value from dropdown.
The error is
Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to find element with css selector == #oHeight
I have already set all the IE settings as mentioned in Selenium Docs
The code i have tried is mentioned below:
System.setProperty("webdriver.ie.driver", "D:\\Workspace\\Selenium\\Model\\servers\\IEDriverServer_32bit.exe");
WebDriver driver = new InternetExplorerDriver();
driver.manage().timeouts().implicitlyWait(15000, TimeUnit.MILLISECONDS);
driver.get("http://samples.msdn.microsoft.com/workshop/samples/author/dhtml/refs/showModalDialog2.htm");
WebElement ddlHeight = driver.findElement(By.id("oHeight"));
Select select = new Select(ddlHeight);
select.selectByVisibleText("150");
driver.findElement(By.xpath("//input[#value='Push To Create']")).click();
driver.quit();
The system config is Windows 7 + IE 11
You can try this code , It's working on my System :
I'm using IE 11 + Win7 Professional:
Code
public class Sandeep {
static WebDriver driver;
static WebDriverWait wait;
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.ie.driver", "D:\\Automation\\IEDriverServer.exe");
InternetExplorerOptions options = new InternetExplorerOptions();
options.ignoreZoomSettings();
driver = new InternetExplorerDriver(options);
driver.manage().window().maximize();
wait = new WebDriverWait(driver, 40);
driver.get("http://samples.msdn.microsoft.com/workshop/samples/author/dhtml/refs/showModalDialog2.htm");
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("oHeight")));
wait.until(ExpectedConditions.elementToBeClickable(By.id("oHeight")));
Select select = new Select(driver.findElement(By.id("oHeight")));
select.selectByVisibleText("150");
// wait.until(ExpectedConditions.elementToBeClickable(By.name("Push To Create")));
// driver.findElement(By.name("Push To Create")).click();
driver.close();
}
}
Please let me know if you have any concerns related to this.

find xpath for a repeated angularJs Item

how is possible to find the xpath for an angularJs element? for instance i discovered that all links in my page have the same xpath due to the repeated items in angularJs -->
.//*[#id='div_1_1_1_2']/div/div[1]/div[2]/div[2]/div/div[2]/div[1]/div/div[2]/a
but i have 10 of element , they are differente for text, so i tried with `"
so i tried with contains but it never find it
.//[#id='div_1_1_1_2']/div/div[1]/div[2]/div[2]/div/div[2]/div[1]/div/div[2]/a[contains(#aria-label='Creazione Prodotto')]"`
i use selenium, junit4 , firefox webDriver
this is my code
public class PB01_TTT {
private WebDriver driver;
private String baseUrl;
private boolean acceptNextAlert = true;
private StringBuffer verificationErrors = new StringBuffer();
WebElement element;
#Before()
public void setUp() throws Exception {
FirefoxProfile fxProfile = new FirefoxProfile();
fxProfile.setPreference("browser.download.folderList", 2);
fxProfile.setPreference("browser.download.manager.showWhenStarting", false);
fxProfile.setPreference("browser.helperApps.alwaysAsk.force", false);
fxProfile.setPreference("browser.helperApps.neverAsk.saveToDisk",
"application/pdf, application/x-pdf, application/octet-stream");
fxProfile.setPreference("pdfjs.disabled", true);
driver = new FirefoxDriver(fxProfile);
baseUrl = "https://w8aon2bpm.replynet.prv:9443";
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
#Test
public void testPBO1TTT() throws Exception {
driver.get(baseUrl + "/ProcessPortal/login.jsp");
// driver.get(baseUrl + "/ProcessPortal/dashboards/SYSRP/RESPONSIVE_WORK");
driver.findElement(By.id("username")).clear();
driver.findElement(By.id("username")).sendKeys("user");
driver.findElement(By.id("password")).clear();
driver.findElement(By.id("password")).sendKeys("password");
String columnToDisplay=driver.findElements(By.xpath(".//*[#id='div_1_1_1_2']/div/div[1]/div[2]/div[2]/div/div[2]/div[1]/div/div[2]/a[contains(#aria-label='Creazione Prodotto')]"));
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
element = (WebElement) driver.findElements(By.xpath(columnToDisplayXpath));
Assert.assertNotNull(element);
it always return me a notFoundElement, any suggestions ?
Thank you
If you have 10 links, then there is a high chance they are different in a way even if the path is the same, in this case you need to construct an path based on the thing that is different.
For example: use href, text or other any part that differs
//a[contains(#href, 'part_of_href')]
//a[contains(text(), 'part_of_text')]
//a[#title='title']
//a[contains(#aria-label='Creazione Prodotto')]
If you need any help in getting the selector please add the html section of the links, you can change the url if needed.
Tip: avoid using absolute xpaths and attributes that do not suggest anything and they can change like: .//*[#id='div_1_1_1_2']/div/div[1]/div[2]/div[2]/div/div[2]/div[1]/div/div[2]/a[contains(#aria-label='Creazione Prodotto')]"
This will give you a lot of work in the future.

How to set focus to a web element in Selenium webdriver

I am new to Selenium. I am learning by automating some test scenarios on MakeMyTrip website.
Scenario: Editing the user account created.
Code:(yet to be completed)
public class AccountEdit {
#Test
public void AccEdit()
{
WebDriver driver = new FirefoxDriver();
driver.get("http://www.makemytrip.com/");
driver.manage().window().maximize();
driver.findElement(By.id("ssologinlink")).click();
driver.findElement(By.id("username")).sendKeys("abcd#gmail.com");
driver.findElement(By.id("password_text")).sendKeys("*****");
driver.findElement(By.id("login_btn")).click();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.findElement(By.id("ssologinlink")).click(); **======> Here I notice the click is not happening to select the My Account or My Profile from the drop down.**
}
}
Kindly let me know how I can take the focus back to the webelement once I login.
driver.findElement(By.id("ssologinlink")).click();
works fine the first time but not post the user login.
Thank you for your comments. The element ID had not changed post the login. I had to wait for the user name to appear before I click on the drop down.
Below is the code which worked for me:
public class AccountEdit {
#Test
public void AccEdit()
{
WebDriver driver = new FirefoxDriver();
driver.get("http://www.makemytrip.com/");
driver.manage().window().maximize();
driver.findElement(By.id("ssologinlink")).click();
driver.findElement(By.id("username")).sendKeys(""abcd#gmail.com"");
driver.findElement(By.id("password_text")).sendKeys("*******!");
driver.findElement(By.id("login_btn")).click();
WebElement myDynamicElement = (new WebDriverWait(driver, 10)).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//span[#id='ssologinlink']/strong[contains(text(),'user')]")));
myDynamicElement.click();
}
}
Try waiting for the element to be clickable with Expected Conditions of Explicit waits. See the doc here
public class AccountEdit {
#Test
public void AccEdit()
{
WebDriver driver = new FirefoxDriver();
driver.get("http://www.makemytrip.com/");
driver.manage().window().maximize();
driver.findElement(By.id("ssologinlink")).click();
driver.findElement(By.id("username")).sendKeys("abcd#gmail.com");
driver.findElement(By.id("password_text")).sendKeys("*****");
driver.findElement(By.id("login_btn")).click();
//Waiting for the element to be clickable with Explicit wait
WebElement myDynamicElement = (new WebDriverWait(driver, 10))
.until(ExpectedConditions.elementToBeClickable(By.id("ssologinlink")));
myDynamicElement.click();
}
}
Some time element ID gets changed post login(something like dynamic ID).. pls. check the element ID again and update..