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?
Related
I am trying to login Gmail using selenium web driver. The problem I am facing is that I am not able to set the password in the input box.
Here is the generated error message:
Exception in thread "main" org.openqa.selenium.WebDriverException: unknown error: cannot focus element.
Here is my code:
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "C:\\chromedriver.exe");
WebDriver driver=new ChromeDriver();
driver.navigate().to("http://www.gmail.com");
driver.findElement(By.cssSelector("#identifierId")).sendKeys("********#gmail.com");
driver.findElement(By.cssSelector("#identifierNext")).click();
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("#password")));
driver.findElement(By.cssSelector("#password")).sendKeys("********");
driver.findElement(By.cssSelector("#passwordNext")).click();
driver.close();
driver.quit();
}
Wait until an element becomes clickable. Here is how you can do this.
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("input[type=password]"));
driver.findElement(By.cssSelector("input[type=password]")).sendKeys("your password");
This could be a combination of a wait issue and selecting the wrong element.
Try changing your selector in your sendKeys as follows:
driver.findElement(By.cssSelector("input[type=password]")).sendKeys("********");
If that still doesn't work, you could try a different wait condition before that call, such as:
wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("input[type=password]")));
You might need to do some experiments to find the right wait condition, but doing both of those things should get you what you need. :)
The cssSelector of the element you are trying to use is not right.
I would suggest to use dynamically generated xpath and/or cssSelector all the time.
In the code below, I used dynamically generated xpath for email Id and cssSelector for password. Try this and it works fine.
public static void main(String[] args)
{
System.setProperty("webdriver.chrome.driver", "Y:\\Selenium\\chromedriver.exe");
WebDriver driver=new ChromeDriver();
driver.navigate().to("http://www.gmail.com");
driver.findElement(By.xpath("//input[#type='email']")).sendKeys("*********#gmail.com");
driver.findElement(By.cssSelector("#identifierNext")).click();
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("input[type=password]")));
driver.findElement(By.cssSelector("input[type=password]")).sendKeys("*********");
driver.findElement(By.cssSelector("#passwordNext")).click();
driver.quit();
}
You can also use the name atribute here, like this:
driver.findElement(By.name("password")).sendKeys("********");
I think everything is solved, however I would like to clarify why it was not working:
To enter the email, You used an id and it worked fine, because the id identifierId is one of the attribute of the filed you are filling.
To enter the password, you cannot use password as an id because the field you are trying to fill has no id among its attributes. The id password exists, but it embraces a larger amount of WebElement. In fact you are localizing an id which contains this field among other WebElements, hence the error message telling you it is unable to focus
you can either use xpath, such as
driver.findElement(By.xpath("//input[#type='password']")).sendKeys("********");
or css selector such as:
driver.findElement(By.cssSelector("input[type=password]")).sendKeys("********");
but you cannot access this field using an ID.
I tried all those three solutions and they worked.
Regarding the possible waiting issue, do not hesitate to use wrappers with waiter around findElement method, it can help a lot. However, I think it was not the main issue here.
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
I have written the code and that works well when I run this is debugging mode but when I run it in normal mode then I am getting the following exception
org.openqa.selenium.NoSuchElementException: no such element:
Unable to locate element: {"method":"xpath","selector":".//*[#id='address-0']/span"}
The code I have written is:
WebElement searchBox = driver.findElement(By.id("search-input"));
searchBox.sendKeys("somepostcode");
Actions actions = new Actions(driver);
actions.moveToElement(searchBox);
WebElement address = driver.findElement(By.xpath(".//*[#id='address-0']/span"));
actions.moveToElement(address);
actions.click();
actions.perform();
I am not able to understand where should I put wait.
I am using eclipse IDE. The functionality works like when I put some postcode in the search box it search for some addresses at runtime and the user has to select any address related to the postcode. Ajax has been used to fetch the postcode
Here search box is a textbox.
Please let me know if more information is required.
Try adding some wait time before WebElement address = driver.findElement(By.xpath(".//*[#id='address-0']/span"));
Error tells you that, you are trying to create an instance of WebElement "address" before its visible on the page.
Try adding wait before
WebElement address = driver.findElement(By.xpath(".//*[#id='address-0']/span"));
In cases like this, when the script works in debug mode but fails during normal it is almost always the issue with timing. So your page is just not fully loaded at the time you are trying to locate that element.
Place an explicit wait just before your problematic element. It's usually not the best practice to use explicit wait but you can do it as a quick try to see if that solves your problem. If that does you can refactor it into a sturdier solution later.
WebElement myDynamicElement = (new WebDriverWait(driver, 10))
Hope this will help you..
WebElement searchBox = driver.findElement(By.id("search-input"));
searchBox.sendKeys("somepostcode");
Actions actions = new Actions(driver);
actions.moveToElement(searchBox);
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(".//*[#id='address-0']/span")));
WebElement address = driver.findElement(By.xpath(".//*[#id='address-0']/span"));
actions.moveToElement(address);
actions.click();
actions.perform();
I have solved this problem by breaking the postcode in two parts
searchBox.sendKeys("postcodePart1");
searchBox.sendKeys("postcodePart2");
There must be kind of on change event was calling.
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.
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();