how to parse pdf in selenium - pdfbox

I have been trying to read a pdf which is opened in browser. through the following selenium code.
URL pdfURL = new URL(driver.getCurrentUrl());
InputStream is = pdfURL.openStream();
BufferedInputStream fileToParse= new BufferedInputStream(is);
PDFParser pdfParser=new PDFParser(fileToParse);
pdfParser.parse(); // getting error here !!!
Getting Error:
java.io.IOException: Error: End-of-File, expected lineenter code here
How to get rid of this error?

Related

Selenium getting stuck on data:, (MS Edge)

I want to use Selenium to navigate to a website. Selenium opens up the browser but does not navigate further to the specified website URL, but gets stuck on the "data:," url. After some time time I get the following exception:
"The HTTP request to the remote WebDriver server for URL http://localhost:58504/session timed out after 60 seconds"
Note: I did not specify the 58504 port anymore, so I guess it is the default port that Selenium use?
I am programming in C# and using the following Nuget Packages:
https://www.nuget.org/packages/Selenium.WebDriver/4.1.1
https://www.nuget.org/packages/WebDriverManager/2.13.0
Here is the code:
using OpenQA.Selenium;
using OpenQA.Selenium.Edge;
using WebDriverManager;
using WebDriverManager.DriverConfigs.Impl;
using WebDriverManager.Helpers;
public void VisitWebsite()
{
IWebDriver driver = null;
try
{
new DriverManager().SetUpDriver(new EdgeConfig(), VersionResolveStrategy.MatchingBrowser);
EdgeOptions options = new EdgeOptions();
options.AddArgument("--no-sandbox);
options.AddArgument("--disable-infobars");
options.AddArgument("--disable-dev-shm-usage");
options.AddArgument("--disable-browser-side-navigation");
options.AddArgument("--disable-extensions");
options.AddArgument("--dns-prefetch-disable");
options.AddArgument("--disable-gpu");
options.AddArgument("--disable-software-rastersizer");
driver = new EdgeDriver(options);
}
catch (Exception ex)
{
throw ex;
}
driver.Navigate().GoToUrl("https://www.google.com");
}
Where does it go wrong? Thanks!
I found the answer. It seems the DeveloperToolsAvailability policy for the MSEdge browser had a value that "blocks" Selenium from working. You can read more about it here: https://learn.microsoft.com/en-us/microsoft-edge/webdriver-chromium/?tabs=c-sharp#developer-tools-availability-policy

org.openqa.selenium.WebDriverException: Invalid byte 10, offset 76

I am having a method which returns the web driver for firefox, while running the tests they are getting failed throwing the error
"org.openqa.selenium.WebDriverException: Invalid byte 10, offset 76."
On searching I couldn't find anything related to error.
Have pasted the code as well.
System.setProperty("webdriver.gecko.driver", "/Users/sdabral/Downloads/geckodriver");
final DesiredCapabilities capabilities = new DesiredCapabilities(DesiredCapabilities.firefox());
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("security.mixed_content.block_active_content",
false); //Required for browser mixed contents
capabilities.setCapability(FirefoxDriver.PROFILE, profile);
final WebDriver wd = new RemoteWebDriver(capabilities);
return wd;
The path to geckodriver is correct as referenced other answers related to WebDriverException.

Missing 'ELEMENT' when using switchTo().frame() in javascript

I'm using selenium webdriver with javascript on Chrome and I'm trying to interact with an input inside an iframe like so:
await driver.switchTo().frame(By.xpath('/html/body/div[1]/div/div/form/ng-form/div/ui-view/div/dqcomponent[3]/ng-form/div[1]/div[1]/iframe/html/body/form/input[1]'));
I am able to use .click() on the element just fine but the above line causes the following error:
(node:4514) UnhandledPromiseRejectionWarning: InvalidArgumentError: invalid argument: missing 'ELEMENT'
I have tried switching to the element using CSS but it produces the same error, how can I properly switch to said iframe?
My webdriver version is ^4.0.0-beta.1 and my chrome driver is ^88.0.0"
await driver.switchTo().frame expects an element not locator
Use it like
Elem = await driver.findElement(By.id("some if"))
await driver.switchTo().frame(Elem)

How to use gethtmlsource or storeHtmlSource in Selenium Webdriver(Java)?

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/

org.openqa.selenium.WebDriverException: Unknown command: uploadFile

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.