Selenium web Driver retrieve response - java

how to retrieve response text generated in browser after executing script in selenium web drive.in the above figure the underlined response I want to retrieve and store it in a string.
//Opening the web page
driver = new ChromeDriver();
System.setProperty("webdriver.chrome.driver", "/usr/local/share/chromedriver");
driver.get(Mymark.str);
//Opening the login page
WebElement login= driver.findElement
(By.xpath("//a[text()='Log in']"));
login.click();
// Enter Username
WebElement Usrnm=driver.findElement
(By.xpath("//div[#id='mainContainer']//mymark-login[#class='x-scope mymark-login-0']//input[#name='uname']"));
Usrnm.sendKeys("nayazjh");
//Enter Password
WebElement Pswd= driver.findElement
(By.xpath("//div[#class='content']//div[#class='loginlayout layout vertical justified style-scope mymark-login']//input[#name='password']"));
Pswd.sendKeys("doordie");
//click on remember me button
driver.findElement(By.xpath( "//div[#id='toggleButton']")).click();
//Click on login button
WebElement logIn= driver.findElement
(By.xpath("//div[#class='lsubmitarea style-scope mymark-login']//paper-button[text()='Log in']"));
logIn.click();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.findElement
(By.xpath("//paper-button[text()='Add Bookmark Group ']")).click();
driver.findElement
(By.xpath("//paper-dialog[#id='addgroupdialog']//input[#id='input']")).sendKeys("selenium71");
element = driver.findElement
(By.xpath("//paper-dialog[#id='addgroupdialog']//div[#class='adddialogsubmit style-scope mymark-addgroup']" +
"//paper-button[#id='addgroupbutton']"));
element.click();
here i need a code to retrieve the response in java
}
1
The above image shows the response I want to get it using selenium or java
how to retrieve response text generated in browser after executing script in selenium web drive.in the above figure the underlined response I want to retrieve and store it in a string.

As far as I know it is impossible for selenium to get the actual response.
Selenium is build "on top". It can not directly control how the browser interacts with the server nor is it supposed to support this kind of control.
Selenium is thought as an ui automation framework, i.e. clicking and asserting ui state. It is not meant to intercept browser-server communication.

Related

Not able to click on Login button using selenium webdriver and Java

Hi I am trying to automate https://www.nextgenerationautomation.com and unable to click on login / SignUp button using Selenium 4
Steps:
Navigate to URL: https://www.nextgenerationautomation.com
Click on LogIn/SignUp button.
Issue: when I am using Thread.Sleep, code is working fine but when I am using explicit wait & implicit wait it's not working.
I have added Implicit in my base class at the time of driver initialization.
Here is the code that I have tried.
public class nextGenAutomationLoginPage extends Base {
#FindBy(xpath = "(//button[#class='_1YW_5'])[1]")
WebElement login;
public nextGenAutomationLoginPage() {
super();
PageFactory.initElements(driver, this);
// TODO Auto-generated constructor stub
}
public void clickOnLogin() throws InterruptedException {
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
System.out.println(driver.getTitle());
// Thread.sleep(2000);
wait.until(ExpectedConditions.elementToBeClickable(login));
//wait.until(ExpectedConditions.presenceOfNestedElementLocatedBy(login, By.xpath("//div[#class='_10b1I']") ));
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("window.scrollBy(0,document.body.scrollHeight)");
js.executeScript("arguments[0].scrollIntoView();", login);
login.click();
//driver.findElement(By.xpath("(//button[#class='_1YW_5'])[1]")).click();
wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.xpath("//div[#id='comp-kaoxkn4a1']")));
String sign = driver.findElement(By.xpath("//div[#id='comp-kaoxkn4a1']/h1/span")).getText();
System.out.println(sign);
}
Note: I tried to add Scroll as well to add some wait before clicking.
DOM:
Please let me know if i am missing anything here.
Hi I got the point whats happening here :
Manually navigate to URL: https://www.nextgenerationautomation.com (Selenium also loading this URL)
Manually Immediately keep clicking on "LogIn/SignUp button" (Selenium also able to click therefore NO error in console )
"LogIn/SignUp" page not opening unless enter image description here (LinkedIn following count widget ) gets appear on screen
Once enter image description here gets appear manually click on "LogIn/SignUp button", now Login page is opening (Selenium able to click after Thread.Sleep)
Summery:
This is a codding defect on that page.
"LogIn/SignUp" is not clickable until enter image description here gets added on page
Your selenium code is perfectly fine :)
Xpath I have used is //span[text()='Log In / SignUp']
Thanks KS
To click() on the element with text as Log In / SignUp you can use either of the following Locator Strategies:
xpath:
driver.findElement(By.xpath("//div[starts-with(#id, 'defaultAvatar')]//following::span[text()='Log In / SignUp']")).click();
However, as the element is a dynamic element, so to click() on the element you need to induce WebDriverWait for the elementToBeClickable() and you can use either of the following Locator Strategies:
xpath:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[starts-with(#id, 'defaultAvatar')]//following::span[text()='Log In / SignUp']"))).click();
Actually your code is clicking the login button before the page is loaded properly. please add visibility condition.
wait.until(ExpectedConditions.visibilityOfElementLocated(by));

How to check remember_me checkbox of Jenkins login page using selenium?

I cannot check-in the user credentials encrypt/decrypt logic, to the git, so for now we are copying the class files on CI machines. But it has dependency to contact IT team for copy access.
Approach I am thinking is like - create a customer chrome profile, launch it using selenium chrome driver, on Jenkins login page, add user name, password and click remember me. I hope this setting will persisted to next chrome session as well. But, somehow the code is not able check the checkbox. It do identify it but no click.
I have tried by suing wait, maximize the window, still get the error as :
org.openqa.selenium.ElementClickInterceptedException: element click intercepted: Element is not clickable at point (764, 504). Other element would receive the click: ...
Code I am using :
driver.get(input_params.get("JenkinsURL"));
driver.manage().window().maximize();
WebElement remember_me = driver.findElement(By.xpath("//*[#id='remember_me']"));
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
remember_me.click();
Please try with the below. It worked for me.
WebDriver driver = new ChromeDriver();
driver.manage().timeouts().setScriptTimeout(20, TimeUnit.SECONDS);
driver.manage().window().maximize();
driver.get("URL");
WebElement checkbox = driver.findElement(By.xpath("/html/body/div/div/form/div[4]/label/div[1]"));
checkbox.click();
Please use the xpath as below:
//div[#class='Checkbox-indicator']//*[local-name(0='svg']

How to press Page Down key in Selenium to scroll down a webpage through Java?

I want to scroll down my web page. What Java command should I use in Selenium?
Scrolling down a web page is not a valid usecase which can be validated perhaps you want to scroll down to bring a WebElement within the Viewport to interact with it. To achieve that you can use the executeScript() method as follows :
((JavascriptExecutor)driver).executeScript("arguments[0].scrollIntoView();", element);
Use the below code and try,
JavascriptExecutor js = (JavascriptExecutor) driver;
// Launch the application
driver.get("http://demo.guru99.com/test/guru99home/");
//This will scroll the web page till end.
js.executeScript("window.scrollTo(0, document.body.scrollHeight)");
Please refer the code below :
1) Using Action class****
WebDriver driver = new ChromeDriver();
//Creating an object 'action'
Actions action = new Actions(driver);
//open SoftwareTestingMaterial.com
driver.get(Site URL); // Give site URL as name of the web page
//sleep for 3secs to load the page
Thread.sleep(3000);
//SCROLL DOWN
action.sendKeys(Keys.PAGE_DOWN).build().perform();
Thread.sleep(3000);
//SCROLL UP
action.sendKeys(Keys.PAGE_UP).build().perform();

Selenium Webdriver - Unable to find element after page refresh/redirect

I am trying to write some UI test cases for an SAP-webUI (web based) application. After login, it shows the dashboard ( Workcenter's ) screen.
Now the problem is, I am able to open the page, enter U/N, Pwd and login through Selenium. After i press the "Login" button the URL changes and the page got redirected/refreshed.
E.g. URL before login : https://a/b/c/d/e/f/g.htm?sap-client=001&sap-sessioncmd=open
E.g. URL after successful Login : https://a/b(bDsdfsdsf1lg==)/c/d/e/f/g.htm
After this im unable to perform any action or press any link in any part of the page. I tried with all the possible attributes ( css, xpath, id ). Webdriver was unable to find any element on the page. It shows the error "No element found" alone.
I am using java with Selenium Web Driver.
Please find the html structure of the webpage below
<html><body><div><div><iframe>#document<html><head></head><frameset><frameset><frame>#document<html><head></head><body><form><div><div><table><tbody><tr><td><div><ul><li><a id=abcdef></a></li></ul></div></td></tr></tbody></table></div></div></form></body></html></frame></frameset></frameset></html></iframe></div></div></body></html>
Actually i want to click a linkmenu "abcd", which is inside iframe and frame as shown in the below HTML code
<html><head></head><body><iframe name=a1><html><head></head><frameset><frameset name=fs1><frame name=f1><html><head></head><body><table><tbody><tr><td><ul><li><a id=abcdef>
I tried the below code as well.
driver.switchTo().frame("a1");
driver.findElement(By.id("abcd")).click();
OR
driver.findElement(By.xpath("//*[#id='abcd']")).click();
After using the above code, still im getting the error "No such element"
Kindly suggest
Regards,
Siva
Do it this way...
driver.switchTo().frame(driver.findElement(By.xpath("//iframe[#name='a1']"))); // switching to iframe
followed by
driver.switchTo().frame("f1"); // switch to frame
and then your desired action...
driver.findElement(By.id("abcd")).click();
This is because of the iframe. You need to switch to it first:
driver.switchTo().frame(0);
driver.findElement(By.id("abcdef")).click();
where 0 is a frame index.
See doc on implicit wait here
I guess you should do a implicit wait until your chosen element is available
modify these code to suit your chosen element:
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.id("someid")));

How to logout gmail using selenium script?

I am writing selenium script for Gmail login and logout functionality. I am able to successfully login using below code.
//Open gmail
driver.get("http://www.gmail.com");
// Enter userd id
WebElement element = driver.findElement(By.id("Email"));
element.sendKeys("xyz#gmail.com");
//wait 5 secs for userid to be entered
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
//Enter Password
WebElement element1 = driver.findElement(By.id("Passwd"));
element1.sendKeys("Password");
//Submit button
element.submit();
But i could not write a script to logout. Could yu please provide me script for logout?
Thanks!
It is a very bad idea trying to automate Gmail. First of all, it is against Google's policy and when you sign up for Gmail, you accepted terms and conditions that you won't automate. Having said that there are many efficient ways to connect to your Gmail account. One of the approaches is to use IMAP client like IMAP4. Using this, you can connect to your Gmail, go through your inbox, delete messages,sign out etc. By doing this, you don't have to worry about automating UI portion. Also, Google changes its UI frequently just to stop people from automating it, so if you automate Gmail UI, then it might work today but it won't work after a couple of days.
Go through this link and you should be able to implement IMAP4 in your tests within few minutes:
http://mailsystem.codeplex.com/discussions/269058
Try the below code.
driver.findElement(By.className("gb_Ta")).click(); // To click the flyout menu
driver.findElement(By.className("gb_71")).click(); // To click the sign out button
Try the code below
driver.findElement(By.xpath("//*[#id='gb']/div[1]/div[1]/div/div[3]/div[1]/a")).click();
// click on actual logout button step 2
driver.findElement(By.id("gb_71")).click();
//closing the webdriver window after successful completion of the test
driver.close();
WebDriverWait wait=new WebDriverWait(driver,50);
WebElement logout=driver.findElement(By.cssSelector("span.gb_4.gbii"));
logout.click();
WebElement signout=driver.findElement(By.id("gb_71"));
signout.click();
After login to gmail, Try this code to logout:-
driver.findElement(By.xpath("//span[#class='gb_7 gbii']")).click(); driver.findElement(By.id("gb_71")).click();
use xpath
// Click on the image icon present in the top right navigational Bar
driver.findElement(By.xpath("//div[#class='gb_1 gb_3a gb_nc gb_e']/div/a")).click();
//Click on 'Logout' Button
driver.findElement(By.xpath("//*[#id='gb_71']")).click();
//Close the browser.
driver.close();
use cssSelector
//Click on the profile image present in the right top corner
driver.findElement(By.cssSelector("span.gb_3a.gbii")).click();
//Click on 'Sign Out' button
driver.findElement(By.id("gb_71")).click(); //Close the browser
window driver.close();
use cssselector:
driver.findElement(By.cssSelector("span.gb_8a.gbii")).click();
driver.findElement(By.id("gb_71")).click();
Try this code for sign out the gmail using the selenium webdriver. It's working for me
driver.findElement(By.cssSelector(".gb_b.gb_db.gb_R")).click();
Thread.sleep(5000);// Click on the image icon present in the top
right navigational Bar
driver.findElement(By.cssSelector(".gb_Fa.gb_Pe.gb_We.gb_wb")).click();
Thread.sleep(5000); //Signout button

Categories