How to avoid pop-up appearing for a second randomly on different pages - python-3.x

I write UI tests using Selenium Webdriver 2.0 with Python 3.6. A popup appears randomly, that causes focus to be lost, causing my test to fail. It appears for one second, randomly on different pages, and doesn't exist in the DOM, how to win it?
What I've tried:
Try/except
Wait for element
Sometimes these methods help but it's unreal to add it on every step to wait or catch that element
I expect my tests shouldn't fail because of that popup.
here it is

Related

Selenium/ python performing a click with browser.execute_script VS. normal click line

I wanted to click on something in a webpage so I used
WebDriverWait(browser, 30).until(EC.presence_of_element_located((By.CSS_SELECTOR, "#styleguide-v2 > div.banner-container > a:nth-child(2)")))
except that it doesn't work in the background.I have to switch to the browser manually where it to be seen on my screen so that the code works properly.
Then I added this
x = WebDriverWait(browser, 30).until(EC.presence_of_element_located((By.CSS_SELECTOR, "#styleguide-v2 > div.banner-container > a:nth-child(2)")))
browser.execute_script("arguments[0].click();", x)
now it works like charm, my question is what's the difference? I want to know what's happening behind this
the webpage https://www.imdb.com/title/tt0198781/
presence_of_element_located expected condition finishes and the program continues to the next call while the element already created but still not clickable and still not located on it's final position on the page and still not ready to accept regular click.
JavaScript click can handle this kind of click, however this doesn't really imitates real UI user action.
To mimic real user action you should use element_to_be_clickable expected condition and click the element only when it became clickable.
visibility_of_element_located didn't work because the element is not actually visible itself, so we had to use element_to_be_clickable expected condition.
It is also possible that element is covered by some other element during the page rendering when it is literally become clickable but the page is still rendered. In this case we have to add some hardcoded delay or to wait until the element covering the desired button is disappeared. this can be achieved by invisibility_of_element_located expected condition for the covering element.

StaleElementReferenceException when iterating through dynamical iterable

I want to scroll down the website by looping through all webElements and I'm doing it in this way:
driver.get('https://justjoin.it/')
driver.maximize_window()
while True:
for web_element in driver.find_elements_by_class_name('css-1x9zltl'):
driver.execute_script("arguments[0].scrollIntoView();", web_element)
Unfortunately after few seconds scroll bar stops and console throws me this exception:
Message: stale element reference: element is not attached to the page document
Looking for solutions I searched a lot of webpages and now I know why I have this type of error. I tried to solve that by using try/except and it works but of course it doesn't solve the essence of the problem. I want to know Is there a better way to solve my issue?
Perhaps you're calling find_elements_by_class_name() before your justjoin.it page is loaded and "stable". Then, while you're looping through the elements you collected, the page continues loading and invalidates those collected element objects. I would try waiting longer between calling driver.get() and calling find_elements_by_class_name().
Stale element exception occuring when element in on page but selenium driver instance could not interect with that element.
Following actions can be resolve stale element exception
1.Refresh page by using "navigate(). refresh()" method in selenium
2.using loop try to click or check visible of that element if that element visible or already clicked exit from loop

Any new arrangement for select_list in WATIR 6.0.2 waits until options got populated?

I have a question about select_list which got populated in run time according to the option of some other select list.
For an example If I choose motor car make in my select_list, then model select_list got populated. but the problem is, I have to wait for few seconds before I interact with model select_list otherwise it throws the error no options present(because it takes some time to populate). I find very interesting thing in WATIR 6.0.2 like
b.link(:id,'NewContactNewGenFromMenu_Link').wait_until(&:visible?).click
which first confirm the element present through implicit wait using when_present, then it waits until element visible, this is amazing change. But Is there anything to check whether select list options got populated? Is any new arrangement?
It looks like there is no change to the Select#select method in Watir 6.0.2 - ie it does not wait:
browser.select_list.select('changed_text')
#=> Watir::Exception::NoValueFoundException
You can get an implicit wait if you locate/select the option directly:
browser.select_list.option(text: 'changed_text').select
I think the wait functionality should be added to the #select and #select_value methods. I have opened Issue 503 to request the functionality.

Watir: Always wait when webpage element present

A web application consistently uses this div after user actions. My test scripts have this line. verbatim, all over the place.
#browser.div(:id => 'loading-indicator').wait_while_present
Explicitly including this line everywhere is OK, but ugly and repetitive.
Using wait_until_present with the next element ("foo") after the loading indicator would sometimes work. However, in some cases foo and the loading indicator are present simultaneously, but foo is not visible? until the loading indicator disappears.
Is there a way, perhaps in env.rb, to indicate to watir-webdriver that it should always wait_while this element is present?
If you want to check for it after every click and page load/reload you can add it to an After Hook
browser.after_hooks.add do |b|
b.div(id: 'loading-indicator').wait_while_present
end

Capybara Cucumber test fail to find xpath when browser is minimized

I'm running a scenario where form fields are automatically filled in with invalid values which trigger some javascripts to show warnings under each incorrectly filled field when I blur.
The test passes when the browser is in focus. It finds the xpath with the "expected warning" that I pass. But if I minimize or just click on another application, it fails to find the xpath.
I'm running Firefox 3.6 (going to update it soon) and the way I'm doing to find the xpath is by using "page.should have_xpath(xpath)"
Does anyone have any idea how what might solve this? It's really important for me to run it with the browser minimized.
Edit and alternative solution:
I guess the timing issue that occurs in events such as blur followed by finding a certain xpath in a minimized browser inherent to the driver itself. Therefore, I decided to run the tests in a Virtual Frame Buffer using xvfb in Linux and it seems to be working really well. I'm planning on applying this to be triggered by Hudson/Jenkins whenever a change is committed.
Could it be a timing issue? Perhaps if the browser isn't frontmost and maximised, the rendering is not happening quickly enough for the content be present when Capybara checks for it.
Also: How are you triggering the blur event?

Resources