How to close Popovers and in-line ads with selenium webdriver - java

I'm trying to learn Selenium Webdriver using tutorials online etc...
I'm struggling to overcome this obctacle which is to close this popover.
Using:
Laptop: Alienware
O.S: Windows 10 64bits
Browser: Firefox 51.0.1 (32-bit)
Eclipse: Version: Neon.2 Release (4.6.2) Build id: 20161208-0600
Selenium Webdriver: Java 3.0.1 2016-10-18
`package com.indeed.tests;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class IndeedJobSearch {
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
//Create firefox driver to drive the browser
System.setProperty("webdriver.gecko.driver", "C:\\Users......\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
//Open Indeed home page
driver.get("https://www.indeed.co.uk/");
//Find what field and enter Selenium
Thread.sleep(1000);
driver.findElement(By.id("what")).sendKeys("Selenium");
//Find location field and enter London
driver.findElement(By.id("where")).clear();
Thread.sleep(1000);
driver.findElement(By.id("where")).sendKeys("London");
//Find FindJobs button and click on it
Thread.sleep(1000);
driver.findElement(By.id("fj")).click();
//Close popup - popover, not popup
//prime-popover-div
//selenium webdriver cannot close bootstrap popovers
//Can't find a solution
//From job search results page, get page title and jobs count message
//searchCount
System.out.println(driver.getTitle());
System.out.println(driver.findElement(By.id("searchCount")).getText());
driver.close();
}
}
`
Expected Result: Selenium Webdriver would open firefox browser, load indeed.co.uk webpage, insert "Selenium" in the first field, insert "London" in the second field, hit the search button, get title and job count values on the console and driver window.
Actual Result: Selenium Webdriver would open firefox browser, load indeed.co.uk webpage, insert "Selenium" in the first field, insert "London" in the second field, hit the search button, STOPS the focus in on the url field and nothing else happens.
I've tried a few solutions but couldn't get it working
(https://sqa.stackexchange.com/questions/5310/how-to-close-pop-up-window-in-selenium-webdriver)
e.g.
driver.findElement(By.id("prime-popover-close-button")).click();
Driver.SwitchTo().frame("prime-popover-div");
Driver.findElement(By.id("prime-popover-close-button")).click();
Driver.SwitchTo().defaultContent();
driver.findElement(By.xpath("//*[#id='prime-popover-close-button']/a/img")).click();
Note: Not entirely sure my xpath was writen correctly, still learning.
None of these seem to work. I read something about Selenium WebDriver not handling bootstrap popovers, not sure if that's exactly my case, or if any of you has found a solution.
Would love solutions and or advice :)
Thank you very much in Advance.

Your code generally looks fine (other than the use of Thread.Sleep(), which I will address in a minute.
Basically what you want to do in these cases is to right-click on the close X of the dialog and treat it like any other element on the page. Find a locator for the X, in this case it also has an id, prime-popover-close-button, that we can use. All you need to do is grab that element using the id and click it to dismiss the popup. I've simplified the code below.
driver.get("https://www.indeed.co.uk/");
driver.findElement(By.id("what")).sendKeys("Selenium");
driver.findElement(By.id("where")).sendKeys("London");
driver.findElement(By.id("fj")).click();
driver.findElement(By.id("prime-popover-close-button")).click();
If you aren't trying to test the UI (entering text and clicking buttons) on the search page, you can just navigate directly to the url and even feed your own keywords in, if you like. See the code below for that.
String what = "selenium";
String where = "london";
driver.get("https://www.indeed.co.uk/jobs?q=" + what + "&l=" + where);
driver.findElement(By.id("prime-popover-close-button")).click();
Now back to Thread.Sleep(). This form of wait is generally a bad practice. You can do some research into the details but suffice it to say that it's not flexible. If you sleep for 10s and the element is present in 25ms, you've waited a long time that you didn't need to. Read up on WebDriverWait and ExpectedConditions. While you didn't need it here, you will eventually need to wait and these are best practices for waiting.

It looks like I'm doing the same tutorial you are :) I ran into the exact same issue you did, and tried almost everything you did to click that close button and kill that popover before finding this thread.
It appears that the problem lies in that the popover isn't immediately available for Selenium to close after we click Find Jobs. A 'wait.until..' has to be set in place to wait for the popover to appear so we can close it. Here's what I did:
package com.indeed.tests;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait; //**and this
public class IndeedJobSearch {
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
//Create firefox driver to drive the browser
WebDriver driver;
System.setProperty("webdriver.gecko.driver", "C:\\Users\\BURRITOBEAST\\Downloads\\jars\\geckodriver-v0.14.0-win64\\geckodriver.exe");
driver = new FirefoxDriver();
WebDriverWait wait = new WebDriverWait(driver,10); //**and this. 10 is the number of seconds it'll wait before an error is thrown.
//Open Indeed homepage
driver.get("http://www.indeed.com");
//Find the 'what' field and enter "selenium"
driver.findElement(By.id("what")).sendKeys("Selenium");
//Find the 'location' field and enter "San Diego, CA"
driver.findElement(By.id("where")).clear();
driver.findElement(By.id("where")).sendKeys("San Diego, CA");
//Find the 'findjobs' button and click on it
driver.findElement(By.id("fj")).click();
wait.until(ExpectedConditions.elementToBeClickable(By.id("prime-popover-close-button"))); //**this is where the magic happens
//Thread.sleep(1000); **tested my idea first using a sleep. then found the wait method after. plus, i want to avoid sleeps if possible to make things speedy.
driver.findElement(By.id("prime-popover-close-button")).click();
//From the job search results page, get page title and jobs count msg
}
}

Related

Selenium Webdriver: How to bypass Google "accept-cookies" dialog box

I have a really simple Selenium WebDriver project in Java where I am using FireFox driver.
My goal is to navigate to Google's page (https://www.google.com) and when prompted to accept
Cookies be able to click on the "I agree"-button to just get rid of it and continue the automation process further. But for some reason I just can't get the browser to locate it.
This is the instruction I am using currently:
package main;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class SeleniumGoogleTest {
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.get("https://www.google.com");
WebElement acceptButton = driver.findElement
(By.xpath("/html/body/div/c-wiz/div[2]/div/div/div/div/div[2]/form/div/div[2]"));
}
}
I don't know why the browser can't locate it and activate/enable that part of the page
with neither Implicit wait or Explicit wait. Thread.sleep() method don't seem to be the
solution either in this case.
The only error message I get when running the application is that of "Unable to locate the element".
Is it that you actually can't automate some stuff with Selenium WebDriver or have I misunderstood some important concepts here?
Much grateful for all tips !
you can handle it by update the cookies "CONSENT" delete the old one because the value is "PENDING" and Add it with the below value.
driverManager.driver.manage().deleteCookieNamed ("CONSENT");
driverManager.driver.manage().addCookie(new Cookie("CONSENT","YES+shp.gws-"+LocalDate.now().toString().replace("-","")+"-0-RC2.en+FX+374"));
driverManager.driver.navigate().refresh();
The popup is located on an iFrame, first you have to switch to the iFrame:
driver.switchTo().frame(yourFrame);
after you can find the accept button, and click it:
driver.findElement(By.id("id")).click();
I've used the same solution as Jus, but Chrome had 'I agree' button id changed since then
Here how updated solution should look like
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
import time
driver = webdriver.Chrome('chromedriver.exe')
driver.get('https://google.com/xhtml')
time.sleep(2) # seconds until popup appears
try: # 2 different popups
frame = driver.find_element_by_xpath('//*[#id="cnsw"]/iframe') #<-locating chrome cookies consent frame
driver.switch_to.frame(frame)
driver.find_element_by_xpath('//*[#id="introAgreeButton"]').click()#<-looking for introAgreeButton button, but seems google has changed its name since and it only works in old chrome versions.
except NoSuchElementException:
driver.find_element_by_xpath('//*[#id="L2AGLb"]').click() #<- pay attention to new id.
In case if this id will expire, I recommend you to inspect element yourself like this:
Inspect Element
Click twice 'Inspect element'
Locating 'I agree' button's id
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
import time
driver = webdriver.Chrome('chromedriver.exe')
driver.get('https://google.com/xhtml')
time.sleep(2) # seconds until popup appears
try: # 2 different popups
frame = driver.find_element_by_xpath('//*[#id="cnsw"]/iframe')
driver.switch_to.frame(frame)
driver.find_element_by_xpath('//*[#id="introAgreeButton"]').click()
except NoSuchElementException:
driver.find_element_by_xpath('//*[#id="zV9nZe"]').click()

Selenium code to open facebook messenger and send a message

I'm just starting to learn a little bit of Selenium scripting (in Java). Currently I'm trying to open a chat box in Facebook and send a message.
I have gotten up to being able to open the chat box through selenium, however I can't figure out how to actually type things into the text box.
Currently here is my code:
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
public class practice {
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "C:/max/chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("http://facebook.com/login/");
driver.manage().window().maximize();
driver.findElement(By.name("email")).sendKeys("myemail#yahoo.com");
driver.findElement(By.name("pass")).sendKeys("myPassword");
driver.findElement(By.id("loginbutton")).click();
driver.findElement(By.name("mercurymessages")).click();
driver.findElement(By.cssSelector("a[href*='https://www.facebook.com/messages/conversation-8148306']")).click();
// This has worked randomly. Sometimes the driver will work and open the chat box. Sometimes it will say element not found. I didn't include the full link of the conversation because apparently you don't have to. And it has worked like this in the past.
}
}
My current issue I'd like resolved is: why does this only work sometimes, and how do i find the text box area on the chat box? I did an Inspect Element and the chat box is very odd. It doesn't have anything like an id or a name, so i can't do By.id, or By.name. I can't figure out how to do it with By.cssSelector. This is what the inspect element for the text box looks like:
textarea class="uiTextareaAutogrow _552m" data-ft=".... more stuff here" onkeydown=" more stuff here" style= "more stuff here."
You have to learn Xpath and how to create Relative xpath.The xpath for the textarea
driver.findElement(By.xpath("//textarea[#class='uiTextareaAutogrow _552m']"));
Anyhow I've made few changes that Include instead of clicking on some other message.It will create a new message and send to your friend
driver.findElement(By.name("mercurymessages")).click();
//wait for 20 seconds
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("u_0_5")));
driver.findElement(By.id("u_0_5")).click();//To click on Send a New Message Link
//To enter a name into the to field
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//input[#class='inputtext textInput']")));
WebElement friendName = driver.findElement(By.xpath("//input[#class='inputtext textInput']"));
friendName.sendKeys("Deep");//Change it with your friend name
//wait for the user list to appear
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//li[#class='user selected']")));
friendName.sendKeys(Keys.ENTER);
WebElement messageBox = driver.findElement(By.xpath("//textarea[#class='uiTextareaAutogrow _552m']"));
wait.until(ExpectedConditions.visibilityOf(messageBox));
messageBox.sendKeys("Hi there");
messageBox.sendKeys(Keys.ENTER);
Hi pal hope this gonna help you:
there is a method called By.className so in order to find elements without an id or a name you can use this method, the thing it's that sometimes the class name of the element it's used in other elements, so be aware of the uses in the current page, if you are lucky and the class name is unique then you can use it :)
System.setProperty("webdriver.chrome.driver", "C:/max/chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("http://facebook.com/login");
driver.manage().window().maximize();
Thread.sleep(5000);
driver.findElement(By.name("email")).sendKeys("myemail#yahoo.com");
driver.findElement(By.name("pass")).sendKeys("myPassword");
driver.findElement(By.id("loginbutton")).click();
Thread.sleep(5000);
driver.findElement(By.name("mercurymessages")).click();
Thread.sleep(7000);// here you have to use ExpectedConditions in order to verify that the elemnt it´s "clickable"
driver.findElement(By.cssSelector("a[href*='https://www.facebook.com/messages/conversation-8148306']")).click();
Thread.sleep(5000);
WebElement mssgbox = driver.findElement(By.cssSelector("textarea[class*='uiTextareaAutogrow _552m']"));
mssgbox.click();
mssgbox.sendKeys("hi");
hope this help you.
and some times elements in the pages changes so use ExpectedConditions to be sure the element its on the page
How to send message to your friend on Facebook:: Using Selenium WebDriver
// Find Username and Enter
driver.findElement(By.id("email")).sendKeys("Email");
System.out.println("Email");
// Find Password and Enter
driver.findElement(By.id("pass")).sendKeys("Password");
System.out.println("Password");
// Click Login
driver.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS);
driver.findElement(By.xpath(".//*[#id='u_0_q']")).click();
System.out.println("LogIn Successfull");
Thread.sleep(2000);
// Message
driver.findElement(By.xpath(".//*[#id='u_0_f']/a/div")).click();
driver.findElement(By
.xpath(".//*[#id='u_0_f']/div/div[3]/div/div[1]/div/div/ul/li[2]/a/div/div[2]/div/div[2]/div/div[1]/strong/span"))
.click();
Thread.sleep(2000);
driver.findElement(By.xpath("//*[#class='_1mf _1mj']//following :: div[11]")).click();
Thread.sleep(2000);
WebElement sendmsg = driver
.findElement(By.xpath("//div[#class='_1ia']/descendant::div[#class='_5rpu' and #role='combobox']"));
sendmsg.sendKeys("Just testing: using selenium webdriver" + Keys.ENTER);
IWebElement messageBox = driver.FindElement(By.ClassName("_1mj"));
messageBox.SendKeys(string.Format("{0} Do not try to scam people anymore!!!", iteratia++));
messageBox.SendKeys(Keys.Enter);
Using C#
I can access the message textbox to insert the message with the first line of code. The second line is inserting the message. The third line is sending it.
Result:

How to automate SOAP UI in selenium Web driver

We have an application where we have a customer Module.
Here it will displayed below Field
Customer Name
Address 1
Address 2
City
State
To Fetch the records in customer module in a Web page, We need to give the input data in soap UI, once after execute from soap UI, A new customer will created and display in the UI Web page.
How can we automate this process through selenium Web driver.
So the most obvious, and perhaps the easiest way, to get Selenium and SoapUI to cooperate is:
Install SoapUI.
Download Selenium (you need the selenium-server-standalone-2.*.jar)
and drop it into your SoapUI installation (into
%SOAPUI_HOME%\bin\ext).
Fire up SoapUI; start a new Project; create a new test case; add a
new Groovy step; copy-paste the sample code into the step. I made a
few modification: drop the package line, drop the class
Selenium2Example and void main lines along with the closing
brackets, and change the System.out.println to log.info. My final
(full) test code is below.
Click Play. You should see Firefox starting up, navigating to
Google, and afterwards you should see the SoapUI log entries.
sample code:
import org.openqa.selenium.By
import org.openqa.selenium.WebDriver
import org.openqa.selenium.WebElement
import org.openqa.selenium.firefox.FirefoxDriver
import org.openqa.selenium.support.ui.ExpectedCondition
import org.openqa.selenium.support.ui.WebDriverWait
// Create a new instance of the Firefox driver
// Notice that the remainder of the code relies on the interface,
// not the implementation.
WebDriver driver = new FirefoxDriver()
// And now use this to visit Google
driver.get("http://www.google.com")
// Find the text input element by its name
WebElement element = driver.findElement(By.name("q"))
// Enter something to search for
element.sendKeys("Cheese!")
// Now submit the form. WebDriver will find the form for us from the element
element.submit()
// Check the title of the page
log.info("Page title is: " + driver.getTitle())
// Google's search is rendered dynamically with JavaScript.
// Wait for the page to load, timeout after 10 seconds
(new WebDriverWait(driver, 10)).until(new ExpectedCondition() {
public Boolean apply(WebDriver d) {
return d.getTitle().toLowerCase().startsWith("cheese!")
}
});
// Should see: "cheese! - Google Search"
log.info("Page title is: " + driver.getTitle())
//Close the browser
driver.quit()
This answer is a copy-paste from my blog.

webdriver is not able to click on a hyperlink in firefox

package testproject;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class WeblinkTest {
public static void main(String[] args) throws InterruptedException {
// Creating a fierfox driver/window
WebDriver driver= new FirefoxDriver();
//Assigning address of the webpage which you want to check
driver.get("https://www.google.co.in/");
Thread.sleep(2000);
//Creating and Identifing--By.xpath the element on which you want testing
WebElement wb1= driver.findElement(By.xpath(".//*[#id='gb']/div[1]/div[1]/div[1]/div[2]"));
wb1.click();
Thread.sleep(2000);
}
}
Today i was trying to test a gmail link which is available on Google homepage(www.google.co.in). I am able to launch a fierfox window and also it is able to do the first step which is taking me to google homepage but after that nothing is happening neither i am getting any run time error or any exception from eclipse. Don't know what is going on with the webdriver.
I was facing problem with one more program which i already posted on stakwave so if u can then please have a look at this link-Why my test is throwing Exception-Unable to locate element in webdriver?
Please try below. It should work
List<WebElement> elements = driver.findElement(By.LinkText("Gmail"))
elements.get(0).click().
Try with implimentig implicit wait after driver initializes. by adding below line
driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);
The XPATH in the code is DIV element but you want to click on ANCHOR Gmail So update the xpath for ANCHOR and click on it.
I have tested the below code,it clicks on GMAIL link on google page,if your question is solved then select as answer
WebDriver driver= new FirefoxDriver();
driver.get("https://www.google.co.in/");
WebElement wb1= driver.findElement(By.xpath(".//*[#id='gb']/div[1]/div[1]/div[1]/div[2]/a"));
wb1.click();
This should work,
new WebDriverWait(driver,30).until(ExpectedConditions.visibilityOfElementLocated(By.linkText("Gmail"))).click();
I have posted this question in Jan saying that the web-driver was not able to click on the hyperlink and just now i got the solution.
Actually the xpath for the hyperlink was not accurate.
I had used this xpath-
.//*[#id='gb']/div[1]/div[1]/div[1]/div[2]
which was locating the logo but not the button.
Today i changed it by
.//*[#id='gb']/div[1]/div[1]/div[1]/div[2]/a
and now its working absolutely fine.
Please don't be angry on me because i was going through the questions which i had asked from the forum and found this question. I got the solution for the problem thats why i am sharing this.

Downloading a pdf file

I am automating a application using selenium webdriver,below is my codes which works fine
enter code here:
import java. util.concurrent. TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.JavascriptExecutor;
import com.thoughtworks.selenium.SeleneseTestCase;
public class MonTaxRep1 extends SeleneseTestCase{
/**
* #param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
WebDriver driver = new FirefoxDriver();
driver.get(" URL ");
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
WebElement un= driver.findElement(By.name("username"));
un.sendKeys("clientremote");
driver.findElement(By.name("password")).sendKeys("12345678");
driver.findElement(By.name("submit")).click();
// find the element and click on signin
// driver.findElement(By.id("loginButton")).click();
driver.findElement(By.xpath("//a/span[contains(text(),'Thailand')]")).click();
driver.findElement(By.linkText("Williams Limited Thailand")).click();
driver.findElement(By.xpath("//map[#id='Map']/area[3]")).click();
driver.findElement(By.cssSelector("a > img")).click();
driver.findElement(By.xpath("//img[#onclick=\"showItem('_self')\"]")).click();
new Select(driver.findElement(By.name("pay_year"))).selectByVisibleText("2010");
new Select(driver.findElement(By.name("pay_month"))).selectByVisibleText("January");
driver.findElement(By.linkText("Monthly Tax Report")).click();
driver.findElement(By.name("g_title")).sendKeys("Test1");
driver.findElement(By.xpath("//input[#value='Download']")).click();
When selenium clicks on download link a pop-up window appears which contains two radio buttons by default the radio button is clicked on for open with option now i need to switch that radio button into save as option and click on OK button .
When i click on OK button the the pdf file should be saved in some specific local drives .
For this i have used the below code but it is not working.
//Before opening pop-up get the main window handle
String mainWindowHandle=driver.getWindowHandle();
//open the pop-up window(i.e click on element which causes open a new window)
driver.findElement(By.xpath("//input[#value='Download']")).click();
//Below code returns all window handles as set
Set s = driver.getWindowHandles();
Iterator ite = s.iterator();
while(ite.hasNext())
{
String popupHandle=ite.next().toString();
if(!popupHandle.contains(mainWindowHandle))
{
driver.switchTo().window(popupHandle);
}
So please help me in providing code for this,
I had a doubt whether the downloaded pdf file can be opened and read line by line and can compare some text present in that or not,is this possible ??
The short answer: This has been asked many times, please search.
The long and more correct answer: As of now (2012/11), it can't be done via WebDriver. It's one of the most requested features for the Selenium project. You can try one of these things:
Make a request for the specified link using HttpURLConnection or Apache HttpComponents. You can even download the file this way, although the usual practice is just to assert a 200 OK response to make sure that the file can be downloaded (since you usually don't really need the file when you're testing your application).
Snatch the file using any Java approach. Or this tool made by someone to be used with Selenium.
Use the Robot class to simply press Down arrow and Enter or something. But beware, this will only work for your particular browser and OS. It will break on any other configuration.
In firefox you can fix this by adding the following code to the setUp method of your selenium tests.
profile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/pdf,application/x-pdf");
If you have other types of documents you want to download other than pdfs you should look up the MIME type of whatever document you are trying to download and adding it to the comma delimited list.

Categories