I want to get session variable and cookies after login. i have used selenium webdriver and successfully login. but how to get session and cookies after login in selenium.here is my code:
try {
WebDriver driver = new FirefoxDriver();
driver.get("https://pacer.login.uscourts.gov/csologin/login.jsf");
System.out.println("the title is"+driver.getTitle());
WebElement id= driver.findElement(By.xpath("/html/body/div[3]/div/div[1]/div[1]/div[15]/div[2]/form/table[1]/tbody/tr/td[2]/input"));
WebElement pass=driver.findElement(By.xpath("/html/body/div[3]/div/div[1]/div[1]/div[15]/div[2]/form/table[2]/tbody/tr/td[2]/input"));
WebElement button=driver.findElement(By.xpath("/html/body/div[3]/div/div[1]/div[1]/div[15]/div[2]/form/div[2]/button[1]"));
id.sendKeys("USERNAME");
pass.sendKeys("PASSWORD");
button.click();
} catch (Exception e) {
e.printStackTrace();
}
please provide your suggestion asap.
Thanks
I ran your code with the following code additions and was able to get the following value in my console.
try {
WebDriver driver = new FirefoxDriver();
driver.get("https://pacer.login.uscourts.gov/csologin/login.jsf");
System.out.println("the title is" + driver.getTitle());
WebElement id = driver
.findElement(By
.xpath("/html/body/div[3]/div/div[1]/div[1]/div[15]/div[2]/form/table[1]/tbody/tr/td[2]/input"));
WebElement pass = driver
.findElement(By
.xpath("/html/body/div[3]/div/div[1]/div[1]/div[15]/div[2]/form/table[2]/tbody/tr/td[2]/input"));
WebElement button = driver
.findElement(By
.xpath("/html/body/div[3]/div/div[1]/div[1]/div[15]/div[2]/form/div[2]/button[1]"));
id.sendKeys("USERNAME");
pass.sendKeys("PASSWORD");
button.click();
Thread.sleep(10000);
Set<Cookie> cookies = driver.manage().getCookies();
System.out.println("Size: " + cookies.size());
Iterator<Cookie> itr = cookies.iterator();
while (itr.hasNext()) {
Cookie cookie = itr.next();
System.out.println(cookie.getName() + "\n" + cookie.getPath()
+ "\n" + cookie.getDomain() + "\n" + cookie.getValue()
+ "\n" + cookie.getExpiry());
}
} catch (Exception e) {
e.printStackTrace();
}
console
the title isPACER Login
Size: 1
JSESSIONID
/csologin/
pacer.login.uscourts.gov
E44C8*********************400602
null
Instead of Thread.sleep(10000) you can also try using explicit wait. I believe that the web page took some time to set the cookies since it was busy waiting for the page to load.
Hope this helps you.
Related
I am trying to setup a tool to monitor my Plex account. I am using Chrome Driver to try and login to my Plex account with an email and password. I cannot manage to locate the input fields no matter what I try, whether by ID, XPath etc. I have run a test with the Selenium Chrome Plugin and it manages to to find the element by ID but when running the following Java code I cannot get the driver to find the email and password fields. The extra code is to deal with the Cookies pop up and seems to work, at least it dismisses the pop up.
...
File file = new File("src/chromedriver88.exe");
System.setProperty("webdriver.chrome.driver", file.getAbsolutePath());
LOGGER.info("Got Chrome driver - " + file.exists());
ChromeOptions options = new ChromeOptions();
options.addArguments("no-sandbox");
driver.get("https://www.plex.tv/en-gb/sign-in/");
// Sort out the cookies pop up if it's there
boolean present;
try {
driver.findElement(By.xpath("/html/body/div[2]/div[3]/a[2]"));
present = true;
} catch (NoSuchElementException e) {
present = false;
}
if (present) {
driver.findElement(By.xpath("/html/body/div[2]/div[3]/a[2]")).click();
}
// Wait for log in button to exist
WebDriverWait wait = new WebDriverWait(driver, 30);
#SuppressWarnings("unused")
WebElement elementLogin = wait.until(ExpectedConditions.presenceOfElementLocated(By.id("email")));
// Send log in details
WebElement username = driver.findElement(By.id("email"));
username.sendKeys("...");
WebElement pwd = driver.findElement(By.id("password"));
pwd.sendKeys("...");
// Click log in
driver.findElement(By.xpath("//html/body/div[1]/div/div/div/div[1]/form/button")).click();
Your email and password input inside a frame:
<iframe src="..." id="fedauth-iFrame"
#document
You need switch it first, use .frameToBeAvailableAndSwitchToIt:
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.id("fedauth-iFrame")));
WebElement username = wait.until(ExpectedConditions.elementToBeClickable(By.id("email")));
username.sendKeys("email");
WebElement pwd = wait.until(ExpectedConditions.elementToBeClickable(By.id("password")));
pwd.sendKeys("password");
Selenium version : 3.4.0
phantomjs version : 2.1.1
java : 1.8.0_151
I have tried below things but it didnt work for me.
enter code here
1:
webDriver instanceof PhantomJSDriver){
JavascriptExecutor je = (JavascriptExecutor) driver;
je.executeScript("window.alert = function(){};");
je.executeScript("window.confirm = function(){return true;};");
System.out.println("Alert has been handled"); }
else {
Alert a1 = driver.switchTo().alert();
a1.accept();
}
2:
/*((PhantomJSDriver)driver).executeScript("window.alert = function(){}");
((PhantomJSDriver)driver).executeScript("window.confirm = function(){return true;}");*/
3:
PhantomJSDriver phantom = (PhantomJSDriver) driver;
phantom.executeScript("window.confirm = function(){return true;};");
4:
((JavascriptExecutor)driver).executeScript("window.alert = function(msg){};");
((JavascriptExecutor)driver).executeScript("window.confirm = function(msg){return true;};");
5 :
PhantomJSDriver phantom = (PhantomJSDriver)driver;
phantom.executePhantomJS("var page = this;" +
"page.onAlert = function(msg) {" +
"console.log('ALERT: ' + msg);" +
"};");
phantom.executePhantomJS("var page = this;" +
"page.onConfirm = function(msg) {" +
"console.log('CONFIRM: ' + msg);"+ "return true;" +"};");
Can you please suggest apart from above.
To handle alert in PhantomJS you have to wait for the alert to be present and then cast the WebDriver to JavascriptExecutor. You can use can use the following code block :
((JavascriptExecutor) driver).executeScript("window.alert = function(msg){};");
//or
((JavascriptExecutor) driver).executeScript("window.confirm = function(msg){return true;};");
driver.findElement(By.id("element_which_trigers_the_alert")).click();
My code. I enter my login and password in the "userId" and "password" fields. And I would like to write a condition that will check when you hit the "Submit" button or it is possible to login to the system. If "userId" and "password" are correct, then when you press the "Submit" button a new page loads and the next part of the code is posted. But I would like to add a condition when the user gives a bad password. Then, after pressing the "Submit" button, the message "Bad login information" appears on the page. Can anyone help me write such a condition?
for(int i =0;i<userName.size();i++){
driver.findElement(By.id("userId")).sendKeys(userName.get(i));
Thread.sleep(6000);
driver.manage().timeouts().implicitlyWait(100, TimeUnit.SECONDS);
driver.findElement(By.id("password")).sendKeys(password.get(i));
Thread.sleep(6000);
driver.manage().timeouts().implicitlyWait(100, TimeUnit.SECONDS);
driver.findElement(By.id("Submit")).click();
// Here load new page
// change password
driver.findElement(By.xpath(".//*[#id='_id9']/table/tbody/tr[2]/td[1]/table[1]/tbody/tr[5]/td[2]/a")).click();
driver.manage().timeouts().implicitlyWait(100, TimeUnit.SECONDS);
Thread.sleep(6000);
driver.manage().timeouts().implicitlyWait(100, TimeUnit.SECONDS);
// new password
String pwd = RandomStringUtils.random( 15, upper+smaller+number+character);
System.out.println("New password: " + pwd);
driver.findElement(By.id("newPassword")).sendKeys(pwd);
Thread.sleep(6000);
driver.manage().timeouts().implicitlyWait(100, TimeUnit.SECONDS);
// confirm password
driver.findElement(By.id("confirmPassword")).sendKeys(pwd);
System.out.println("Confirm password: " + pwd);
Thread.sleep(10000);
driver.manage().timeouts().implicitlyWait(100, TimeUnit.SECONDS);
Try following:
after clicking Submit button; you can check whether this element is present on your page or not : driver.findElement(By.xpath(".//*[#id='_id9']/table/tbody/tr[2]/td[1]/table[1]/tbody/tr[5]/td[2]/a"));
if it is that means new page was loaded as login was successful or else it was a bad login.
you can do so after clicking submit button try following:
UPDATE
List<WebElement> myElemnt = driver.findElements(By.xpath(".//*[#id='_id9']/table/tbody/tr[2]/td[1]/table[1]/tbody/tr[5]/td[2]/a"));
loginSuccessful(myElemnt);
// implementation of loginSuccessful() function:
public boolean loginSuccessful(WebElement element){
try{
element;
return true;
}
catch(Exception e){
System.out.println("Bad Login Information!!");
return false;
}
}
OLD
String beforeTitle = driver.getTitle();
//put your Click Submit Button code here
String afterTitle = driver.getTitle();
if(beforeTitle.equals(afterTitle)){
System.out.println("Bad Login Information!!");
}
public void test() throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "C://chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().Timeouts().implicitlyWait(10, TimUnit.SECONDS);
driver.get("http://stackoverflow.com");
driver.manage().window().maximize();
driver.findElement(By.id("nav-tags")).click();
WebElement element = driver.findElement(By.name("tagfilter"));
element.sendKeys("qa");
element.submit();
}
I have written the above code but what would be the next?
I would assume that the most popular tag is the first one shown in the tags page.
Based on above assumption, you can try out the following code:
System.setProperty("webdriver.chrome.driver", "G:\\naveen\\mywork\\testing\\automation\\tools\\webdrivers\\chromedriver.exe");
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("http://stackoverflow.com");
driver.manage().window().maximize();
driver.findElement(By.id("nav-tags")).click();
WebElement element = driver.findElement(By.name("tagfilter"));
element.sendKeys("qa");
Thread.sleep(2000);
element.sendKeys(Keys.ENTER);
Thread.sleep(3000);
WebElement popularTag = driver.findElement(By.xpath("//table[#id='tags-browser']/tbody/tr[1]/td[1]/a"));
System.out.println("most popular tag: " + popularTag.getText());
WebElement populatTagCount = driver.findElement(By.xpath("//table[#id='tags-browser']/tbody/tr[1]/td[1]/span/span[2]"));
System.out.println("popular tag count " + populatTagCount.getText());
WebElement tagCountAnual = driver.findElement(By.xpath("//table[#id='tags-browser']/tbody/tr[1]/td[1]/div[2]/div[1]/a[2]"));
System.out.println("anaual count : " + tagCountAnual.getText());
int anaualCount = Integer.parseInt(tagCountAnual.getText().split(" ")[0]);
System.out.println("count " + anaualCount);
driver.close();
Following is the output that i got:
most popular tag: qa
popular tag count 712
anaual count : 133 this year
count 133
you can cross check in the image:
I have been trying to use PhantomJSWebDriver framework for automating an application using Headless browser. The main issue is as we can successfully switch between windows in firefox or IE windows, here I am not able to switch between windows based on handles. Please help me.
Below is the code I have tried so far.
System.setProperty("phantomjs.binary.path", file.getAbsolutePath());
driver = new PhantomJSDriver();
driver.get(application url);
driver.manage().timeouts().implicitlyWait(100, TimeUnit.SECONDS);
WebElement txtUsername = driver.findElement(By.id("it_C_C5"));
txtUsername.sendKeys("sreenis");
WebElement txtPassword = driver.findElement(By.id("it_C_C7"));
txtPassword.sendKeys("sreeni");
WebElement btnLogin = driver.findElement(By.id("ic_C_C8"));
btnLogin.click();
Thread.sleep(10000);
String winTitle = "Role profile selection";
boolean bool = switchWindow(winTitle);
if (bool){
System.out.println("Page title is: " + driver.getTitle());
driver.quit();
}
else {
System.out.println("Switch to window '" + winTitle + "' failed!");
driver.quit();
}
public static boolean switchWindow(String windowtitle){
String mainWindowsHandle = driver.getWindowHandle();
Set<String> handles = driver.getWindowHandles();
System.out.println(handles.size());
for(String winHandle : handles){
driver.switchTo().window(winHandle);
System.out.println(driver.getTitle());
if(driver.getTitle().toLowerCase().equals(windowtitle)){
return true;
}
}
driver.switchTo().window(mainWindowsHandle);
return false;
}
When I tried to print the window titles in collection, it is only printing the parent window and not the other windows. I am not able to guess what is happening since nothing can be seen and it is headless testing. Please suggest me is there any other way so that I can test the app with many browser windows.
Actions act = new Actions(d);
act.contextClick(elements).sendKeys("W").perform();
Set<String> win = d.getWindowHandles();
Iterator <String> itrwin = win.iterator();
String parent = itrwin.next();
String child = itrwin.next();
d.switchTo().window(child);
identify an web element first using findElement() and store it in element.