My Wait() method works in 'Chrome' & 'Firefox' but not in 'IE', any ideas?
I have tried using IEDriverServer.exe both the 32bit and the 64bit(Send keys acts slow), but the following method still dosnt click on the intended target.
Please note: I know the locators are fine as listed above works in both Chrome and firefox.
public void waitAndClickFirstDrivingExperiencesOption(WebElement element) throws Exception {
WebDriverWait wait2 = new WebDriverWait(driver, 30);
Base_Page basePage = new Base_Page(driver);
try {
System.out.println(basePage.browser_type);
Boolean elementPresent = wait2.until(ExpectedConditions.elementToBeClickable(element)).isEnabled();
if (elementPresent == true) {
if (!this.browser_type.equals("firefox")) {
// Provide a slight timeout before clicking on the element
basePage.scrollToElementByLocator(element);
Thread.sleep(1000);
basePage.actionMoveAndClick(element);
} else {
Thread.sleep(500);
basePage.scrollToElementByLocator(element);
Thread.sleep(1000);
element.click();
}
}
System.out.println("Clicked on the first supercars link, using locator: " + element.toString());
} catch (StaleElementReferenceException elementUpdated) {
element = this.driver.findElement(By.xpath(".//*[#id='prd_listing']/div/li[1]/a"));
Boolean elementPresent = wait2.until(ExpectedConditions.elementToBeClickable(element)).isEnabled();
if (elementPresent == true) {
if (!this.browser_type.equals("firefox")) {
basePage.scrollToElementByLocator(element);
Thread.sleep(1000);
basePage.actionMoveAndClick(element);
} else {
Thread.sleep(500);
basePage.scrollToElementByLocator(element);
Thread.sleep(1000);
element.click();
}
}
System.out.println(
"Clicked on the first supercars link (Stale Exception), using locator: " + element.toString());
} catch (Exception e) {
System.out.println("Exception! - could not click on the first supercars link, Exception: " + e.toString());
throw (e);
} finally {
}
Related
I am finding trouble in ExpectedConditions.Provided type is "ExpectedCondition<List>
" and it is showing that expected should be "function". Not getting how to resolve it. Below is a code snippet.
public boolean verifyElementExists(final By locator) {
try {
List<WebElement> elementList = (new WebDriverWait(driver, Duration.ofSeconds(60), Duration.ofMillis(5000))
.until(new ExpectedCondition<List<WebElement>>() {
public List<WebElement> apply(WebDriver d) {
LOGGER.info("method[verifyElementExists] try to find element in HTML "
+ locator);
return d.findElements(locator);
}
});
if (elementList.size() > 0) {
return true;
}
} catch (Exception e) {
LOGGER.info("Error: element not found for " + locator);
ERRORLOGGER.error(this.getClass().getName() + e.getMessage());
}
return false;
}
Please help to get it resolve. Thanks!
How to verify whether file downloading is completed before closing my Selenium web driver in JAVA.
I have written a Selenium code to download 2 files to my desired folder location. However I close the browser instantly after clicking on the 2 links, which given me the downloaded files as temporary files or with an invalid extension. I used Thread.sleep method after clicking on the each of the two links before closing the web driver and it is now working fine.
I need to know whether there is an optimum method to check whether download is completed or not before closing the web driver using explicit method or any other way rather setting a pre-defined time using the Thread.sleep() method.
Here is part of the source code (JAVA, Selenium and Testng) which is relevant to this question.
// Text file download
#Test(priority=2)
public void txtFileDownloadTest() {
fileDownloadPage.clickTxtFileLink();
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
// Java file download
#Test(priority=3)
public void javaFileDownloadTest() {
fileDownloadPage.clickJavaFileLink();
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
#AfterClass
public void setDown() {
closeUp();
}
There is a piece of code I was using to download files. Just change fileName and increase timeout in waitSec(driver, int).
public WebDriverWait waitSec(WebDriver driver, int sec) {
return new WebDriverWait(driver, sec);
}
String fileName = "foo";
String filePathFull = "C:/users/user/downloads/" + fileName + ".csv";
waitSec(driver, 30).until(new Function<WebDriver, Boolean>() {
public Boolean apply(WebDriver driver) {
if(Files.exists(Paths.get(filePathFull))) {
return true;
}
else {
try {
Thread.sleep(1000);
}
catch (InterruptedException e) {
}
}
return false;
}
});
File exportFile = new File(filePathFull);
if (Files.size(Paths.get(filePathFull)) == 0) {
try {
waitSec(driver, 120).until(new Function<WebDriver, Boolean>() {
public Boolean apply(WebDriver driver) {
try {
if(Files.size(Paths.get(filePathFull)) > 0) {
return true;
}
else {
try {
Thread.sleep(1000);
}
catch (InterruptedException e) {
}
}
}
catch (IOException e) {
}
return false;
}
});
}
catch (TimeoutException e) {
}
}
There is always a .part file and until download is complete orginial file (csv in my example) has zero size.
I have purposely changed the element so it is incorrect, but my test doesn't fail in TestNG. Any ideas?
My Code:
public void waitAndClickElement(WebElement element) throws InterruptedException {
try {
element.click();
} catch (TimeoutException timeEx) {
this.wait.until(ExpectedConditions.elementToBeClickable(element)).click();
} catch (StaleElementReferenceException elementUpdated) {
this.wait.until(ExpectedConditions.elementToBeClickable(element)).click();
} catch (Exception e) {
System.out.println("Unable to wait and click on element, Exception: " + e.getMessage());
}
}
#Test(priority = 2)
public void clickOnDrivingExperiencePage() throws Exception {
basePage.waitAndClickElement(homepageHeader.link_DrivingExperiences);
}
New Code Changes:
public void waitAndClickElement(WebElement element) throws InterruptedException {
try {
this.wait.until(ExpectedConditions.elementToBeClickable(element)).click();
System.out.println("Successfully clicked on the WebElement: " + "<" + element.toString() + ">");
} catch (Exception e) {
System.out.println("Unable to wait and click on WebElement, Exception: " + e.getMessage());
Assert.assertFalse(true, "Unable to wait and click on the WebElement, using locator: " + "<" + element.toString() + ">");
}
}
} catch (Exception e) {
System.out.println("Unable to wait and click on element, Exception: " + e.getMessage());
}
You are catching all exceptions, so the test won't fail.
I have code something like this
WebDriver driver = new FirefoxDriver();
driver.get(something URL);
WebDriverWait waiting = new WebDriverWait(driver, 10, 1000);
WebElement element = waiting.until(ExpectedConditions.presenceOfElementLocated(By.id(my_id)));//problem is here
But then i try to find element on my page, WebDriverWait waiting until the page has completely loaded and then starts searching.
If I'm trying something like this
WebDriverWait waiting = new WebDriverWait(driver, 10, 2700);
driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS);
WebElement element;
try {
driver.get(something_url);
} catch (TimeoutException e) {
((JavascriptExecutor) driver).executeScript("window.stop();");
}finally {
element = waiting.until(ExpectedConditions.presenceOfElementLocated(By.id(my_id)));
}
It works, but if I go on like this
element.click();//go to another page
On this line doesn't throw a timeout exception, I have to wait for the full page load.
How to be in this situation ?
Solution is to wait for ajax refresh to complete on this page:
public void waitForAjaxRefresh() {
System.out.println("Waiting for Ajax Refresh");
try {
WebDriverWait wait = new WebDriverWait(driver,35);
final JavascriptExecutor javascript = (JavascriptExecutor) (driver instanceof JavascriptExecutor ? driver
: null);
wait.until(new ExpectedCondition<Boolean>() {
#Override
public Boolean apply(WebDriver d) {
boolean outcome = Boolean.parseBoolean(javascript
.executeScript("return jQuery.active == 0")
.toString());
return outcome;
}
});
} catch (TimeoutException ex) {
throw new TimeoutException("Timed out after "
+ 35
+ " seconds while waiting for Ajax to complete.");
} catch (WebDriverException e) {
System.out.println("JQuery libraries are not present on page "
+ driver.getCurrentUrl() + " - "
+ driver.getTitle());
}
}
I'm trying to expand all comments, replies, see more in comment, and see more in post in Facebook.
1) The codes I have written below allows me to expand all contents that I want except for maybe a few replies in a post even though I have put the repliesbutton in a loop.
2) There will a Exception in thread "main" org.openqa.selenium.ElementNotVisibleException: Element is not currently visible and so may not be interacted with error at the .click() if there is no try-catch in all the smaller for loops.
//declare WebDriverWait variable, times out after 20 seconds
WebDriverWait wait = new WebDriverWait(dr, 20);
//check element is present on the DOM of a page and visible
wait.until(ExpectedConditions.visibilityOfElementLocated(By.className("commentable_item")));
List<WebElement> comments = dr.findElements(By.className("commentable_item")); //get the comments
//iterate through the comments
for (WebElement comment : comments) {
boolean clickMore = true;
try {
while(clickMore == true) {
//find web elements by their respective class name
List<WebElement> commentsbutton = comment.findElements(By.className("UFIPagerLink")); //view more/previous comments
List<WebElement> repliesbutton = comment.findElements(By.className("UFIPagerIcon")); //replies
List<WebElement> seemorebutton = comment.findElements(By.className("_5v47")); //see more in comment
List<WebElement> seemorelinkbutton = dr.findElements(By.className("see_more_link")); //see more in link
//click more comments
if(commentsbutton.size() > 0) {
for (WebElement comments_element : commentsbutton) {
//comments_element.click(); //click on button if found
//Thread.sleep(5000); //pause for 5 seconds
try{
comments_element.click(); //click on button if found
Thread.sleep(5000); //pause for 5 seconds
} catch(Exception e){
}
}
for (WebElement replies_element : repliesbutton) {
//replies_element.click(); //click on button if found
//Thread.sleep(3000); //pause for 3 seconds
try{
replies_element.click(); //click on button if found
Thread.sleep(3000); //pause for 5 seconds
} catch(Exception e){
}
}
}
else clickMore = false;
for (WebElement seemorelinks_element : seemorelinkbutton) {
try{
seemorelinks_element.click(); //click on button if found
Thread.sleep(5000); //pause for 5 seconds
} catch(Exception e){
}
}
for (WebElement seemore_element : seemorebutton) {
try{
seemore_element.click(); //click on button if found
Thread.sleep(5000); //pause for 5 seconds
} catch(Exception e){
}
}
}
} catch (NoSuchElementException e) { //when no elements are found
System.out.println("Comments in this post not found");
}
}
}
//return the given element if it is visible and has non-zero size, otherwise null.
private static WebElement elementIfVisible(WebElement element) {
return element.isDisplayed() ? element : null;
}
public static ExpectedCondition<WebElement> visibilityOfElementLocated(final By locator) {
return new ExpectedCondition<WebElement>() {
#Override
public WebElement apply(WebDriver driver) {
try {
return elementIfVisible(dr.findElement(locator));
} catch (StaleElementReferenceException e) {
return null;
}
}
};
}
}
You can create a utility method and put it in some 'Utility.java' class something like below:
public void click(WebElement element)
{
((JavascriptExecutor)driver).executeScript("arguments[0].scrollIntoView(true);", element);
Thread.sleep(500);
if(element.isDisplayed())
element.click();
}
Usage:
WebElement element=driver.findElement(//Locator);
Utility.click(element);
This will ensure every time before you click the element is scrolledIntoView.
Let me know if you need further help on this.