Program terminates entering pass into textbox with webrunner - java

public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "C://chromedriver//chromedriver.exe");
WebDriver driver = new ChromeDriver();
//Maximize the Browser window
driver.manage().window().maximize();
driver.get("https://google.com");
WebElement signin = driver.findElement(By.id("gb_70"));
signin.click();
WebElement username = driver.findElement(By.id("Email"));
username.sendKeys("email#gmail.com");
WebElement next = driver.findElement(By.id("next"));
next.click();
WebElement password = driver.findElement(By.id("Passwd"));
password.sendKeys("password");
WebElement next1 = driver.findElement(By.id("signIn"));
next1.click();
My program terminates when it gets to the password entry screen.. it says
Exception in thread "main" org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"id","selector":"Passwd"}
(Session info: chrome=53.0.2785.143)
However, I have tried ID element and Xpath element and they are both correct
:
(

Possibly you need to wait after next.click(); command. After clicking next button, your input filed is not loaded yet. So, wait for the element to get loaded and put text in it.
Edit:
You can use explicit wait for this. It's better way then Thread.sleep(). Specify a maximum time to wait for element. If the specified time elapsed before your element is visible then it will throw a exception.
Code snippet:
WebDriverWait wait = new WebDriverWait(driver, 30); // waiting for maxiumum of 30 seconds
wait.until(ExpectedConditions.presenceOfElementLocated(By.id("Password")));

Related

Unable to identify and click on the "Next" button in date field

I am trying to automate the flight booking of http://www.mercurytravels.co.in using Selenium+java.
The issue that I am facing is that I'm unable to click on the ">" (next button) of the "Date of Return" date field in the Book Flights Online page.
Getting error message that --> Exception in thread "main" org.openqa.selenium.ElementNotInteractableException: element not interactable
The code that I've written is:
driver.findElement(By.cssSelector("input[name='returnDate']")).click();
//Retrieving current year value
String currentReturnMonthYear = driver.findElement(By.xpath("//div[#class='datepicker dropdown-menu'][4]/div[#class='datepicker-days']/table[#class=' table-condensed']/thead/tr[1]/th[2]")).getAttribute("innerHTML");
driver.findElement(By.xpath("//div[#class='datepicker dropdown-menu'][4]/div[#class='datepicker-days']/table[#class=' table-condensed']/thead/tr[1]/th[3]")).click();
Request any inputs on issue that I'm facing?
org.openqa.selenium.ElementNotInteractableException Indicates that a click could not be properly executed because the target element was obscured in some way. ElementNotInteractableException is Thrown to indicate that although an element is present on the DOM, it is not in a state that can be interacted with.
Using Explicit wait:
driver.findElement(By.cssSelector("input[name='returnDate']")).click();
WebElement next = (new WebDriverWait(driver, 30)).until(ExpectedConditions.elementToBeClickable(By.xpath(
"//div[#class='datepicker dropdown-menu'][4]/div[#class='datepicker-days']/table[#class=' table-condensed']/thead/tr[1]/th[3]")));
next.click();
Using Actions:
driver.findElement(By.cssSelector("input[name='returnDate']")).click();
WebElement next = (new WebDriverWait(driver, 30)).until(ExpectedConditions.elementToBeClickable(By.xpath(
"//div[#class='datepicker dropdown-menu'][4]/div[#class='datepicker-days']/table[#class=' table-condensed']/thead/tr[1]/th[3]")));
Actions action = new Actions(driver);
action.moveToElement(next).click().build().perform();
Using JavascriptExecutor:
driver.findElement(By.cssSelector("input[name='returnDate']")).click();
WebElement next = (new WebDriverWait(driver, 30)).until(ExpectedConditions.elementToBeClickable(By.xpath(
"//div[#class='datepicker dropdown-menu'][4]/div[#class='datepicker-days']/table[#class=' table-condensed']/thead/tr[1]/th[3]")));
JavascriptExecutor javascriptExecutor = (JavascriptExecutor) driver;
javascriptExecutor.executeScript("arguments[0].click();", next);

selenium webdriver detect UI pop up error messages

I have this react application being tested using selenium webdriver.
if my login is wrong, how do i detect the text using selenium webdriver? I am unable to find the code/ figure out how to trap the pop up message . 'authentication failed'
#Test
public void failed_login() {
System.setProperty("webdriver.chrome.driver",
"C:\\Users\\rahul\\Downloads\\chromedriver_win32_83\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
driver.get("http://testingapp.workspez.com");
driver.manage().window().maximize();
WebElement username = driver.findElement(By.id("field_email"));
WebElement password = driver.findElement(By.id("field_password"));
WebElement login = driver.findElement(By.xpath("//*[text()='Log In']"));
username.sendKeys("wrongemail#gmail.com");
password.sendKeys("wrongpassword");
login.click();
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
driver.manage().timeouts().implicitlyWait(120, TimeUnit.SECONDS);
String url = driver.getCurrentUrl();
assertEquals(url, "http://testingapp.workspez.com/login");
}
You can use below code to verify if authentication failed pop up is displayed or not:
List<WebElement> popUpElement = driver.findElements(By.id("client-snackbar");
if(popUpElement.size() != 0){
System.out.println("Pop up is Present "+popUpElement.get(0).getText());
}else{
System.out.println("Pop up is Absent");
}
After you perform click, you can use Explicit Wait:
try{
WebDriverWait wait = new WebDriverWait(driver, 10); //10 seconds
WebElement messageElement = wait.until(
ExpectedConditions.visibilityOfElementLocated(
By.xpath("//*[#id = 'client-snackbar']")
)
);
//if reach here, means the error is visible.
System.out.println(messageElement.getText());
}catch(TimeoutException ignored){
//if trigger timeoutexception
//that means the element with message is not visible, that means no error
}
This will wait 10 seconds for the element to be visible.
If it is not visible, it will throws TimeoutException which you can simply ignore.
That means the error was not visible in first 10 seconds after click.
You can change the time with whatever you want.
You can find the locator for that toast message.
For locating web element
Go to your login.
Enter your invalid credentials.
Press F12 and go to console/Sources
Click on login to get your message.
Press F8 to pause it. Now you can inspect your element.
So from webelement you can fetch text with gettext() method.
You can apply explicit wait to wait for your error message.

Unable to locate an element using xpath error in selenium-java

This is the code I am trying to execute
public WebDriver createPart() {
try {
driver.findElement(By.id("username")).sendKeys("502409373");
driver.findElement(By.id("password")).sendKeys("Magic14Magic");
driver.findElement(By.id("submitFrmShared")).click();
driver.manage().timeouts().implicitlyWait(70, TimeUnit.SECONDS);
driver.manage().timeouts().pageLoadTimeout(70, TimeUnit.SECONDS);
Select dropCountry = new Select(driver.findElement(By.id("txtNewLocation")));
dropCountry.selectByVisibleText("India");
driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);
driver.findElement(By.xpath("//button[#class='btn']/label")).click();
driver.manage().timeouts().implicitlyWait(70, TimeUnit.SECONDS);
driver.manage().timeouts().pageLoadTimeout(70, TimeUnit.SECONDS);
Thread.sleep(10000);
driver.findElement(By.xpath("//li[#class='icon-button add']/span")).click();
driver.findElement(By.xpath("//div[#id='ENCActions']/a/label")).click();
driver.findElement(By.xpath("//label[starts-with(text(),'Create Part...')]")).click();
driver.manage().timeouts().implicitlyWait(70, TimeUnit.SECONDS);
driver.manage().timeouts().pageLoadTimeout(70, TimeUnit.SECONDS);
String parentWindowHandler = driver.getWindowHandle(); // Store your parent window
String subWindowHandler = null;
Set<String> handles = driver.getWindowHandles(); // get all window handles
Iterator<String> iterator = handles.iterator();
while (iterator.hasNext()){
subWindowHandler = iterator.next();
}
driver.switchTo().window(subWindowHandler); // switch to popup window
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = driver.findElement(By.xpath("//select[#id='Type-Field']//following-sibling::div//div//input"));
element.click();
Thread.sleep(3000);
element.sendKeys(Keys.BACK_SPACE);
Thread.sleep(3000);
element.sendKeys("Subassy");
Thread.sleep(4000);
driver.findElement(By.xpath("//div[#data-value='Subassy']")).click();
driver.findElement(By.xpath("//span[text()='Description']//parent::td//following-sibling::td//textarea")).sendKeys("Testing");
driver.findElement(By.xpath("//option[text()='BioSc-DS-Chemical']//parent::select")).click();
Thread.sleep(2000);
driver.findElement(By.xpath("//option[text()='BioSc-DS-Chemical']")).click();
driver.findElement(By.xpath("//a[text()='Done']")).click();
driver.switchTo().window(parentWindowHandler);
driver.manage().timeouts().implicitlyWait(70, TimeUnit.SECONDS);
driver.manage().timeouts().pageLoadTimeout(70, TimeUnit.SECONDS);
wait = new WebDriverWait(driver, 60);
element = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//a[#title='Part Details']")));
element.click();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return driver;
}
This is the html code of the element, I am unable to find.
<div class="tabcontent">
<a class="tablink" href="javascript:void(0);" title="Part Details"
onclick="tvcTabPage_tabClicked(this,true)">Part Details</a>
<a href="javascript:void(0);" class="closetab"
onclick="tvcTabPage_tabClosed(this)" title="Close Tab"></a>
</div>
This is the stack trace
Exception in thread "main" org.openqa.selenium.TimeoutException: Expected
condition failed: waiting for element to be clickable: By.xpath:
//a[#title='Part Details'] (tried for 60 second(s) with 500 MILLISECONDS
interval)
Caused by: org.openqa.selenium.NoSuchElementException: Cannot locate an
element using By.xpath: //a[#title='Part Details']
Even though I have used wait statement. I am unable to locate the element. I have verified the xpath using chrome xpath. Please help me resolving my issue.
Selenium version:3.3.1. The actual flow is, I am clicking a link in main page which results a popup consisting a form. After filling the form and clicking on done, popup gets closed automatically and main page gets refreshed and a new page is opened. Now, I am trying to click on the Part Details link in the new main page which is not working.
I have added this peice of code to find the right window handler.
subWindowHandler = null;
Set<String> handles1 = driver.getWindowHandles(); // get all window handles
iterator = handles1.iterator();
while (iterator.hasNext()){
subWindowHandler = iterator.next();
driver.switchTo().window(subWindowHandler);
element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//a[#title='Part Details']")));
System.out.println("KK1");
}
Giving me the output:
Exception in thread "main" org.openqa.selenium.TimeoutException: Expected
condition failed: waiting for visibility of element located by By.xpath:
//a[#title='Part Details'] (tried for 10 second(s) with 500 MILLISECONDS
interval)
Here, required 'Part Details' element is located in a frame which I have included in code. But, i am unable to locate the element inside the frame.
This is my modified code.
System.out.println(driver.switchTo().window(parentWindowHandler).getTitle());
driver.switchTo().frame("content");
driver.switchTo().frame("detailsDisplay");
driver.findElement(By.xpath("//a[#title='Part Details']")).click();
But I am facing this error.
Exception in thread "main" org.openqa.selenium.NoSuchElementException: no
such element: Unable to locate element:
{"method":"xpath","selector":"//a[#title='Part Details']"}
I have attached my screenshot of the html code.
HTML screenshot
Change visibilityOfElementLocated instead of elementToBeClickable.
You can directly find the webeelement, and then click on it as shown below:
element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//a[#title='Part Details']")));
To invoke click() on the link with text as Part Details you need to induce WebDriverWait and invoke click() as follows :
linkText :
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.linkText("Part Details"))).click();
cssSelector :
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("a.tablink[title='Part Details']"))).click();
xpath :
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//a[#class='tablink' and #title='Part Details']"))).click();
your parentWindowHandler should not be correct, you fetch it at wrong time point.
Try move below code line to the first line in the try block
try {
String parentWindowHandler = driver.getWindowHandle(); // Store your parent window
Once switched to parent window, try to refresh the page then find the element as given below. It may solve your issue.
driver.switchTo().window(parentWindowHandler);
driver.navigate().refresh();
driver.manage().timeouts().implicitlyWait(70, TimeUnit.SECONDS);
driver.manage().timeouts().pageLoadTimeout(70, TimeUnit.SECONDS);
wait = new WebDriverWait(driver, 60);
element = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//a[#title='Part Details']")));
element.click();

how to make the selenium to wait till item is selected in the litbox

My script is getting failed to select the item from a list box, this problem i am facing when i execute the script first time, for next time there is no problem.In console the message printed like
"org.openqa.selenium.TimeoutException"
Here is my code
public void primarydign(final String diagnosis) throws InterruptedException{
primarydiag.sendKeys(diagnosis);
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[#id='ui-id-3']")));
}
here is my html
can i use fluentwait instead of Webdriverwait can any one help me plz
thanks in advance
srinuvas m
Your id looks like dynamic generated as you provided By.xpath("//*[#id='ui-id-3']") to locate the present time the id of your listbox seems as ui-id-6 as you provided in the comment. So in this case you can not locate the listbox using id. try using cssSelector as below :-
public void primarydign(final String diagnosis) throws InterruptedException{
primarydiag.sendKeys(diagnosis);
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("ul.ui-autocomplete.ui-front.ui-menu.ui-widget.ui-widget-content")));
element.click();
}
Hope it will work...:)

WebDriverWait is not working

I am using Windows 8, IE 10 (java - WebDriver 2.37.0) and I am trying to wait until the element is loaded on the page. I used following code:
WebDriver driver = new FirefoxDriver();
driver.get("http://abc.com");
WebElement myDynamicElement = (
new WebDriverWait(driver, 10).until(
ExpectedConditions.presenceOfElementLocated(By.id("myDynamicElement")));
But it's throwing a timeout exception. If I remove this code, it's able to identify the element on the webdriver.
I tried the same code in other browsers as FireFox, Chrome but its still throwing error.
Any help is appreciated.
Thanks
You're assigning that wait to the variable myDynamicElement. If you don't give the WebElement variable something to do, Selenium will throw that timeout exception. If you just want to wait for the element to be present then there is no need to assign it to a WebElement variable.
WebDriver driver = new FirefoxDriver();
driver.get("http://abc.com");
new WebDriverWait(driver, 10).until(
ExpectedConditions.presenceOfElementLocated(By.id("myDynamicElement")));
If you need to assign that variable for later use then do something with the element.
WebElement myDynamicElement =
new WebDriverWait(driver, 10).until(
ExpectedConditions.presenceOfElementLocated(By.id("myDynamicElement")));
myDynamicElement.isDisplayed();
public static void waitForElementToAppear(Driver driver, By selector, long timeOutInSeconds) {
WebDriverWait wait = new WebDriverWait(driver, timeOutInSeconds);
wait.until(ExpectedConditions.visibilityOfElementLocated(selector));
}

Categories