How to handle not clickable condition - selenium

I am using implicitly wait in my script, the problem is in one scenario the element is found but it is not in clickable state, in this case I am not able to use explicitly wait. How can I handle this scenario pls help me.

You can use both Implicit Wait & Explicit Wait in your script,
If you want webDriver to wait until element is clickable use Explicit wait before perform click(); action on that element.
String elementid = ""; //put id of element inside " --- ";
WebElement element = driver.findElement(By.id(elementid));
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.elementToBeClickable(element));
element.click();

Related

Selenium how to click/ access an a tag with a href that has a javascript function

I have searched but havent found something similar to what Im trying to do. I'm using java by the way, I'm trying to click/access an a tag with selenium. The issue is that I'm not sure how to go about it. There seems to be a function/event that I need to set off but not quite sure how to. I tried a few ways as get text and clicking but I knew that wasn't going to work. Also I seen there are ways of using JavascriptExecutor but not sure how to use it for my case. I will post the tag below and alsothe function signature, that might help. If theres a similar question please post the link.
<a name="DERIVED_SSS_SCL_SSS_ENRL_CART$276$" id="DERIVED_SSS_SCL_SSS_ENRL_CART$276$" ptlinktgt="pt_peoplecode" tabindex="203" onclick="javascript:cancelBubble(event);" href="javascript:submitAction_win0(document.win0,'DERIVED_SSS_SCL_SSS_ENRL_CART$276$');" class="SSSHYPERLINKBOLDSMALL">Enrollment Shopping Cart</a>
the signature
function submitAction_win0(form, id, event)
You don't need any JS. Just use this xpath:
"//a[contains(#onclick,'javascript:cancelBubble(event);')]"
Be sure the element is clickable, see
import org.openqa.selenium.support.ui.ExpectedConditions;
for the case of more matches:
List<WebElement> elements = driver.findElements(By.xpath("//a[contains(#onclick,'javascript:cancelBubble(event);')]"));
int elementIndex = 0; // 0 to get first of the 33 mathes, 32 to get the last one
WebElement element = elements.get(elementIndex);
element.click();
EDIT:
You should use WebDriverWait to avoid NoSuchElementException this way the driver will wait until the element is clickable... it will wait up to 10 seconds you can tell it to wait more if needed...
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.partialLinkText("Student Center")));
element.click();
Using click():
WebElement element = driver.findElement(By.cssSelector("a[class='SSSHYPERLINKBOLDSMALL']"));
element.click();
Using JavascriptExecutor (Not recommended):
WebElement element = driver.findElement(By.cssSelector("a[class='SSSHYPERLINKBOLDSMALL']"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", element);
You can use other locators too... like linkText:
WebElement element = driver.findElement(By.linkText("Enrollment Shopping Cart"));
Or partialLinkText:
WebElement element = driver.findElement(By.partialLinkText("Shopping Cart"));

How to applied a default wait time to visible or find or display of webElement in selenium except using Implicit wait or webdriver wait

I want to apply a one specification to my selenium test's code as webdriver have to wait for web element to display or load on a webpage for anytime. It should be applicable for a every web element in the code. Is there any way to get this. Instead of applying implict wait or Webdriver wait when ever it required. I can use this specification so that even though in future any webElement take sometimes to get visible it will wait default till it is visible.
You can create a method which receives By as parameter and returns WebElement, and use it for all the element search instead of driver.findElement()
// Java syntax
public WebElement findElement(By by) {
WebDriverWait wait = new WebDriverWait(driver, 30);
WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(by));
return element;
}
You can also put WebDriverWait wait = new WebDriverWait(driver, 30); at class level instead of creating a new instance every time.

wait until function not working

I am clicking one element and I am using wait function to identify the next element. This is the wait function.
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("some xpath")));
After identifying the element, I carry out other actions but when I click for the 1st element it will lead to next page so by the time page loads the wait function is applying for the current page and giving exception. Is there any solution for this? I tried
Browser.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS);
before wait but it's not working. It will only work if I use thread.sleep(1000) before wait but I dont want use thread.sleep().
I'm guessing the problem is with the fact that you are using
ExpectedConditions.presenceOfElementLocated(...)
Presence just means that the element is present in the DOM, not that it's visible, enabled, clickable, etc. I would suggest that you change to wait to match what you want to do with the element you are looking for. If you want to click it, wait for it to be clickable. If you want to get text from it, wait for it to be visible, etc.
Another issue may be that you are intending to wait for an element on page 2 but an element matches that locator on page 1. One solution is to find a unique element on page 2, wait for it to be visible, and then wait for your desired element. That way you ensure that you are on the correct page before waiting for the desired element on page 2.
I think there are 2 waits in this situation:
Wait for next page loaded
Wait for specified element loaded in the new page
Below is an option to wait for page loaded:
public void waitForLoad() {
ExpectedCondition<Boolean> condition = webDriver -> webDriver.getCurrentUrl().contains(getPageUrl());
WebDriverWait wait = new WebDriverWait(driver, pageLoadTimeout);
wait.until(condition);
}
Then wait for next element visible:
protected void waitFor(By by) {
ExpectedCondition<Boolean> condition = webDriver -> !webDriver.findElements(by).isEmpty();
WebDriverWait wait = new WebDriverWait(driver, pageLoadTimeout);
wait.until(condition);
}
or using other solutions:
public WebElement elementToBeClickable(By locator, int timeout) {
try {
return getWebDriverFluentWait(timeout)
.until(ExpectedConditions.elementToBeClickable(locator));
} catch (Exception e) {
return null;
}
}
with:
private Wait<WebDriver> getWebDriverFluentWait(int timeout) {
return new FluentWait<WebDriver>(driver)
.withTimeout(timeout, TimeUnit.SECONDS)
.pollingEvery(1, TimeUnit.SECONDS)
.ignoring(NoSuchElementException.class);
}
I think you want to click on element, so use elementToBeClickable
WebDriverWait wait=new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.elementToBeClickable(locator))
I also Have the same Problem
But using this I solved
WebElement element = (new WebDriverWait(driver, 30)).until(ExpectedConditions.elementToBeClickable(By.xpath(".//*[#class='IM_overlay']"))); //it will wait 30 second for get this element
element .click();
//You can Do any operator here
If the element that is to be clicked is in the next(new) page, you'll have to use the windows iterator.
You could use this code:
Set <Strings> ids = driver.getWindowHandles();
Iterator <String> it = ids.iterator();
String currentPage = it.next();
String newPage = it.next();
driver.switchTo().window(newPage);//this will switch to the new window. Use your 'wait' condition now and do all the operations
//now to switch back to the previous(current) window, you could use the below code
driver.switchTo().window(currentPage);

How can i write an if statement for a button to be visible?

I have a button on a page that is not displayed sometimes. I would like for my code to go around it, but it won't work withdriver.findElement.isDisplayed() . I was thinking of ExpectedConditions.elementToBeClickable, but i don't know how to make it boolean. Some help please?(of course, the condition is returning error as it's not boolean).
WebDriverWait wait = new WebDriverWait(driver, 10);
if(ExpectedConditions.elementToBeClickable(By.xpath("html//body//div[5]//div//div//form//div//div[1]//div[3]//div//div//div//input")).){
WebElement ex = driver.findElement(By.xpath("//*[#class='ng-pristine ng-untouched ng-valid'][#value='export'][#type='radio']"));
ex.click();
WebElement in = driver.findElement(By.xpath("html//body//div[5]//div//div//form//div//div[1]//div[3]//div//div//div//input"));
in.click();
}else{new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("html//body//div[5]//div//div//form//div//div[1]//div[3]//div//div//div//input")));}
'
findElement returns either element or throws NoSuchElementException, So to determine element is visible with if condition You should try using findElements instead because it's returns either list of WebElement or empty list, so you just check its size as below :-
List<WebElement> elements = driver.findElements(By.xpath("your xpath"));
//Now check it size
if(elements.size() > 0 && elements.get(0).isDisplayed())
{
WebElement element = elements.get(0);
//Now do your further stuff with this element
}

Why does not Explicit Wait work for autosuggest boxes?

I tried to use ExplicitWait for an autosuggest box but it did not work, so I am using simple Thread.sleep(5000):
/***************************************************************************************/
// The below Thread.sleep(5000) works fine
// Thread.sleep(5000);
// However the below Explicit Wait block does not recognize the element
WebDriverWait wait5000 = new WebDriverWait(driver, 0);
wait5000.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[#class='auto_suggest']/*[#class='title_item']")));
/***************************************************************************************/
WebElement firstItem = driver.findElement(By.xpath("//*[#class='auto_suggest']/*[#class='title_item']"));
firstItem.click();
System.out.println("Done!");
}
}
I can use Thread.sleep(5000) but this is not efficient, because of time loss. Is there any way to introduce an explicit wait for the autosuggest box? How to click the autosuggest box more in a more efficient way?
The 2nd parameter in the constructor is the timeout in milliseconds.
Here WebDriverWait wait5000 = new WebDriverWait(driver, 0); you are passing in 0 - you are giving it a maximum of 0 milliseconds to find the element. You could try increasing the timeout, for example, let it search for the element for up to 5 whole seconds:
WebDriverWait wait5000 = new WebDriverWait(driver, 5000);