Unable to click 2 value from the dropdown in webdriver - selenium

The Code is:
public void setRing(int index, String ringPattern) throws InterruptedException {
List<WebElement> webElementList = driver.findElements(By.xpath(an.getProperty("an_ringPattern")));
webElementList.get(index).click();
List<WebElement> options = webElementList.get(index).findElements(By.tagName("option"));
for (WebElement element : options) {
if (element.getText().equals(ringPattern)) {
element.click();
Thread.sleep(2000);
}
}
}
When I execute this code in debug mode, it is working fine. It selects whatever value I pass to the method.
But when I run this code it is not able to change the value in the drop down. It selects the value in the drop down, but it's not able to set the value.
Please let me know if I am missing something.

If the Exception .Error message display below at org.openqa.selenium.support.ui.WebDriverWait.timeoutExceptio‌​n(WebDriverWait.java‌​:80) is being thrown, means that somehow that element is not ready on the frame, the driver is not on that frame or the xpath is wrong.
You can try to switch to the frame where the element is located, like this: driver.switchToFrame("here goes the id of the frame"); You can inspect the ID of the frame or sometimes just passing the integer 0 works. Also, I would rather use wait1.until(ExpectedConditions.visibilityOfElementLocated(element)); instead of wait1.until(ExpectedConditions.presenceOfElementLocated(element));.
When you use presenceOfElementLocated, you cant assure that the element is visible for the driver.

You can use explicit wait in selenium - WebDriverWait
Use below modified code
public void setRing(int index, String ringPattern) throws InterruptedException {
List<WebElement> webElementList = driver.findElements(By.xpath(an.getProperty("an_ringPattern")));
WebDriverWait wait = new WebDriverWait(driver, 60);
wait.until(ExpectedConditions.presenceOfElementLocated(webElementList.get(index)));
webElementList.get(index).click();
List<WebElement> options = webElementList.get(index).findElements(By.tagName("option"));
for (WebElement element : options) {
WebDriverWait wait1 = new WebDriverWait(driver, 60);
wait1.until(ExpectedConditions.presenceOfElementLocated(element));
if (element.getText().equals(ringPattern)) {
element.click();
Thread.sleep(2000);
}
}
}

Related

Find Elements by Xpath not giving the correct element in Selenium Webdriver

In the following example, I have to click only for Jet Airways. It appears that the xpath selected is correct since it gives the right only from the selection.
http://i.imgur.com/8TiOgha.png
However when this same is being pulled by Selenium WebDriver, it says element not visible. But it still gives a Button with zero length text string. as given in watch window.
http://i.imgur.com/jrR0221.png
I would appreciate if anyone can help me to point if any error I am making since I am new to VBA
Not sure if your XPath is correct and is locating the right static element...
You may use this for locating 'Jet Airways'
//label[text()[contains(.,'Jet Airways')]]
Also, using .click(), try clicking on the 'Airlines' dropdown before locating the 'Airlines Dropdown' XPath
//span[#title='Airlines']
Update:
public class SkyScanner
{
static String chkBxXpth = "//label[#class='dropdown-item cfx']/input[#checked]";
public static void main(String[] args) throws InterruptedException
{
WebDriver driver = new FirefoxDriver();
Actions actions = new Actions(driver);
WebDriverWait wait = new WebDriverWait(driver, 30);
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
driver.get("http://skyscanner.cntraveller.com/en-GB/flights#/result?originplace=AUH&destinationplace=LHR");
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//span[#title='Airlines']")));
driver.findElement(By.xpath("//span[#title='Airlines']")).click();
List<WebElement> chkBx = driver.findElements(By.xpath(chkBxXpth));
for(WebElement i : chkBx)
{
actions.moveToElement(i).click().perform();
}
driver.findElement(By.xpath("//label[text()[contains(.,'Jet Airways')]]")).click();
}
}

Element not found - Selenium

I have written a piece of code to login into an application which is working fine. Now I have to click an add button, and I have tried it by Id, XPath, ClassName but it just gives me the exception of element not found. I thought I should apply an explicit wait, but it also did not work. Please check my code below:
public static void Login()
{
Browser.Url = "http://example.com";
_username = Browser.FindElement(By.Id("UserName"));
var password = Browser.FindElement(By.Id("Password"));
var loginbtn = Browser.FindElement(By.ClassName("btn-primary"));
_username.SendKeys("admin");
password.SendKeys("123");
loginbtn.Click();
var supplierTab = Browser.FindElement(By.Id("mainSupplier"));
supplierTab.Click();
WebDriverWait wait = new WebDriverWait(Browser, TimeSpan.FromSeconds(20));
IWebElement myDynamicElement = wait.Until<IWebElement>((d) =>
{
try
{
return d.FindElement(By.Id("btnAddSupplier_SupplierForm"));
}
catch
{
return null;
}
});
var addbtn = Browser.FindElement(By.Id("btnAddSupplier_SupplierForm"));
addbtn.Click();
}
This always gives an exception on the second last line of code that element not found.
Here is the HTML:
Try the following
public static void Login()
{
Browser.Url = "http://example.com";
_username = Browser.FindElement(By.Id("UserName"));
var password = Browser.FindElement(By.Id("Password"));
var loginbtn = Browser.FindElement(By.ClassName("btn-primary"));
_username.SendKeys("admin");
password.SendKeys("123");
loginbtn.Click();
//I think you have mentioned the iframe exist and assuming the element is inside the iframe do the following. If not skip the SwitchTo() part
//you can use name, css to identify the iframe
Browser.SwitchTo().Frame(Browser.FindElement(By.XPath("xpath for the iframe")));
var supplierTab = Browser.FindElement(By.Id("mainSupplier"));
supplierTab.Click();
WebDriverWait wait = new WebDriverWait(Browser, TimeSpan.FromSeconds(20));
IWebElement myDynamicElement = wait.Until<IWebElement>((d) =>
{
return d.FindElement(By.Id("btnAddSupplier_SupplierForm"));
});
//if you think the id is not unique try using xpath or css
//even though you added an explicit wait you never used it
myDynamicElement.Click();
}
Sometimes the element will be existing in the source code but will not be visible for selenium to perform a click operation. Try the below code which will wait until the element is visible:
WebDriverWait wait = new WebDriverWait(_driver, TimeSpan.FromSeconds(20));
IWebElement element = wait.Until(
ExpectedConditions.ElementIsVisible(By.Id("btnAddSupplier_SupplierForm")));
element.Click();
Not sure if this would help, but try calling this function before you click on Add button:
void waitForPageLoad(WebDriver driver)
{
ExpectedCondition<Boolean> pageLoadCondition = new ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver driver) {
return ((JavascriptExecutor)driver).executeScript("return document.readyState").equals("complete");
}
};
wait.until(pageLoadCondition);
}
A NoSuchElementException is thrown when the locating mechanism used is not available in the dom.
A TimeoutException is thrown when your expected condition fails to be true within the time limit. An expected condition can be anything, (exists,visibility,attribute value etc...).
First you want to figure out if the element's locating mechanism you are using is indeed not found in the dom. Here is a good way to manually find out.
Open chrome and navigate to the page.
Open chrome dev tools and click on the console tab.
In the text box type $x("//*[#id='btnAddSupplier_SupplierForm']") and hit enter. This will run an xpath query, basically you are looking for any element that has an id attribute with value "btnAddSupplier_SupplierForm".
If an element appears in the console and is the correct element then it's likely that you are trying to find an element in the dom before the dom is finished loading. If no element appears in the console then you have a bad locator.
Please report your findings.

How to fix 'stale element reference exception' while trying to pick date from date-picker?

I am trying to select date from the Datepicker.Following is the code
WebDriver d=new FirefoxDriver();
Actions a=new Actions(d);
String date="14";
d.get("http://www.eyecon.ro/bootstrap-datepicker/");
d.findElement(By.cssSelector("div#dp3>span")).click();
List<WebElement> trs=d.findElements(By.cssSelector("div.datepicker-days>table>tbody>tr"));
for(WebElement tr:trs) {
List<WebElement> tds=tr.findElements(By.tagName("td"));
for(WebElement td:tds) {
if(date.equals(td.getText())) {
a.moveToElement(td).click().build().perform();
}
}
}
With the above code i got stale element reference exception at this line of code
"if(date.equals(td.getText())) {"
so i have changed the code to this
for(WebElement td:tds) {
while(i<4) {
try {
if(date.equals(td.getText())) {
a.moveToElement(td).click().build().perform();
}
break;
}catch(Exception ex) {
}
System.out.println(i);
i++;
}
}
Now i am able to select the date.But the script is still throwing the stale element reference exception.The script is showing error at this line now
List<WebElement> tds=tr.findElements(By.tagName("td"));
I am working on this for the past 3 days.Any suggestions on how to solve this.
Thanks in advance
In your first code, after you clicked on the element, the DOM changed, as in the Date became "14", and since the both the for-loop were still iterating, hence it threw StaleElementReferenceException.
Similarly, in the second code, you did break the "inside for-loop" that was iterating the td elements, but you didn't break the "outside" one, that continued with iterating the tr elements, hence it threw StaleElementReferenceException yet again.
Resolution:- You should've come out of both the for-loops using break after the element was clicked, and hence averting the StaleElementReferenceException, in the process.
Below code shows how you could've broken out of both the for-loops without any exception:
WebDriver d=new FirefoxDriver();
d.manage().window().maximize(); //Maximizing window
d.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS); //Implicit wait for 20 seconds
Actions a=new Actions(d);
String date="14";
int flag=0;
d.get("http://www.eyecon.ro/bootstrap-datepicker/");
d.findElement(By.cssSelector("div#dp3>span")).click();
List<WebElement> trs=d.findElements(By.cssSelector("div.datepicker-days>table>tbody>tr"));
for(WebElement tr:trs) {
List<WebElement> tds=tr.findElements(By.tagName("td"));
for(WebElement td:tds) {
if(date.equals(td.getText())) {
a.moveToElement(td).click().build().perform();
flag=1; // Set to 1 when the required element was found and clicked.
break; //To come out of the first for-loop
}
}
if(flag==1)
break; //Since the element was found, come out of the second for-loop.
}
NOTE:- I have added the code for maximizing windows and providing implicit wait too, which is actually advised when you are writing selenium scripts.
You should use WebDriverWait and ExpectedConditions to tackle staleness of an element. Below is modified block of your code which I tested and it works.
driver.findElement(By.cssSelector("div#dp3>span")).click();
WebDriverWait wait = new WebDriverWait(driver, 30);
List<WebElement> trs = wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.cssSelector("div.datepicker-days>table>tbody>tr")));
datePicker:
{
for (WebElement tr : trs) {
List<WebElement> tds = tr.findElements(By.tagName("td"));
for (WebElement td : tds) {
wait.until(ExpectedConditions.not(ExpectedConditions.stalenessOf(td)));
if (date.equals(td.getText())) {
td.click();
break datePicker;
}
}
}
}
For more information check WebDriverWait and ExpectedConditions here
Found an easier way to resolve my Stale Element Reference Exceptions.
In Java with Selenium2 try the code below:
public WebElement cleanAndRebuildElement(final WebElement ee) {
WebElement e2;
try {
e2=ee;
e2.isDisplayed();
logger.info("Element is cleaned : Not Stale Anymore !");
} catch (StaleElementReferenceException e) {
e2=null;
} catch(NoSuchElementException nse) {
nse.printstacktrace();
}
return e2;
}
}
As per my understanding when you perform element.click() in for loop DOM reloaded that's why it shows stale element exception.
Use following css selector[It will selected elements from expected date picker only, used this as there are multiple date pickers on page ] & apply break in for loop on matching date like below.It will select date 14 & breaks loop without exception -
String date="14";
d.get("http://www.eyecon.ro/bootstrap-datepicker/");
d.findElement(By.cssSelector("div#dp3>span")).click();
List<WebElement> trs=d.findElements(By.cssSelector(".datepicker.dropdown-menu:nth-of-type(4)>div.datepicker-days>table>tbody>tr>td"));
for(WebElement td:trs) {
if(date.equals(td.getText())) {
td.click();
break;
}
}

wait on handler registering - selenium

There is a html page with button, and my selenium test is testing, that there is an action executed, when the button is clicked.
The problem is, that it looks like the click happens before the javascript is executed - before the handler is bound to the page. The consequence is, that the selenium test will click on the button, but no action happens.
I can solve this problem by repeatedly trying to click and then observe, if the desired action happened (some element is present on page, typically). I'd like to hear that there are some more elegant solutions...
There is no clear way to say "wait until element X has such-and-such handler"; this is a limitation of JavaScript and the DOM (see for example Get event listeners attached to node using addEventListener and jQuery find events handlers registered with an object), and for that matter a selenium Expected Condition can't be created, at least not trivially.
I've resorted to time.sleep(0.5).
You can write some logic to handle this.
I have write a method that will return the WebElement and this method will be called three times or you can increase the time and add a null check for WebElement
Here is an example
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.get("https://www.crowdanalytix.com/#home");
WebElement webElement = getWebElement(driver, "homekkkkkkkkkkkk");
int i = 1;
while (webElement == null && i < 4) {
webElement = getWebElement(driver, "homessssssssssss");
System.out.println("calling");
i++;
}
System.out.println(webElement.getTagName());
System.out.println("End");
driver.close();
}
public static WebElement getWebElement(WebDriver driver, String id) {
WebElement myDynamicElement = null;
try {
myDynamicElement = (new WebDriverWait(driver, 10))
.until(ExpectedConditions.presenceOfElementLocated(By
.id(id)));
return myDynamicElement;
} catch (TimeoutException ex) {
return null;
}
}

Selenium - How to execute a command while WebDriverWait wait.until is running

Is it possible to execute a command while waiting for an element to appear? In particular I'm waiting for an element to appear but it's not going to appear unless I refresh the page. Here's my code
wait = new WebDriverWait(driver, 30);
element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("xpath_here")));
state = element.getText();
The element I'm waiting for is never going to appear unless I hit the app's refresh button. How can I refresh during the 30 second timeout that I set?
You can do something like this:
System.out.println("before wait");
WebElement el = (new WebDriverWait(driver, 30))
.until(new ExpectedCondition<WebElement>(){
//try the following cycle for 30 seconds
#Override
public WebElement apply(WebDriver driver) {
//refresh the page
driver.navigate().refresh();
System.out.println("Refreshed");
//look for element for 5 seconds
WebElement sameElement = null;
try{
sameElement = (new WebDriverWait(driver, 5))
.until(new ExpectedCondition<WebElement>(){
#Override
public WebElement apply(WebDriver driver) {
System.out.println("Looking for the element");
return driver.findElement(By.id("theElementYouAreLookingFor"));
}});
} catch (TimeoutException e){
// if NULL is returns the cycle starts again
return sameElement;
}
return sameElement;
}});
System.out.println("Done");
It will try to get the element for 30 seconds, refreshing the page every 5 seconds.
I would add more lines to that code: basically, if state.isEmpty() then use a Webdriver "Action" class to move the mouse to the refresh button (you app would need a refresh button inside of it for this to work) then run the wait one more time to verify it appears.