How to identify a "search" in Kayak.co.in/flights/One-way page in selenium web driver.
driver = new FirefoxDriver();
driver.get("https://www.kayak.co.in/");
driver.manage().window().maximize();
driver.findElement(By.xpath("//a[#href='/flights']")).click();
driver.findElement(By.xpath("//label[#title = 'One-way']")).click();
driver.findElement(By.xpath("//*[contains(#id='-submit')]")).click();
driver.quit();
Getting the following exception after executing the above code:
Exception in thread "main" org.openqa.selenium.NoSuchElementException:
Unable to locate element: {"method":"xpath","selector":"//*[contains(#id='-
submit')]"}
There are multiple buttons with this xpath. You should specify more your xpath to obtain an unique button. Try with this one .//div[contains(#class,'Flights-Search-StyleJamFlightSearchForm')]/.//div[contains(#class,'centre')]/button[#title='Search']
You should declare a WebElement while calling an element to precisely targeting it and using moveToElement command instead of simply calling FindElement.By.xxxxx ex:
//*** Calling a WebElement and using moveToElement command***//
WebElement (anyElementname) = browser.findElement(By.partialLinkText("xxxxxxxxxxx"));
action.moveToElement(anyElementname).perform();
//*** Waiting for 8 seconds***//
Thread.sleep(8000, 80000);
You can use 'waits' for providing page load time and interacting between elements
I am converting first xpath (flights) command for you. You can do the rest in the same way:
WebElement flights = driver.findElement(By.xpath("//a[#href='/flights']")).click();
action.moveToElement(flights).perform();
Dont forget to import relevant libraries like for action, you have to import:
import org.openqa.selenium.interactions.Actions;
Let me know if it works for you or not. Cheers!
Related
I have tried the following code but it is throwing the exception (ElementNotVisibleException)
FirefoxDriver dr = new FirefoxDriver();
dr.get("http://54.169.235.143/book.html?v=0.03");
System.out.println("First Testcase");
System.out.println(dr.findElement(By.id("user_name")));
dr.findElement(By.id("user_name"));
dr.findElement(By.id("user_name")).click();
dr.findElement(By.id("user_name")).getAttribute("user_name");
dr.findElement(By.id("user_name")).clear();
dr.findElement(By.id("user_name")).sendKeys("student100");
What am I doing wrong and how to fix it?
Actually your page taking time to load so web driver need wait until element gets visible , Below code will solve your issue :
WebDriverWait wait= new WebDriverWait(dr,30);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("user_name")));
dr.findElement(By.id("user_name")).clear();
dr.findElement(By.id("user_name")).sendKeys("test");
wait= new WebDriverWait(dr,30);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("pass_word")));
dr.findElement(By.id("pass_word")).clear();
dr.findElement(By.id("pass_word")).sendKeys("test");
I have just added wait for elements.
n software testing services this can be achieved by many ways some of the options are displayed above remaining are as follow.
Using java script
driver.executeScript("document.getElementByXpath('element').setAttribute('value', 'abc')");
Using action class Actions actions = new Actions(driver);
actions.click(driver.findElement(element) .keyDown(Keys.CONTROL).sendKeys("a").keyUp(Keys.CONTROL).sendKeys(Keys.BACK_SPACE).build().perform());
I have a dropdown which is hidden at the moment it loads and with a button click it is set to visible and I can see it when the selenium is running it in the browser but still it gives me this exception
org.openqa.selenium.WebDriverException: ElementNotVisibleError: Element is not currently visible and may not be manipulated'ElementNotVisibleError: Element is not currently visible and may not be manipulated' when calling method: [wdIMouse::click] Command duration or timeout: 47 milliseconds
Can someone suggest how we can resolve this?
Try using Actions and WebDriverWait
Maybe something like this
Actions builder = new Actions(driver);
WebDriverWait wait = new WebDriverWait(driver);
Action clickTheDropDown = builder.moveToElement(dd).Click(otherElement).build();
clickTheDropDown.perform();
wait.Until(Expectedcondition.VisibilityOfElement(dd);
I'm attempting to write a Selenium test script in Java for a web application. The application has a button which dynamically adds a form field onto the screen. The problem I've been having is that I can't seem to get the Selenium Web Driver to recognize the new form element element. I first tried initializing the elements in the constructor using #FindBy like this:
#FindBy(how = How.CSS, using = "#WIN_0_536870929") private WebElement form;
Then, I tried using an explicit wait to make sure the element was loaded (on the button which adds the form to the DOM):
WebDriverWait webDriverWait = new WebDriverWait(this.getDriver(), 20);
WebElement formButton = webDriverWait.until(ExpectedConditions.elementToBeClickable(By.id("WIN_0_536870929")));
formButton.click();
But either way, the dynamic form elements fail to load. There error I get is as follows:
org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"id","selector":"#WIN_0_817000996"}
Even more confusing, when I step through the test in the debugger, the form field is fully loaded into the DOM before the test tries to access it.
Does anyone have any idea what could be causing this to happen?
So it turns out I was trying to access an element that was in an iframe. Big thanks to Vivek Singh for suggesting I look into that. I was able to get into the iframe using this line of code:
this.driver.switchTo().frame(this.driver.findElement(By.cssSelector("#WIN_0_817000899 iframe")));
You have to define your WebElement only if you are expecting for its visibility:
public static WebElement waitForVisibleElement( WebDriver driver, By locator, int timeOutInSeconds ) {
WebDriverWait wdw = new WebDriverWait( driver, timeOutInSeconds );
try {
return wdw.until( ExpectedConditions.visibilityOfElementLocated(locator) );
} catch ( NoSuchElementException | TimeoutException | NullPointerException ex ) {
return null;
}
}
I am trying to write a few test cases for automation testing as a part of practice assignment in selenium webdriver using java.
The java version is 1.6 and selenium webdriver version is 2.39 while firefox browser version is 29.0.1.
I am trying to access the drop down titled CARSIZE in the following link:
http://www.carrentals.com/
I am not able to manipulate it.
I have tried the following code...
driver.get("http:\\www.carrentals.com/");
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
Select dropdown= new Select(driver.findElement(By.xpath("//*[#name='carType']")));
dropdown.selectByValue("carsize-1");
With the above code, it seems that I am able to find the element(as no exception is thrown) but not change a value. When I try to change a value by SELECTBYVALUE method, I get an exception saying element not visible. Can someone help me?
The Html code for the above can be seen in firebug and just for information I have also tried using ID and name instead of XPath for the concerned select box but I get the same exception.
Try this:
Select dropdown= new Select(driver.findElement(By.xpath("//select[#id='cartype']")));
dropdown.selectByVisibleText("Small Cars");
Updated.
Try this code. It works for me.
String dropdownXpath = "//label[#for='cartype']/following-sibling::div[#role='listbox']";
WebElement textInDropDown = webDriver.findElement(By.xpath(dropdownXpath + "//div[#class='text']"));
textInDropDown.click();
webDriver.findElement(By.xpath(dropdownXpath)).sendKeys("Small Cars");
It locates text in dropdown element and then sends value which you want to select.
I am creating a test and having some issues. Here is the scenario. I use Selenium Web driver to fill out a form on Page1 and submit the form by clicking a button. Page2 starts loading... but the problem is, Page2 uses Google Analytics codes, and sometimes it takes forever for the page to stop loading.
Even though the expected element is already present, Selenium web driver does not proceed until the whole web page is fully loaded.
How do I make Selenium to move on to the next task or stop loading external javascript/css if the expected element is already present?
I tried tweaking the following settings but no luck.
driver.manage().timeouts().pageLoadTimeout(60, TimeUnit.SECONDS);
driver.manage().timeouts().setScriptTimeout(30, TimeUnit.SECONDS);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
TEMPORARY SOLUTION: Scroll below for answer!
Give below approaches a shot.
driver.findElement(By.tagName("body")).sendKeys("Keys.ESCAPE");
or
((JavascriptExecutor) driver).executeScript("return window.stop");
Alternatively, you can also use WebDriverBackedSelenium as shown in the snippet below from Vincent Bouvier.
//When creating a new browser:
WebDriver driver = _initBrowser(); //Just returns firefox WebDriver
WebDriverBackedSelenium backedSelenuium =
new WebDriverBackedSelenium(driver,"about:blank");
//This code has to be put where a TimeOut is detected
//I use ExecutorService and Future<?> Object
void onTimeOut()
{
backedSelenuium.runScript("window.stop();");
}
Source: https://sqa.stackexchange.com/a/6355
Source: https://stackoverflow.com/a/13749867/330325
So, I reported to Selenium about these issues. And the temporary workaround is... messing with Firefox's timeout settings. Basically by default Firefox waits about 250 seconds for each connection before timing you out. You can check about:config for the details. Basically I cranked it down so Firefox doesn't wait too long and Selenium can continue as if the page has already finished loading :P.
Similar config might exist for other browsers. I still think Selenium should let us handle the pagetimeout exception. Make sure you add a star to the bug here: http://code.google.com/p/selenium/issues/detail?id=6867&sort=-id&colspec=ID%20Stars%20Type%20Status%20Priority%20Milestone%20Owner%20Summary, so selenium fixes these issues.
FirefoxBinary firefox = new FirefoxBinary(new File("/path/to/firefox.exe"));
FirefoxProfile customProfile = new FirefoxProfile();
customProfile.setAcceptUntrustedCertificates(true);
customProfile.setPreference("network.http.connection-timeout", 10);
customProfile.setPreference("network.http.connection-retry-timeout", 10);
driver = new FirefoxDriver(firefox, customProfile);
driver.manage().deleteAllCookies();
Once you have checked for the element and you know that it is present, you could either navigate to/load a different page (if the next tasks are on a different page) or if the tasks are on the same page (as you anyway do not need the elements that have not yet loaded), you could continue as usual - selenium will identify the elements which have already been loaded. This works for me when I work with feature rich pages.
Instead of using the webdriver click() to submit the form use jsexecutor and do a click. Jsexecutor does not wait for page load and you can with other actions.
As per the above scenario explained i feel its best to use the below wait command in the first page.
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(ExpectedConditions.presenceOfElementLocated(By.id(>someid>)));
Once the required element is found in the first page next you can proceed to the second page.
As per the above scenario explained i feel its best to use the below wait command in the first page.
WebDriverWait wait = new WebDriverWait(driver, 10); WebElement element =
wait.until(ExpectedConditions.presenceOfElementLocated(By.id(>someid>)));
Once the required element is found in the first page next you can proceed to the second page.
Use explicit/webdriver wait----
WebDriverWait wt=new WebDriverWait(driver, 20);
wt.until(ExpectedConditions.elementToBeClickable(By.name("abc")));