i want to print html page source in output. In selenium IDE there is a command to perform this action.
storeHtmlSource
How to do the same in Selenium Webdriver(Java)?
While exporting the testcase, It shows the following error.
// ERROR: Caught exception [ERROR: Unsupported command [getHtmlSource | | ]]
Using Selenium WebDriver:
WebDriver driver = new ChromeDriver();
driver.get("your Url here");
String htmlSource = driver.getPageSource();
Api doc to get the page source
https://seleniumhq.github.io/selenium/docs/api/java/
Related
I am testing a website in linux host.The page i am accessing loads infinitely so I am trying to set pageLoadTimeout for selenium.
Firefox is triggered correctly but URL is not loading/navigating/added in url bar.just blank firefox window.I am not seeing any errors also.
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().pageLoadTimeout(2, TimeUnit.SECONDS);
driver.get("http://www.example.com");
However if I remove driver.manage().timeouts().pageLoadTimeout(2, TimeUnit.SECONDS); code is working fine
Selenium version : 3.14.0;
gecko driver : 18 - linux (tested with gecko 16,17 also same issue)
browser : firefox-52
os/platform : linux
If this kind of some issue how do I make sure my driver quit itself after 5 minute.Host will support only firefox 52.
I checked this link but doesnt fix my problem.
Thanks
Jk
You can set the pageload strategy for browser which will then make the page not wait for the full page load for your other Selenium commands to be executed. Below is the sample code snippet in Java. There are three supported values:
normal
This stategy causes Selenium to wait for the full page loading (html content and subresources downloaded and parsed).
eager
This stategy causes Selenium to wait for the DOMContentLoaded event (html content downloaded and parsed only).
none
This strategy causes Selenium to return immediately after the initial page content is fully received (html content downloaded).
By default, when Selenium loads a page, it follows the normal pageLoadStrategy.
DesiredCapabilities caps = new DesiredCapabilities();
caps.setCapability("pageLoadStrategy", "eager");
FirefoxOptions opt = new FirefoxOptions();
opt.merge(caps);
WebDriver driver = new FirefoxDriver(opt);
driver.get("https://www.google.com/");
If you are interested only in the HTML of the page, better use the "eager" strategy.
You haven't mentioned the url you are trying to access but pageLoadTimeout for Selenium works as expected with With Selenium v3.14.0, GeckoDriver v0.23.0 and Firefox Quantum v62.0.3 combination. I am able to see the expected output on the console with the following example which prints TimeoutException occurred. Quiting the program whenever the pageLoadTimeout is triggered:
Code Block:
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.TimeoutException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class A_Firefox_Test
{
public static void main(String[] args)
{
System.setProperty("god.bless.us", "C:/Utility/BrowserDrivers/geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().pageLoadTimeout(2, TimeUnit.SECONDS);
try {
driver.get("https://www.booking.com/hotel/in/the-taj-mahal-palace-tower.html?label=gen173nr-1FCAEoggJCAlhYSDNiBW5vcmVmaGyIAQGYATG4AQbIAQzYAQHoAQH4AQKSAgF5qAID;sid=338ad58d8e83c71e6aa78c67a2996616;dest_id=-2092174;dest_type=city;dist=0;group_adults=2;hip_dst=1;hpos=1;room1=A%2CA;sb_price_type=total;srfid=ccd41231d2f37b82d695970f081412152a59586aX1;srpvid=c71751e539ea01ce;type=total;ucfs=1&#hotelTmpl");
} catch (TimeoutException e) {
System.out.println("TimeoutException occurred. Quiting the program.");
}
driver.quit();
}
}
Console Output:
1539157195615 Marionette INFO Listening on port 1920
Oct 10, 2018 1:09:56 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C
Oct 10, 2018 1:10:00 PM org.openqa.selenium.remote.ErrorCodes toStatus
INFO: HTTP Status: '500' -> incorrect JSON status mapping for 'timeout' (408 expected)
TimeoutException occurred. Quiting the program.
You can find the detailed stack trace in pageLoadTimeout in Selenium not working
You can find the Pythonic approach to pageLoadTimeout in How to set the timeout of 'driver.get' for python selenium 3.8.0?
I am trying to detect the login id and password field of a website : https://mretailstore.com/login but seems selenium is not able to locate the email type box. I have checked stackoverflow but didn't get any solution to this. Someone has used iframe because of what he/she was facing the same issue but here we have not incorporated any iframe.
The error I am getting is:
Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element: .//*[#id='identity']
The code I am using:
System.setProperty("webdriver.gecko.driver", "C:\\Users\\MI SERVICE\\Downloads\\geckodriver.exe");
FirefoxOptions capa = new FirefoxOptions();
capa.setCapability("marionette", true);
WebDriver driver = new FirefoxDriver(capa);
driver.get("https://www.mretailstore.com/");
driver.findElement(By.xpath(".//*[#id='identity']")).sendKeys("abc#d.com");
driver.findElement(By.xpath(".//*[#id='password']")).sendKeys("abc123");
driver.findElement(By.id("loginbutton")).click();
driver.navigate().back();
driver.close();
It looks your xpath is correct only and this exception is happening before element rendering.So, Please add the some explicit wait after the page loading.
It is working for me with/without Explicit Wait.
Code:
driver.get("https://www.mretailstore.com/");
WebDriverWait wait=new WebDriverWait(driver,20);
wait.until(ExpectedConditions.titleIs("Login"));
driver.findElement(By.xpath(".//*[#id='identity']")).sendKeys("abc#d.com");
driver.findElement(By.xpath(".//*[#id='password']")).sendKeys("abc123");
driver.findElement(By.id("loginbutton")).click();
I'm doing some R&D with Selenium and I've found an inconsistancy I can't explain between the InternetExplorerDriver and the ChromeDriver.
I run this code:
var driver = new ChromeDriver();
driver.Navigate().GoToUrl("http://www.google.com");
var query = driver.FindElement(By.Name("q"));
query.SendKeys("Hello World!");
query.Submit();
Everything works fine, my browser navigates to google and searches for "Hello World!".
But when I try exactly the same code with the InternetExplorerDriver
var driver = new InternetExplorerDriver();
driver.Navigate().GoToUrl("http://www.google.com");
var query = driver.FindElement(By.Name("q"));
query.SendKeys("Hello World!");
query.Submit();
I get an error on the FindElement line.
An exception of type 'OpenQA.Selenium.NoSuchWindowException' occurred
in WebDriver.dll but was not handled in user code
Additional information: Unable to find element on closed window
I have IE 11.0.9600.17905 installed.
Am I missing a step with IE?
Check out the required configuration section on the IEDriver wiki. I would hazard a guess and say your problem is "you must set the Protected Mode settings for each zone to be the same value".
I am trying to upload a file using Safari Driver.
Here is my code:
DesiredCapabilities browserCapabillities = DesiredCapabilities.safari();
RemoteWebDriver driver = new SafariDriver(browserCapabillities);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("myAppURL");
WebElement upload = driver.findElementByXPath("//input[#id='fileElementId']");
RemoteWebElement webElement = ((RemoteWebElement) upload);
LocalFileDetector detector = new LocalFileDetector();
webElement.setFileDetector(detector);
File f = detector.getLocalFile("myFilePath");
upload.sendKeys(f.getAbsolutePath()); // Generating exception:
// org.openqa.selenium.WebDriverException: Unknown command: uploadFile
driver.findElement(By.id("uploadButton")).click();
Only thing that is working for me right now is AppleScript. Thanks to Using AppleScript to choose a file in Safari. But with Apple Script I had to keep my machine unlocked.
I feel LocalFileDetector is a better solution as I would like to run my tests even when the machine is locked.
I am not sure whether the following helps ?
driver.setFileDetector(new LocalFileDetector()); // I am getting
// org.openqa.selenium.WebDriverException: Setting the file detector only
// works on remote webdriver instances obtained via RemoteWebDriver
Change
RemoteWebDriver driver = new SafariDriver(browserCapabillities);
to
RemoteWebDriver driver = new RemoteWebDriver(urlofhub,browserCapabillities);
Refer this post for an example code.
Below is my code to launch IE browser and http://google.co.in page.
File file = new File("C:/IEDriverServer/IEDriverServer.exe");
System.setProperty("webdriver.ie.driver", file.getAbsolutePath());
driver = new InternetExplorerDriver();
baseUrl = "https://myruat.corp.webex.com/US/buy/signup.html";
driver.manage().timeouts().implicitlyWait(120, TimeUnit.SECONDS);
I am getting this below error:
4235 [main] INFO org.apache.http.impl.client.DefaultHttpClient - I/O exception (org.apache.http.NoHttpResponseException) caught when processing request: The target server failed to respond
4235 [main] INFO org.apache.http.impl.client.DefaultHttpClient - Retrying request
Please let me know how to rectify.
did you set InternetExplorerDriver in your path environment variable?
System.setProperty("webdriver.ie.driver", "D:\\ImportantSeleniumTools\\IEDriverServer.exe");
WebDriver driver=new InternetExplorerDriver();
In System.setProperty give the location where you have kept InternetExplorer server file.
Use this...
// Simply give the path in the setProperty
System.setProperty("webdriver.ie.driver", "absolute_path");
//(absolute_path = Full path to the exe file of Internet Explorer)
// Ex : System.setProperty("webdriver.ie.driver", "C:\ \testing\ \IEDriver.exe");
// Use double slash instead of single slash.
WebDriver driver = new InternetExplorerDriver();