Conditional wait vs implicitlyWait -Selenium - selenium

please help me in understanding the following issue. please.
I have to get all links and check them later. I used the following code:
open(url);
List<String> links = new ArrayList<>();
for (SelenideElement link : $$("a"))
links.add(link.attr("href"));
when I used this with Linux with these api version:
Maven 3.1
Selenide v3.5
Selenium v2.53
Firefox v45.0.1
Then code can't take time enough to catch links from the page. Then I have to add driver wait before get links.
I Add the following (which is conditional wait):
WebDriverWait waitLog = new WebDriverWait(WebDriverRunner.getWebDriver(), 20);
waitLog.until(ExpectedConditions.visibilityOf($(By.tagName(Selector))));
And it worked fine and I run it more than one time.
I got surprised when running it yesterday, it didn't work and can't get time enough to get links!
So I replace the conditional wait with implicit wait, and add the following:
WebDriverRunner.getWebDriver().manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
Now it is working fine.
What happened?
What is this thing make it work sometimes and sometimes can't work?
How to return back to the conditional wait with keeping the code working well?
So how to recover this problem? and prevent this problem from happens in the future.

There might be some delay in loading the url for the second time. Please try increasing the delay time for conditional wait. The main difference between explicit and implicit wait is as follows.
Explicit or conditional wait stops the WebDriver for the specified amount of time, till the mentioned element is available. Whereas implicit wait will skip execution of WebDriver for specified amount of time, for every element which is not found on the page.
Hope this helps.

Related

C# Selenium WebDriver Hangs on same page

No matter if I do a FindElement. Or on the Wait.Until.
var wait = new WebDriverWait(Driver.Instance, TimeSpan.FromSeconds(10));
wait.Until(d => d.Title == pageTitle);
I get the following error:
OpenQA.Selenium.Remote.RemoteWebdriver.Title.Get timed out and needed to be apborted in an unsafe way. This may have corrupted the target process.
It says one way to fix it is to click on the 'enable property evaluation and other implicit function calls'...but that doesn't help. Just hangs and gives me a less detailed message. What could cause the hangup on Just trying to get the Title of a page? I need help to look in a new/correct direction, so far I have been hitting dead ends.
Chrome -- Version 76.0.3809.100
Selenium.WebDriver.ChromeDriver version 76.03770.....going to upgrade this one...to be closer
Giving Credit where Credit is due....I thought I had these synced up..... I did not..from one greg to another...thank you.
Usually these things are the result of a mismatch between the web driver version and the version of the browser you are interacting with. – Greg Burghardt

Protractor on top of my Selenium Code

I have my code written in Selenium for automating my application[Loading time of it varies] and it works fine when we use Thread.sleep for its loading time.I need to move out of thread.sleep as it is not ideal way of writing code,so i have tried to use all waits[implicit,explicit and fluent] given by Selenium.
In spite of that the script is not consistent and it is failing very often[mostly on element not found and stale element exception],so need to know since i have my all frameworks and code in Selenium is that any provision that i can import protractor jar file in it and use some lines of code in order to make my application to get synchronize with my script.
Your implicit wait is not gonna help , if your code works fine with Thread.sleep().
Thread.sleep(time) is an extreme case of Explicit wait.
An explicit wait is code you define to wait for a certain condition to occur before proceeding further in the code.
In Java selenium binding we can defined and use Explicit wait like this :
WebElement myDynamicElement = (new WebDriverWait(driver, 10))
.until(ExpectedConditions.presenceOfElementLocated(By.id("myDynamicElement")));
This waits up to 10 seconds before throwing a TimeoutException or if it finds the element will return it in 0 - 10 seconds. WebDriverWait by default calls the ExpectedCondition every 500 milliseconds until it returns successfully.
Note that : Do not mix implicit and explicit waits. Doing so can cause unpredictable wait times.
Question : mostly on element not found and stale element exception occurs?
Just verify your locators. Locators should be static not dynamic. Just for example you should never use dynamic ID of any web element which is generated by JS or any other programming language.
Using protractor, you can't just sync your script. All you can do now is to make changes to your locator and waiting time.
Hope this will help you.
It is not possible to use protractor into Selenium, but you can rewrite all tests into protractor and you will inherit all from selenium. Protractor is wrapper for Webdriverjs so you get each and every faeture of webdriverjs in protractor + protractor's own feature of locators and asynchronous waits.

How to manage time in selenium script to execute same script on different server

I'm automating functionality of My web project . I've created one test suit using selenium Webdriver which is working fine on Local Server(created on same). But while I do execute the same on Azure server(obviously slow as compared to local environment), The script get failed and I need to debug the whole script and need to put some wait where its taking time to locate the element. And after all changes on azure server some time it failed on local server too.
How to deal with slow website response ?
Is there any effective way of scripting which required less effort to execute the script on different environment ?
You can set Implicit wait (less desired) or Explicit waits.
var driver = new ChromeDriver();
driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(1000));
driver.FindElement(By.XPath("your xpath")).Click();
In this case, you're 'telling' Selenium to wait the specified timeout before it will search the element you want to be found. If the element was not found, you will get the Timeout error.
var driver = new ChromeDriver();
driver.FindElement(By.XPath("your xpath")).Click();
In this case you're telling Selenium to get the element automatically, so in case it will not be found, you will get a NoSuchElementException, with no timeouts because there's no time to wait before the element to be present.
And if we're expanding it to the Explicit wait:
var driver = new ChromeDriver();
var wait = new WebDriverWait(driver, new TimeSpan(0, 0, 1000));
wait.Until(d => d.FindElement(By.XPath("your xpath"))).Click();
In this case you're telling Selenium to keep searching for the element until it is found but no longer than the maximum timeout specified as a param to WebDriverWait.
Let's take for example the case where on your local environment a page loads on 1 minute and on Azure it takes 10 minutes (far stretched example), if you're going ahead with the Explicit wait, then you could set the timeout to let's say 11 minutes.
Those 11 minutes are the maximum timeout in which Selenium is polling for a specific element to be found. However, if you run your tests on the local environment and the element is found in 30 seconds, then your test will continue with the execution. It will not wait for the maximum timeout to complete before doing that.
It's most likely not time to locate the element, it's probably the speed of pages loading. Use WebDriverWaits for page transitions and any dynamic page changes. Once you wait for an element or dynamic section of the page to load, you can interact with the page without having to worry about speed. That will probably take care of most of your issues but without more specifics, it's hard to say what else might be going on.

How to `executeScript` before page load by WebDriver in selenium?

I want to use webdriver in nodejs to control a website which use ajax very heavy, especially it always have http request to server.
When I use driver.executeScript , I found the promise returned almost never resovled.
I checked the website, found that it use a keep-alive http request loop to communicate with server. That means, it will always have at least one live connection to server for 30s, then another connection for another 30s, again and again. which cause document.readyState keep interactive instead of completed, and then driver.executeScript almost be blocked forever.
I had tried driver.manage().timeouts().pageLoadTimeout(PAGE_LOAD_TIMEOUT_MS) but it only throws a exception after timeout.
I also tried to stop the connections by press ESC on the browser window. after that, it seems driver.executeScript can run immediately. But I didn't find any function like window.stop() in WebDriver API.
so is there have a way to resolve this problem? Either should be ok like:
run executeScript right now, regardless of whether page is loaded;
a webdriver api like driver.browser.stop() could stop all live connections in page.
Nodejs code to re-produce this problem:
const WebDriver = require('selenium-webdriver')
const driver = new WebDriver.Builder()
.withCapabilities(
WebDriver.Capabilities.firefox()
.set('webdriver.load.strategy', 'unstable')
).build()
driver.get('https://wx.qq.com/')
driver.executeScript('return "99% will be blocked, 1% luck to return"')
.then(function (ret) {console.log(ret)})
thanks!
UPDATE:
I found maybe set webdriver.load.strategy to unstable in firefox will be help.
FirefoxProfile fp = new FirefoxProfile();
fp.setPreference("webdriver.load.strategy", "unstable"); WebDriver
driver = new FirefoxDriver(fp);
https://github.com/seleniumhq/selenium-google-code-issue-archive/issues/4993
https://w3c.github.io/webdriver/webdriver-spec.html#page-load-strategies-1
Links from others got same problem:
http://grokbase.com/t/gg/webdriver/1263dbyws6/how-to-click-stop-button-or-equivalent
I think that if you don't use Implicit time the driver won't wait till it's loaded so you'll be able to execute your script.
If this don't work you can try to use your own timer. Check if doc state is ready, if it's not ready after some timeout then stop it manually (by JS) and run your script.
By using Thread.sleep(secs);. For example, we want to load a page in 3 secs means we write thread.sleep(30000);.

Selenium RC - t.replace is not a function

[edit for godman] I am working on a web based application, written in PHP. I am using Selenium RC to run tests on the webpages produced by this application, through a browser.
I just upgraded to Firefox 14, so I had to upgrade to Selenium RC 2.25.0.
Now I'm seeing this error when running a test with htmlSuite:
Command execution failure. Please search the user group at
https://groups.google.com/forum/#!forum/selenium-users for error
details from the log window. The error message is: t.replace is not a
function
The command executed is:
clickAndWait //a[text()='! selenium test customer']
As you can see, it's not doing anything too tricky - just clicking a link. The test runs fine in the IDE, it's just when run via RC that it's a problem.
Searching in the groups revealed only really old threads dating back to version 1 of selenium.
Any ideas anyone?
[edit] I've been running the test that has these problems several times a day for the last week - and it doesn't seem to fail like this every time. This looks like it's a random problem. Has anyone seen this happen before?
[edit after the bounty got me no answers] Another example of it failing is with:
clickAndWait css=a.edit_company
This same call works fine earlier in the same test, on the same page.
[edit] Now I'm seeing the same error with a completely separate command:
type id=Address1 Address1
Does anyone have any suggestions about this? Any way I can even debug what's going on (I don't see this in the IDE at all)
I am not terribly familiar with clickAndWait. I personally prefer clicking and then doing my own custom waiting.
Try just clicking and add a Thread.Sleep(5000), or something similar, and see if the error is a result of the click or the wait part.
If it is the wait part, then I have a different suggestion than if it is the click part.
I've seen similar issues across various platforms. It has always been somewhat random for me, so I don't use clickandWait. Generally is is much more reliable to do a plain click, and then wait for specific condition. You could do a waitforpageload, but that has also been unreliable for me so I prefer things like waitfortextpresent and waitforelementpresent.
Usually, this kind of error would occur if you are dealing with something that is not a string. Could you just make sure that you are dealing with strings only?
2 possibilities where it is arising from:-
When text() is executed -> it might be dealing with a non-string
a[expression] -> the expression(text() = '! selenium test customer') when evaluated to False/True might be the problem because if a is a Map/array, the corresponding key should be a string and not a bool, probably.
text()='! selenium test customer' -->> is it an assignment operation? if yes, make sure that text() returns a lvalue or a mutable object (based on the language you are using selenium rc with)