Selenium PDF Parser - selenium-webdriver

In my application I will land in one page there will be a text in there and I have to open a PDF in the same page and compare the text in the PDF and the main page matches.
I am using pdfbox -1.8.15
But when I run the below code getting this exception:
Exception in thread "main" java.io.IOException:
Error: End-of-File, expected line at
org.apache.pdfbox.pdfparser.BaseParser.readLine(BaseParser.java:1523) at
org.apache.pdfbox.pdfparser.PDFParser.parseHeader(PDFParser.java:372) at
org.apache.pdfbox.pdfparser.PDFParser.parse(PDFParser.java:186) at
TestCases.lastpdf.main(lastpdf.java:83)
driver.findElement(By.xpath("//*[#id=\"ctl00_cph_main_rg_QiSummaries_ctl00__0\"]/td[2]/img")).click();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
String winHandleBefore = driver.getWindowHandle();
for(String winHandle : driver.getWindowHandles()){
//Switch to child window
driver.switchTo().window(winHandle);
}
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
URL url = new URL(driver.getCurrentUrl());
System.out.println(url);
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
String mypdftxt;
BufferedInputStream fileToParse = new BufferedInputStream(url.openStream());
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
PDFParser parser = new PDFParser(fileToParse);
parser.parse(); // exception occurs here
mypdftxt = new PDFTextStripper().getText(parser.getPDDocument());
System.out.println(mypdftxt);

Related

Selenium Webdriver using java to dispaly browser automatically [duplicate]

This question already has answers here:
java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.chrome.driver system property
(4 answers)
Closed 4 years ago.
I was written this code to open Mozilla Firefox browser
package com.webdrivercommands;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class WDcommands_Test1 {
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
//Launch chrome Browser
//setproperty(method)
System.setProperty("Webdriver.gecko.driver", "C:\\Java Selenium\\geckodriver.exe");
WebDriver driver=new FirefoxDriver();
//WebDriver(class)
//driver (object)
//wait Time
Thread.sleep(5000);
//close the browser close(method)
driver.close();
}
}
After Run the program i got the below error..but already extract the geckodriver(Firefox driver) into my system
Exception in thread "main" java.lang.IllegalStateException: The path
to the driver executable must be set by the webdriver.gecko.driver
system property; for more information, see
https://github.com/mozilla/geckodriver. The latest version can be
downloaded from https://github.com/mozilla/geckodriver/releases at
com.google.common.base.Preconditions.checkState(Preconditions.java:847)
at
org.openqa.selenium.remote.service.DriverService.findExecutable(DriverService.java:125)
at
org.openqa.selenium.firefox.GeckoDriverService.access$100(GeckoDriverService.java:43)
at
org.openqa.selenium.firefox.GeckoDriverService$Builder.findDefaultExecutable(GeckoDriverService.java:168)
at
org.openqa.selenium.remote.service.DriverService$Builder.build(DriverService.java:346)
at
org.openqa.selenium.firefox.FirefoxDriver.toExecutor(FirefoxDriver.java:168)
at
org.openqa.selenium.firefox.FirefoxDriver.(FirefoxDriver.java:125)
at
org.openqa.selenium.firefox.FirefoxDriver.(FirefoxDriver.java:103)
at
com.webdrivercommands.WDcommands_Test1.main(WDcommands_Test1.java:17)
example 1: launch FF in blank temp profile:
#BeforeClass
public static void setUpClass() {
FirefoxOptions options = new FirefoxOptions();
options.setBinary("C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe");
System.setProperty("webdriver.gecko.driver", "C:\\Users\\pburgr\\Desktop\\geckodriver-v0.19.1-win64\\geckodriver.exe");
driver = new FirefoxDriver(options);
driver.manage().window().maximize();}
example 2: launch FF in blank temp profile with some preferences
#BeforeClass
public static void setUpClass() {
FirefoxOptions options = new FirefoxOptions();
FirefoxProfile selenium_profile = new FirefoxProfile();
selenium_profile.setPreference("browser.download.folderList",2);
selenium_profile.setPreference("browser.download.dir", "C:\\Users\\pburgr\\Desktop\\BP_usr_tmp\\");
selenium_profile.setPreference("browser.helperApps.neverAsk.saveToDisk","application/application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
options.setProfile(selenium_profile);
options.setBinary("C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe");
System.setProperty("webdriver.gecko.driver", "C:\\Users\\pburgr\\Desktop\\geckodriver-v0.19.1-win64\\geckodriver.exe");
driver = new FirefoxDriver(options);
driver.manage().window().maximize();}
example 3: lauch FF with existing FF profile:
#BeforeClass
public static void setUpClass() {
FirefoxOptions options = new FirefoxOptions();
ProfilesIni allProfiles = new ProfilesIni();
FirefoxProfile selenium_profile = allProfiles.getProfile("selenium_profile");
options.setProfile(selenium_profile);
options.setBinary("C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe");
System.setProperty("webdriver.gecko.driver", "C:\\Users\\pburgr\\Desktop\\geckodriver-v0.19.1-win64\\geckodriver.exe");
driver = new FirefoxDriver(options);
driver.manage().window().maximize();}

Selenium "Cannot login"

I am unable to access through this lock icon and get to login. I have used different locator options.
The current code I am using:
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
driver.get("example.com");
driver.manage().window().maximize();
driver.findElement(By.id("login-trigger")).click();
driver.findElement(By.id("username")).sendKeys("User1");
driver.findElement(By.id("password")).sendKeys("123456");
driver.findElement(By.id("submit")).click();
Instead of writing driver.findElement(By.id("submit")).click();
try this : driver.findElement(By.id("submit")).sendKeys(Keys.ENTER);

Files not found error when tried to upload files using AutoIT and Selenium

When I am trying to run the AutoIT.exe script or my Selenium script, it is throwing 'File Not found error'. Please suggest!
Selenium code snippet: ===================================
public static void main(String[] args) throws Exception { System.setProperty("webdriver.chrome.driver", "C:\\Users\\IBM_ADMIN\\Desktop\\Drivers\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("http://demo.automationtesting.in/Register.html");
driver.manage().window().maximize(); Thread.sleep(2000);
driver.findElement(By.xpath("//input[#id='imagesrc' and #type='file']")).click();
Runtime.getRuntime().exec("C:\\Users\\IBM_ADMIN\\Desktop\\Selenium practise\\SeleniumAssignment\\AutoIT\\MultipleFileUpload.exe"+" "+"C:\\Users\\IBM_ADMIN\\Desktop\\0234_005.pdf"); Thread.sleep(2000);
driver.findElement(By.xpath("//input[#id='imagesrc' and #type='file']")).click();
Runtime.getRuntime().exec("C:\\Users\\IBM_ADMIN\\Desktop\\Selenium practise\\SeleniumAssignment\\AutoIT\\MultipleFileUpload.exe"+" "+"C:\\Users\\IBM_ADMIN\\Desktop\\BRP_SujataChakraborty.pdf"); driver.quit(); }
AutoIt Code Snippet: ============================
ControlFocus("Open","","Edit1")
ControlSetText("Open","","Edit1",$CmdLine[0])
ControlSetText("Open","","Edit1",$CmdLine[1])
ControlSetText("Open","","Edit1",$CmdLine[2])
ControlClick("Open","","Button1")
Is brower file dialog active window? If not auto it script will enter value in window whichever is active. Do make sure file dialog active?. Do interact with brower once it is started. And one more thing if you input type is file then you can use send keys instead of auto it to enter value.

Selenium Webdriver runtime Exception: unexpected alert "Please close other open tabs of the application and re-open this activity"

Selenium Webdriver runtime Exception: unexpected alert "Please close other open tabs of the application and re-open this activity", even though there are no other tabs open.
The Automation scripts were working fine, but recently I am getting the above exception.The Screenshot of the Alert:
I tried with different versions of chrome, selenium stand alone driver, but the issue still exist. Why is this alert coming, even though the application is not open in any of the tabs or browsers other than the one execution through online?
If this pop up is coming at some specific location every time then you can simply accept it by writing below lines :
WebDriver wb=new ChromeDriver();
wb.switchTo().alert().accept();
Else if any unexpected pop up is coming ,while initializing the driver then you can use the below code :
ChromeOptions options = new ChromeOptions();
Map<String, Object> prefs = new HashMap<String, Object>();
prefs.put("profile.default_content_settings.popups", 0);
String path=new GetBasePath().getPath_XML()+dir;
prefs.put("download.default_directory", path);
options.addArguments("disable-extensions");
prefs.put("credentials_enable_service", false);
prefs.put("password_manager_enabled", false);
options.setExperimentalOption("prefs", prefs);
options.addArguments("chrome.switches","--disable-extensions");
options.addArguments("--test-type");
DesiredCapabilities cap = DesiredCapabilities.chrome();
cap.setCapability(ChromeOptions.CAPABILITY, options);
cap.setCapability(CapabilityType.UNEXPECTED_ALERT_BEHAVIOUR, UnexpectedAlertBehaviour.ACCEPT);
System.setProperty("webdriver.chrome.driver",**path to chromedriver.exe**"));
WebDriver wb= new ChromeDriver(cap);
My personal opinion , use both.

Error when trying to login using selenium webdriver in IE11

I am getting the below error when i try to login and select a drop down in login page using selenium webdriver in IE11.
[{
DesiredCapabilities capabilities=DesiredCapabilities.internetExplorer();
capabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
System.setProperty("webdriver.ie.driver", "C://Users//chetana.inamdar//Desktop//selenium//IEDriverServer.exe");
WebDriver driver = new InternetExplorerDriver(capabilities);
driver.navigate().to("url");
driver.findElement(By.id("ctl00_plhContent_txtUserName")).sendKeys("username");
driver.findElement(By.id("ctl00_plhContent_txtPassword")).sendKeys("Password");
driver.findElement(By.id("ctl00_plhContent_btnLogin")).click();
driver.manage().timeouts().implicitlyWait(120, TimeUnit.SECONDS);
//Select dropdown=new Select(driver.findElement(By.xpath(".//*[#name='ctl00$plhContent$Tabs$pnltabPermanentArea$ddlIDSearch']")));
Select dropdown=new Select(driver.findElement(By.name("ctl00$plhContent$Tabs$pnltabPermanentArea$ddlIDSearch")));
dropdown.selectByValue("CS Number");
System.out.println("the output of selected is"+driver.findElement(By.id("ctl00_plhContent_Tabs_pnltabPermanentArea_ddlIDSearch")).isSelected());
driver.findElement(By.id("ctl00_plhContent_Tabs_pnltabPermanentArea_txtIDSearch")).sendKeys("1234");
}]
Error:

Resources