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.
Related
I am using selenium for my testing a retail website . Once I reached the Checkout page , I am selecting the option as Paypal Where a sandbox url is opening.
https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=EC-07L974777B231831F#/checkout/login
I am able to enter the username and password , clicked on Login button.
After that I am redirected to "Agree & Continue " Page . Where I could not perform any action.
I could see clearly the button properties as below
I have tried the below code, but could not perform any action.
WebElement AgreeandContinue= driver.findElement(By.tagName("input"));
AgreeandContinue.click();
You might have many input elements in the same page. What about trying to select by class or id?
WebElement AgreeandContinue= driver.findElement(By.ByClassName('btn'));
or
WebElement AgreeandContinue= driver.findElement(By.ByClassName('continueButton'));
then using submit() instead of click() as the element is a 'submit' type`
FYI : https://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/WebElement.html
Looks like the button has an id defined, so that would be the best locator to use: driver.findElement(By.id("confirmButtonTop));
If that doesn't work, then you might have to add some waits for the button to be clickable.
And if that still doesn't work, then it's possible, as it is with many commercial tools, that the button is actually inside a different iframe. Look further up in the html to confirm if this is the case (it'll have a iframe tag). If that's the case, then you'll have to first switch to the iframe before clicking the button: driver.switchTo().frame(...) . How to identify and switch to the frame in selenium webdriver when frame does not have id
If we look at the HTML you have provided the WebElement with value Agree & Continue is within an <input> tag. So we have to construct a unique css or xpath to identify the WebElement as follows:
cssSelector :
WebElement AgreeandContinue= driver.findElement(By.cssSelector("input#confirmButtonTop"));
OR
xpath :
WebElement AgreeandContinue= driver.findElement(By.xpath("//input[#id='confirmButtonTop']"));
Try with ID, but if it is not you can try with other locators , I suggest you have to use xpaths:
driver.findElement(By.xpath("//input[contains(#id,'confirmButtonTop')]")).click();
or
driver.findElement(By.xpath("//*[contains(#id,'confirmButtonTop')]")).click();
also I will suggest that you can use wait until element to be clickable or visible
WebDriverWait wait = new WebDriverWait(driver, 15);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//input[contains(#id,'confirmButtonTop')]")));
driver.findElement(By.xpath("//input[contains(#id,'confirmButtonTop')]")).click();
or
WebDriverWait wait = new WebDriverWait(driver, 15);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[contains(#id,'confirmButtonTop')]")));
driver.findElement(By.xpath("//*[contains(#id,'confirmButtonTop')]")).click();
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));
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.