I am new to selenium and building a project (python + selenium). I have inserted a custom wait in code like time.sleep(10)
The thing i am trying to impliment is the code should be block untill a page has been loaded fully and same after button clicks.
I have gone through few reading like
implicit wait
explicit wait
wait untill an element appears up (select by id or something else )
Is there any way to block the code untill a page has been loaded fully. (I do not have any condition upon which i can wait for wait untill). I can not use stuffs implicit wait or explicit as there is no fix time for completion of loading a page
def run(self):
self.browser.get('url')
# here it should wait untill the page has been loaded fully
time.sleep(10)
element = self.browser.find_element_by_css_selector('some-css-selector')
https://www.selenium.dev/documentation/en/webdriver/page_loading_strategy/
normal This will make Selenium WebDriver to wait for the entire page
is loaded. When set to normal, Selenium WebDriver waits until the load
event fire is returned.
By default normal is set to browser if none is provided.
so this is done automatically as page load strategy is normal by defaul ,
But it won't consider asynchronous elements taht loads after the page get loaded , if you want to explicitly wait for some asynchronous element use explicit wait as:
WebDriverWait(driver,15).until(EC.presence_of_all_elements_located(By.CSS_SELECTOR,"some-css-selector"))
Related
Is there a code in selenium python that says wait for all elements on page to load?. If so what is the code?.
webdriver will wait for a page to load by default.
It does not wait for loading inside frames or for ajax requests. It means when you use .get('url'), your browser will wait until the page is completely loaded and then go to the next command in the code. But when you are posting an ajax request, webdriver does not wait and it's your responsibility to wait an appropriate amount of time for the page or a part of page to load; so there is a module named expected_conditions.
Use the following ex with selecting specific element in the page to wait:
try:
element_present = EC.presence_of_element_located((By.ID, 'element_id'))
WebDriverWait(driver, timeout).until(element_present)
except TimeoutException:
print "Timed out waitingenter code here"
Is there a way to reliably wait for pages to load when using Behat with Mink using the Selenium2Driver?
I've inherited some legacy tests that wait like this:
Background:
Given I am on "http://test.example.com"
And I wait for "20000"
Given I click on the element with css selector ".button1"
And I wait for "30000"
Given I click on the element with css selector ".button2"
And I wait for "30000"
Given I click on the element with css selector ".button1"
And I wait for "10000"
i.e. just this single test takes 1minute 30seconds.
What I'd like to do is have a generic way of waiting for the previous click to result in a page load, without having to wait a large fixed amount of time each time.
All of the suggestions I can see for waiting for page load, all refer to checking that a particular page element is loaded.
However these tests run against a variety of legacy websites, where there isn't always a standard element that can be checked to be present, so I'm hoping to use a more generic method.
You can use a wait method with javasccript condition like:
/**
* #When /^wait for the page to be loaded$/
*/
public function waitForThePageToBeLoaded()
{
$this->getSession()->wait(10000, "document.readyState === 'complete'");
}
Another good practice is to have a method that waits for the element, if element is found returns the element object else it will throw an exception.
For the click method you can have something like this:
$this->waitForElement("css_selector")->click();
Implicit Wait in selenium waits for the page to get loaded or all the elements to get loaded.
suppose there is a web page, where the page is loaded while some of the web elements are still loading. So in this scenario if we use Selenium Implicit wait, it will wait for the page loading or for all the elements to get loaded.
Please help with this confusion.
Actually none of them ;)
You can think that implicit wait is something similar to a "timeout" that is aplied to every element you want to interact with (using findElement()) before throwing an exception, even the page is completly loaded or not.
Have a look to official doc.
I have a problem in my framework that instead of using static sleeps I try to wait for a visibility of an element. The thing is that visibilty of element checks the presence of an element on the DOM, that will return true but in my system the page is not fully loaded yet. What happens is that as soon as I get true when checking the visibility of element I set values. These values get reset when the actual page get fully loaded.
My question is what can I use instead of static sleeps to wait for the actual page (not only the DOM) to get fully loaded as visibility of element is not working for me?
P.S. I'm using Selenium webdriver with python 2.7
/Adam
The expected_conditions.visibility_of_element_located(locator) method will check for both - the presence of the element in the DOM, and its visibility (element is displayed with height and width greater than zero).
Ideally, the driver.get(url) method should automatically wait for the full page to be loaded before moving on to the next line. However, this might not behave as expected, in case, the web application being tested uses ajax calls/actions (as although the page has loaded but the ajax actions are still in progress). In such scenario, we can use something like below to wait for stability before performing action(s) on the desired webelements.
# create the firefox driver
driver = webdriver.Firefox()
# navigate to the app url
driver.get('http://www.google.com')
# keep a watch on jQuery 'active' attribute
WebDriverWait(driver, 10).until(lambda s: s.execute_script("return jQuery.active == 0"))
# page should be stable enough now, and we can perform desired actions
elem = WebDriverWait(driver, 10).until(expected_conditions.visibility_of_element_located((By.ID, 'id')))
elem.send_keys('some text')
Hope this helps..
Try ExpectedConditions.elementToBeClickable.
See: https://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/support/ui/ExpectedConditions.html#elementToBeClickable-org.openqa.selenium.By-
I need to click on the dynamically generated element using Selenium WebDriver (Java API).
I'm waiting for this element to appear with WebDriverWait and then clicking on it. This click succeeds but the following click on the different static element freezes the whole test. Here is the code:
webDriver.get(alfrescoURL + "/share/page/create-document");
WebDriverWait wait = new WebDriverWait(webDriver, 10);
WebElement documentTypeList = webDriver.findElement(By.id("template_x002e_create-document_x002e_create-document_x0023_default_documenttype-selected-form-button-button"));
documentTypeList.click();
WebElement listItem = wait.until(
ExpectedConditions.visibilityOfElementLocated(By.id("yui-gen100")));
listItem.click();
// Choosing to create in new project
WebElement projectLink = webDriver.findElement(By.id("template_x002e_create-document_x002e_create-document_x0023_default_projecttype-entry1"));
projectLink.click();
documentTypeList.click() opens a drop-down list, listItem.click() chooses an item, projectLink.click() makes a choice in the group of radiobuttons. Test silently freezes on projectLink.click(). It looks like this click() infinitly waits for page reloading that happens by some reason while it shouldn't. (Disappearing of the list after choosing an item is made by javascript that doesn't make any AJAX requests.)
I think there is something about click() blocking i don't understand. It says in it's javadoc that it attempts to block only if it causes a page to load. Nevertheless here i get a block for some reason.
If i insert a thread sleep before projectLink.click() then test works fine. It agrees with a hypothesis that i get a infinite block on click().
Thanks in advance.
I've run into this before where the test runs faster than the drop down can contract and can't click the following element. Instead of using arbitrary sleeps (although in rare cases they are necessary), can you put in a wait for a class change in the drop down?
For example, if I want to wait for the drop down to contract before moving on, I'll wait for the class of the select to change from "active" to "closed". This, of course, assumes your HTML has these dynamic classes in place.
Another possibility is to set an implicit wait, giving yourself enough padding for instances like these:
driver.manage().timeouts().implicitlyWait(1000, TimeUnit.MILLISECONDS);
I would suggest that you try other click options too:
a) Actions#click()
b) Javascript click()
If any of those click works then it means the issue is with the selenium's WebElement Click method which needs to be reported.