hoping someone can help me here, I have a website that takes a while to load, in order to successfully load all images on the page, I need to keep scrolling down while the page is still loading. I can not wait for the page to load first and then scroll.
I am aware of using the following to scroll to the bottom of the page, I have tried running this on a separate thread in a loop prior to and after getting the url with driver.get("SITE HERE"); to try and keep the page scrolling to the bottom until it has fully loaded.
JavascriptExecutor js = ((JavascriptExecutor) driver);
js.executeScript("window.scrollTo(0, document.body.scrollHeight)");
This has not worked and I am at a loss on how to get selenium to scroll a page while it is still in the process of loading.
The webdriver get(String url) command states in its documentation that it will block until the page is loaded. If you call that you won't be able to try to scroll. I haven't ever tried it myself but you could try to load the page via JavaScript instead but it's not a great practice as you could very well end up with issues regarding the script context if you tried to loop in the same script call and could have timing issues if in a different script call (from the web driver). My first recommendation would be to talk to the page developer and make it so that scrolling after page load works correctly
Related
In few websites few scripts might take some time to run which results the website scraping to work inefficiently or the html which is returned from the scraper is incomplete.How to scrape the website once the site scripts are fully ran.
I am using URL Connection in java when I am reading the text from it I am getting HTML which is pre matured (i.e) I have script which is a bit long which takes some time to load which changes color of the text which is not reflecting in the text which is read using URL CONNECTION.
You can use PhantomJS. It's a browser but headless. It will render all js on the page. You might find this thread useful Any Java equivalent to PhantomJS?
I have used Selenium in java (and kotlin using the java libarary) to do website automation and testing
it can be set up to wait a specified time before looking for elements or wait until it is loaded, since it really just remote controls a webbrowser you can use javascript on pages and act just like any user would
https://www.seleniumhq.org/download/
https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java
RemoteWebDriver driver = new ChromeDriver()
driver.get(url)
driver.findElement(by.name("search")).sendKeys("some query")
driver.find(by.id("submit")).click()
you can wait for all things to load as described here
https://stackoverflow.com/a/33349203/9006779
(or at least in a similar way, the api might have changed)
I am trying to get Selenium WebDriver to find and use a browser window that is already open before the script executes. I am writting in Java.
I am using selenium-server 2.37.0 and the browser is IE8. I am open to using the Chrome browser as well.
Anyway, I have tried opening a driver instance and then looking for the window handles in the usual way (Set handles = driver.getWindowHandles();) but this only finds the hadle of the window that the driver opened. I have also just tried to switchTo the window by the window cannot be found.
The reason I want to use a pre-opened window is because when I execute my script, for some reason the browser won't let it click a link (It may be because the link is to an https address and sends a username and a token). When the script finishes the webpage won't respond to me manually clicking the link either.
I am able to manualy navigate to the link and click it the link works fine, so my thinking is that I can navigate to the page that I want and then kick of the scripts from there, but I need the webdriver to use this browser window that I used.
I cannot navigate the to link directly with Selenium because the link resided behind a secure server. I have to log in first then click the link and this is where I am having the problem.
Unfortunately, as of 2.37.1 (December 2013), it cannot be done.
There is an official feature request for this in the Selenium project (and it's even the most starred one), but it has not been done yet.
You can identify a browser window by windowHandle and switch between several Windows while testing.
You can print out all existing windowHandles (in your case it might be one) and then access it.
This solution worked for me (using Selenium 3.4.0):
Object[] handles = driver.getWindowHandles().toArray();
String windowHandle = handles[0]+"";
driver.switchTo().window(windowHandle);
After switching to your browser window you should be able to continue your test.
It relys on correctness of your webdriver- if you opened an Edge window the driver you use should be an EdgeDriver.
I am using the JQuery UI datepicker plugin to dynamically change some page content. I get HtmlUnit to click on the text input, which should make the datepicker popup appear. However when I call driver.findElement(By.id("ui-datepicker-month")) I get an exception - it seems anything which is newly added to the DOM by JS does not become visible to HtmlUnit.
I'm not sure if there is any way around this, but it is worth noting when I use Chromedriver I don't have this problem - I am able to find the new elements. Also the getSource() method only ever returns the original source code with HtmlUnit whereas with Chromedriver it returns the updated source.
Is there any way to support the JS using Html Unit?
I have written a script for datepicker to choose dates from the calendar. The scripts are running fine in local, but when I run it through jenkins the script is getting failed.
action.moveToElement(driver.findElement(By.xpath("//*[#id='ui-datepicker-div']/div[1]/div/a/span")));//locating the element to click
action.perform();
action.click(driver.findElement(By.xpath("//*[#id='ui-datepicker-div']/div[1]/div/a/span"))); //this line is not executing
action.perform();
The script to click the element is not working. I am getting error as "Element is not currently visible and so may not be interacted with"
I have also tried driver.findElement(By.xpath("//*[#id='ui-datepicker-div']/div[1]/div/a/span")).click() by replacing action.click() but still no use.
I have faced a similar issue and after a couple of frustrating hours, I have figured out, that im my case only one thing has worked for me - JavascriptExecutor.
I don't know why all other attempts have failed(all of them have worked well locally). It seems like Jenkins specific issues.
Anyway you can use this code snippet:
WebElement elem = driver.findElement(By.xpath("//path/to/element"));
JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript("arguments[0].click();", elem);
Note: in my case I was always able to send click action to element, but somehow browser didn't react on this action. So element was remained unclicked without any error.
You cannot click on the hidden element using selenium because if will throw the exception that you saw. You should either make element visible (in the way the user does so) or use javascript to click (see JavaScript executor).
Clicking on an element works fine locally but not in Jenkins;
first, I was locating the web element using XPATH, and when it came to clicking I tried selenium click, actions click, js click. All were working locally But not in Jenkins.
Finally what worked for me was the combination of css selector with javascriptExecutor click. This solved my problem. Now works both locally and in Jenkins.
So try the same.
I've recently updated from HTMLUnit 2.4 to 2.5 (we'd go for the latest version but there is a lot of code to refactor due to the deprecated APIs). I'm now having a problem with some JavaScript that opens a window.
The page under test, is a 'Please wait while loading screen' for reports. The page opens a new window then redirects back to page that originally launched the print.
So the JavaScript looks something like:
window.open(url,'report_window');
document.location.href = original_url;
With HtmlUnit 2.4 the script would continue executing and if I grabbed the original Window object it would have performed the redirect. However, after upgrading to HtmlUnit 2.5, the original window is still on the 'Please wait' page - the redirect is never executed. It appears as though the JavaScript stopped executing after the call to window.open.
I have confirmed the page behaves correctly if I test manually. I've also tried different JavaScript after the window.open call to confirm that that particular call is not the issue.
Is anyone aware of an issue like this and any potential workarounds? We have to stay on HtmlUnit 2.5 because of jQuery compatibility.
I was able to fix this issue by removing the CurrentWindowTracker object from the web window listeners on the WebClient object. Unfortunately, this field is completely encapsulated, so I had to retrieve it via reflection.
Field windowListeners = WebClient.class
.getDeclaredField("webWindowListeners_");
windowListeners.setAccessible(true);
Collection webWindowListeners = (Collection) windowListeners
.get(webClient);
webWindowListeners.clear();