I want to test a site using selenium webdriver (java), but that site contains ajax and HTMLUnit does not see the ajax content.
Is there a workaround?
Example:
WebDriver driver = new HtmlUnitDriver(BrowserVersion.FIREFOX_3_6);
//login into your account
this.login(driver);
//click on edit Profile Link into an ajax-loaded tab
driver.findElement(By.id("editProfile")).click();
//Result: org.openqa.selenium.NoSuchElementException
use Wait condition before interaction with element with must appear after ajax response.
WebDriverWait wait = new WebDriverWait(webDriver, 5);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("xpath_to_element")));
this makes webDriver to wait for your element during 5 secs. This question was asked earlier.
Related
I am using Selenium WebDriver with Java. I have done most things but I am stuck at one point, I have the following HTML code:
<a ng-href="#/studyenrollments/new" ng-show="canCreate" class="btn btn-primary edit-btn" href="#/studyenrollments/new">New Study Enrollment</a>
I've tried the following:
//new study enrollment
driver.findElement(By.linkText("New Study Enrollment")).click();
driver.findElement(By.xpath("/html/body/div[2]/div[3]/a")).click();
((JavascriptExecutor)driver).executeScript("document.querySelector(\"body > div.container.page.ng-scope > div.text-right.ng-scope > a\")");
I expect to click on the New Study Enrollment button and proceed to the next page to complete the registration process
Check if element it inside frame if yes - then - select frame first -
Select Frame name or id of iframe
then do the following -
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath(".//a[#ng-show='canCreate']"))).click();
if there are no frames then you can directly interact with the element -
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath(".//a[#ng-show='canCreate']"))).click();
you need to add explicit wait condition especially when working with pages with Angular JS elements
WebElement newStudyEnrollment= driver.findElement(By.xpath("//a[contains(text(),'New
Study Enrollment')]"));
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.elementToBeClickable(newStudyEnrollment));
newStudyEnrollment.click();
Well, selenium web driver does not work with angular applications due to angular specific controls. You need to use protractor for automating angular and non-angular applications.
I am using following xpath to click on element using JSExecutor in Selenium webdriver. This works fine in Firefox and chrome but does not work in IE.
Any idea to make this work? After lot of trial and error I have made this work in FF and chrome and have come up with the following XPath.
//*[contains(#class,'ui-select-choices-row') or contains(#id,'ui-select-choices-row')]//*[text()='TextofMyElementToBeclicked'
Additional info: This is a Jquery drop down on an angularJS application. When the user clicks on the drop down //ul is loaded and i am using the above xpath (which is part of //ul) to select the element based on text (using Javascript executor click). I used JS executor because, click() function in selenium simply could not click on the drop down element.
I am clicking element using below.
WebElement element = driver.findElement(By.xpath("YourNumbersXpath"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", element);
enter code here
I successfully tested your XPath with IE11, so it's not an issue related to IE. It's most likely a timing issue. First click on the drop button, then wait for the targeted element to appear and finally click on it:
WebDriver driver = new FirefoxDriver();
WebDriverWait wait = new WebDriverWait(driver, 30);
driver.get("...");
// move the cursor to the menu Product
WebElement element = driver.findElement(By.xpath("drop down button")).click();
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("drop down item"))).click();
IE11 seems to struggle with contains(#class and possibly also the contains(#id. Try using alternative solutions like starts-with.
i am using selenium page object model below is the problem scenario
i am clicking on an element using below command
driver.findElement(By.xpath("xxxxxxxx")).click();
when code execute the above line , click has been performed in the browser and a new page starts loading but code stuck at the above line untill the whole page loads.
i am not able to perform any operation on any webelemnet untill the page loads completely.
below is the code:
WebElement element = driver.findElement(By.xpath(".//*[#id='lll-menu-1']/div[1]/ul/li[4]/a"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", element);after this line code stuck here until the page loads completely
WebElement elem = driver.findElement(By.xpath(".//img[#alt='Cool Racerback - regular']"));
elem.click();
You can try with:
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("YOUR ELEMENT XPATH")));
I don't think it is possible, you have to just wait for the page to load as this is built into webdriver.
I doing automation on a particular website(say xyz.com). When I open the URL manually, it lands me onto a login page as expected and I am able to login there as well.
However, when I am automating the scenario by creating new instance of Firefox using new FirefoxDriver(), login page opens quickly but; when I click on login button it takes almost 2 minutes to navigate to a homepage.
I tried using a new profile but it didnt help.
I am using Selenium 2.44.0 on MAC with Java(Eclipse).
Please help.
I had the same problem with Selenium. What I ended up doing was making the webdriver wait till the page title changes(to homepage) using Expected Conditions.
WebDriverWait wait = new WebDriverWait(driver, 15);
wait.until(ExpectedConditions.titleContains(": My Expected Page title"));
I would suggest you to have a look here:
driver.wait() throws IllegalMonitorStateException
Wait for page load in Selenium
I am new in Selenium. I am using Selenium WebDriver with Java. I'm using eclipse as IDE. I have written some code for Login page and it is run successfully. Now I want to go to desired page after successful login, but I want to wait for few time before transiting another page. How can I wait a page before loading another page?
As far as I know, there are 3 ways:
Implicit wait: (It's applicable for all elements on the page)
driver.manage().timeouts().implicitlyWait(A_GIVEN_NUMBER, TimeUnit.SECONDS);
Explicit wait: (Applicable for a particular element)
WebDriverWait.until(CONDITION_THAT_FINDS_AN_ELEMENT);
More specific code is as below:
WebDriverWait wait = new WebDriverWait(driver, 40);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.id("someid")));
Using Thread:
Thread.sleep(NUMBER_OF_MILLIS);
I really would advise against using Thread.sleep(NUMBER_OF_MILLS). It will not be stable and you will hit occasions when the sleep is not long enough.
If you are simply waiting for the DOM to load, then a WebDriver event which triggers page load will always wait for the DOM to load before returning control.
However, if AJAX is used to change the HTML after DOM, then I would advise you to use WebDriverWait, and wait until a known event happens (e.g. Object appears in html, text changes, etc.)
If you take one thing away from this post, then please stop using Sleep!
Use class WebDriverWait
Selenium explicit / implicit wait
You can wait until the element you are expecting on the next page comes up. :
WebDriver _driver = new WebDriver();
WebDriverWait _wait = new WebDriverWait(_driver, TimeSpan(0, 1, 0));
_wait.Until(d => d.FindElement(By.Id("Id_Your_UIElement"));
Try by using implicitlyWait for 60 sec. as follows in Selenium WebDriver:
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);//wait for 60 sec.
If you need to wait more make it 80, 90 and so on.
For Selenium RC you can use the code as below:
selenium.waitForPageToLoad("60000");//wait for 60 sec.
It can be done by using Thread as below:
Thread.sleep(NUMBER_OF_MILLIS);
For explicit wait in WebDriver, identify an element in loading page and write the code as below:
WebDriverWait wait = new WebDriverWait(driver, 40);//wait for 40 sec.
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.id("someid")));
I have used this approach. Try to figure out which element on the page is the last to load. By taking the locator of that element and checking it's existance using isDisplayed, you will be able to see when the entire page loads
Implicit Wait:
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
Explicit Wait:
WebDriverWait wait = new WebDriverWait(driver, 40);
This code is deprecated in Selenium 4.
Instead, use this,
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(60));
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(40));