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!
Related
I have two .java files, one file (StockWatchlistElements.java) I have declare all the elements of the page and on second file (Example.java) I used that element.
StockWatchlistElements.java
public static WebElement lnkaStockWatchlist(WebDriver driver) {
try {
element = driver.findElements(By.xpath("//*[#id=\"dnn_ctr769_StockWatchList_pnlContent\"]/table/tbody/tr"));
} catch (Exception e) {
throw (e);
}
return element;
}
Example.java
List<WebElement> rows = StockWatchlistElements.lnkaStockWatchlist(driver);
int count = rows.size();
System.out.println("ROW COUNT : " + count);
change your method definition from WebElement to List<WebElement> like:
public static List<WebElement> lnkStockWatchlist(WebDriver driver) {
List<WebElement> element = new ArrayList<>();
try {
element = driver.findElements(By.xpath("//*[#id=\"dnn_ctr769_StockWatchList_pnlContent\"]/table/tbody/tr"));
} catch (Exception e) {
throw (e);
}
return element;
}
I am on this page
https://login.alibaba.com/?spm=a2700.8293689.0.0.NdeZUw&tracelog=hd_signin
And trying to access Email field by following web element code:
public static WebElement Email_Field(WebDriver driver) throws InterruptedException {
//element = (new WebDriverWait(driver, 10)).until(ExpectedConditions
//.visibilityOfElementLocated(By.xpath("//input[#id='fm-login-id']")));
element = driver.findElement(By
.xpath("//input[#id='fm-login-id']"));
while (!isDisplayed(element)) {
Thread.sleep(3000);
System.out.println("Element is not visible yet");
}
return element;
}
public static boolean isDisplayed(WebElement element) {
try {
if(element.isDisplayed())
return element.isDisplayed();
}catch (NoSuchElementException ex) {
return false;
}
return false;
}
But getting the following exception:
Exception in thread "main" org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":".//*[#id='fm-login-id']"}
(Session info: chrome=59.0.3071.115)
The email field is inside the frame. Before access any element in the frame, you have to switch. please try following code.
public static WebElement Email_Field(WebDriver driver) throws InterruptedException {
WebElement element;
(new WebDriverWait(driver, 30)).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt("alibaba-login-box"));
element = driver.findElement(By.xpath("//input[#id='fm-login-id']"));
while (!isDisplayed(element)) {
Thread.sleep(3000);
System.out.println("Element is not visible yet");
}
return element;
}
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 {
}
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.