i'm stuck in a funny situation.
Whenever I use implicit wait in my code, my driver is able to locate the elements via its XPath. However when I comment out the implicit wait command, then is not able to locate the element.
Then I was doing some research, and later when I executed the code, I got a different kind of bug. It said "unknown error: cannot get automation extension".
This is really funny because, the wait commands are impacting the way the WebDriver is looking for element on the page.
Please suggest why this is happening and do share your experience if it has happened to any of you before.
The code that is throwing the error is below:-
package xyz;
//import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class Asnmnt11
{
public static void main(String[] args) //throws InterruptedException
{
System.setProperty("webdriver.chrome.driver", "C:\\Users\\Vardhan\\workspace\\SeleniumProject\\files\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://facebook.com");
//driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.findElement(By.cssSelector("input.inputtext")).sendKeys("user#gmail.com");
driver.findElement(By.cssSelector("input[tabindex='2']")).sendKeys("password");
//Thread.sleep(2000);
driver.findElement(By.xpath(".//*[#id='u_0_2']")).click();
//driver.manage().timeouts().implicitlyWait(5000, TimeUnit.SECONDS);
driver.quit();
}
}
Thanks in advance.
When you remove implicit wait then the driver doesn't know how long it should look for the element. It does a quick search and if the element is not present then it will throw the element not found exception.
Xpath elements take more time to find. So it is always good to have an implicit wait. Why?
Searching elements can take time
Website is still loading
Elements are still loading
When I ran your test without any implicit wait the driver gave up finding the element quite fast.
org.openqa.selenium.NoSuchElementException:
no such element: Unable to locate element: {"method":"xpath","selector":".//*[#id='u_0_2']"}
Command duration or timeout: 0 milliseconds
This indicates that the driver was looking for the element for 0 milliseconds and it didn't find it. So it is always a good idea to have an implicit wait.
unknown error: cannot get automation extension
This issue is mostly an chromedriver and chrome issue. Usually is caused by using an older chromedriver version that is not compatible with your current chrome version. Chrome is automatically updated so try to update your chromedriver as well.
If you search element by xpath then it take more time to locate so that it is better to use Implicit or explicit wait. When I used your code without wait some time it throw NoSuchElementException.
AS facebook page take littile time to load and you are trying xpath to locate the elements so it is well and good to use Implicit or explicit wait.
Now as you got the issue
unknown error: cannot get automation extension
it is chromedriver issue, sometime I also got it when i used ChromeOptions. So It is totally irrelevant issue relate with Implicit wait
Related
My Xpath is correct & no iFrame and I can locate element in Chrome console but my program still fails. I have used explicit wait also.
no such element: Unable to locate element: {"method":"xpath","selector":"//*[contains(#ng-click,'authenticationCtrl.onSubmitMage()')]"}
i tested my xpath with Try xpath and it works but when i compile my code i still recieve the error
the page Object :
package com.orange.pageObject;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.CacheLookup;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
public class MageReferentiel {
WebDriver webdriver;
public MageReferentiel(WebDriver rwebDriver) {
webdriver = rwebDriver;
PageFactory.initElements(webdriver, this);
}
#FindBy(xpath = "//*[contains(#ng-click,'authenticationCtrl.onSubmitMage()')]")
#CacheLookup
WebElement connexion;
public void clickConnexion() {
connexion.click();
}
The step definition :
#When("l utilisateur choisi le referentiel")
public void l_utilisateur_choisi_le_referentiel() throws Exception {
mr.clickConnexion();
Thread.sleep(3000);
}
im looking to click in button
thanks
I agree with #Prophet, it could be because of some JS call the button, //*[contains(#ng-click,'authenticationCtrl.onSubmitMage()')] changing it's state to some other state. so what we can do about is that, to try with different locator.
such as :
//button[#translate ='LOGIN']
and see if that works, even if it doesn't try changing it to css.
Since ng elements are going very well with Protractor (Angular), better to use in Protractor in that case, so it suppose to be something like element(by.click('authenticationCtrl.onSubmitMage').click();
I guess the ng-click attribute value is dynamically updated on the page, so when you trying to access that element this element is changed, not having it's initial state.
Instead of locator you are using try this XPath:
//button[contains(text(),'Connexion')]
or this
//button[#translate='LOGIN']
The second element with this locator will be
(//button[#translate='LOGIN'])[2]
Looks like the element is not rendering on time. Try using explicit wait. Following gif shows how it is done using Cucumber:
https://nocodebdd.live/waitime-cucumber
Same been implemented using NoCodeBDD:
https://nocodebdd.live/waittime-nocodebdd
Disclaimer: I am the founder of NoCodeBDD so BDD automation can be achieved in minutes and without code. Through NoCodeBDD you could automate majority of the scenarios and it allows you to write your own code if there are edge cases. Would love to get some feedback on the product from the community. Basic version (https://www.nocodebdd.com/download) is free to use.
The default wait strategy in selenium is just that the page is loaded.
You've got an angular page so after your page is loaded there is a short delay while the JS runs and the element is finally ready in the DOM - this delay is causing your script to fail.
Check out the selenium docs here for wait strategies .
Your options are:
An explicit wait - This needs to be set per element you need to sync on.
I note you say you've used an explicit wait - but where? - It's not present in the code you shared and it might be that you've used the wrong expected condition.
Try something like this:
WebElement button = new WebDriverWait(rwebDriver, Duration.ofSeconds(10))
.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[contains(#ng-click,'authenticationCtrl.onSubmitMage()')]")));
button.click();
Use an implicit wait - you only use this once when you initialise driver and it will wait the specified amount of time for all element interaction.
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
There are other reasons that selenium returns a NoSuchElement but synchronisation is the most common one. Give the wait a go and let me know if it is still giving you trouble.
Through discussion in the comments, the trouble is an iframe
If you google it - there are lots of answers out there.
With frames, you need to identify it, switch to it, do your action(s) then switch back:
//find and switch - update the By.
driver.switchTo().frame(driver.findElement(By.id("your frame id")));
//actions go here
//back to normal
driver.switchTo().defaultContent();
I've run into a problem that has baffled me while attempting to use Selenium 3.4 on jre 1.8 in JUnit. After successfully grabbing a WebElement, attempting to perform the click(), isDisplayed(), sendKeys(), and clear() functions all cause the driver connection to timeout before they can complete. I've wound up creating the following code:
#Test
public void canLogIn(){
WebDriver driver = new HtmlUnitDriver();
driver.get("http://"+ip+"/login/loginpage.html");
WebElement username = driver.findElement(By.id("username_div"));
System.out.println("Username to string: "+username.toString());
/*Thread.sleep(6000);*/
if(!username.isEnabled()) fail();
if(!username.isDisplayed()) fail();
username.click();
username.clear();
username.sendKeys("manager");
...
So far, the code has timed out on username.isDisplayed(), username.click(), username.clear(), and username.sendKeys() when all the other elements were commented out. However, username.toString() works, and shows the correct element, and the code has yet to hang on username.isEnabled(). Thread.sleep() was used to test whether allowing the page to load would eliminate the issue, but to no avail. I have tried executing these commands using Selenium's JavascriptExecutor, also to no avail. I am well and truly stumped at this point, and any assistance you could give me would be greatly appreciated.
Is the username element visible? Perhaps you can try adding this after opening the page:
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until( ExpectedConditions.visibilityOfElementLocated("username_div"));
What exception are you getting if any?
Hi I am new to Selenium Webdriver.
I have taken this website and I am trying to click on the Register Link. I have written the following code.
driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.get("http://www.stepintohealth.qa");
driver.findElement(By.linkText("Register")).click();
while executing the code it is throwing element not found exception. i have tried same in seleniunm IDE it works without any issue. I have verified for the iframes also, but Register link is not in iframes.
Welcome to the world of Selenium. It is often the element wait/find issue that you are facing. I added explicit wait to make sure the element is there before you perform the click.
driver = new FirefoxDriver();
driver.get("http://www.stepintohealth.qa");
By byCss = By.cssSelector(".right1.navmenu a.register");
WebElement myDynamicElement = (new WebDriverWait(driver, 10)).until(ExpectedConditions.presenceOfElementLocated(byCss ));
myDynamicElement.click();
Couple of things to keep in mind:
You don't want to mix the implicit and explicit waits together since that can be a performance issue. It is not recommended.
Selector is very important in finding the element uniquely. I tend to avoid LinkText as much as I can. id should be your first choice always. Try using name,clasName,cssSelector,xpath if you can. In this case className did not work pretty good. Tested with cssSelector and did what it supposed to do. See this for more info.
I'm trying to find the element but I'm getting an error
This is my code:
driver.get(baseURL);
driver.manage().timeouts().implicitlyWait(10 ,TimeUnit.SECONDS);
driver.manage().window().maximize();
//String parentHandle=driver.getWindowHandle();
driver.findElement(By.linkText("Create Account")).click();
System.out.println(driver.getCurrentUrl());
//String currentWindow=driver.getWindowHandle();
//driver.switchTo().window(currentWindow);
//String currentURL=;
//(currentURL);
for (String winHandle : driver.getWindowHandles()) {
driver.switchTo().window(winHandle); // switch focus of WebDriver to the next found window handle (that's your newly opened window)
}
driver.findElement(By.xpath("//html/body/div/div[1]/div[1]/div/div/form/div[1]/input")).sendKeys("9051902811");
driver.close();
//driver.switchTo().window(parentHandle);
}
catch(NoSuchElementException nsee){
System.out.println(nsee.toString());
}
System.exit(0);
}
And I am getting the exception:
Unable to locate element "method":"xpath","selector":"//html/body/div/div[1]/div[1]/div/div/form/div[1]/input"} Command duration or timeout: 89 milliseconds
please help...
Based on what you have shown it is hard to say exactly what the issue is. It could be a couple different things.
1) Does the element exist? If you show the html this could be answered very quickly.
Xpaths are very brittle and very much error prone.
Try to use a different selector if at all possible, id and class are much more reliable.
Here is the link to the By class.
driver.findElement(By.id("id")).sendKeys("keys");
driver.findElement(By.className("className")).sendKeys("keys");
Something that is more concrete, and when content is added that changes the structure, it doesn't break the tests, xpaths wil certainly make your tests brittle.
2) Is the input box loaded yet?
Selenium sometimes will try to find an element that isn't loaded yet. Explicit Waits will help solve this issue, you can use these waits along with ExpectedConditions to wait for an element to be visible, clickable, not visible, among other things. Do Not use thread.sleep unless there is no other choice(which there probably is).
The below code can be used to wait for the element to be visible.
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
WebDriverWait wait = new WebDriverWait(driver, seconds);
wait.until(ExpectedConditions.visibilityOfElementLocated(By
.xpath(xpath)));
You can use this to ensure the element is visible by xpath, but if possible you should consider using a different selector, one that won't make your test very visible. If the element does not have an id/class, you can anchor the xpath to a more reliable selector as well, to reduce some brittleness.
I will be happy to provide more info, if you provide the html.
package testproject;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.*;
public class mytestclass {
public static void main(String[] args) {
WebDriver Driver = new FirefoxDriver();
Driver.get("https://www.gmail.com/");
WebElement wb= Driver.findElement(By.name("Email"));
wb.sendKeys("sweta");
WebElement wb1= Driver.findElement(By.name("Passwd"));
wb1.sendKeys("123456");
WebElement wb2= Driver.findElement(By.id("signIn"));
wb2.click();
WebElement wb3= Driver.findElement(By.xpath(".//*[#id='gb']/div[1]/div[1]/div[2]/div[5]/div[1]/a"));
wb3.click();
WebElement wb4= Driver.findElement(By.id("gb_71"));
wb4.click();
}
}
When i am executing this code everything is going fine till the point where i want the sign in button to be clicked. I am getting exception which says that
Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"xpath","selector":".//*[#id='gb']/div[1]/div[1]/div[2]/div[5]/div[1]/a"} but when i am trying to locate it using fierbug its working fine.
In the above mentioned code i changed the email id and password to keep the email safe.
I was facing problem with one more program which i already posted on stakwave so if u can then please have a look at this link-webdriver is not able to click on a hyperlink in firefox
Are you certain your page is completely loaded after you sign in?
Did you set a timeout for your webdriver? (how long it has to wait for elements). Probably it reads your html before it's completey loaded.
Webdriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);
To find out quickly if this is the problem do Thread.sleep(8000) after you do wb2.click();
Remove the dot at the beginning of your xpath expression. That way you have an xpath expression thaty could match everything. With the dot at the beginning you might retrict yourself depending on if the current node is the root node or not. Ther eis no way to know it. Just the fact the dot can only give you trouble. Unfortunately you cannot always trust what tools like firebug give you (it is still true in 99% of the case).
Of course, ensure that the elemetns you are targeting are already on the screen as suggested by the previous answer.
I faced similar problem,
issue resolved after setting timeout.
Webdriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);
Not sure whats the role of timeout here though.
remove the dot(.) and star(*) from the xpath and give proper tag name in place of star.
for example if #id=gb is the id of div element, place div in place of star.
Hope it will work.
//launch browser
FirefoxDriver driver = new FirefoxDriver(options);
driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);
//gmail login :
driver.get("http://www.gmail.com");
driver.findElement(By.id("identifierId")).sendKeys("****",Keys.ENTER);
Thread.sleep(5000);
driver.findElement(By.id("password")).sendKeys("***",Keys.ENTER);
//logout:
driver.findElement(By.xpath("//div[#id='gb']/div[1]/div[1]/div[2]/div[4]/div[1]/a/span")).click();
Thread.sleep(5000);
driver.findElement(By.linkText("Sign out")).click();