to click on a partially visible webelement in selenium webdriver with java - java

In my application I am not able to click on a partially visible check box, I even tried to do the same with javascript(scrool and click) and mouse hover actions, could any one find me a solution for the same?

You could try this:
WebElement g_element=g_driver.findElement(g_util.getObject("./ObjectRep‌​ository/"+objfile+".‌​xml", objfile, office, "xpath"));
Webdriverwait wait = new Webdriverwait(driver,10)
wait.until(ExpectedConditions.elementToBeClickable(locator);
if(!elementChkBox.isSelected()) {
checkbox.click();
}

Related

Can't click on Submit button, not working in automation testing, works manually

I have a standard form with 2 text boxes to fill and a submit button. The Submit button suppose to work after filling in the first mandatory text box.
It works manually, but when running on the automation infrastructure, the element doesn't get clicked.
The odd thing is that when debugging, the submit button is not clickable too, although it's not greyed out.
I tried the 3 classic methods:
Javascript:
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", element);
Actions:
Actions actions = new Actions(driver);
actions.moveToElement(element).click().perform();
click:
element.click()
**Not working manually while in automation **
only when closing the form and creating a new one it works.
To click() on any clickable element ideally you need to induce WebDriverWait for the elementToBeClickable() and you can use either of the following solutions:
new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.elementToBeClickable(By.xpath("//elementXpath"))).click();
As an alternative, using Actions class:
new Actions(driver).moveToElement(new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.elementToBeClickable(By.xpath("//elementXpath")))).click().build().perform();
Another alternative, using JavascriptExecutor:
((JavascriptExecutor)driver).executeScript("arguments[0].click();", new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.elementToBeClickable(By.xpath("//elementXpath"))));
According to your steps an replays , may be there is a hidden popup or something block your elements ,so use WebDriverWait not only clickable but try to elementToBeSelected() , invisibilityOfTheElementLocated() or
presenceOfAllElementsLocatedBy()
increase the waiting time to be able to detect the issues

Element is clickable when ran in debug mode, but ran normally throws ElementClickInterceptedException

I am trying to do web automation.
I am defining a pop-up menu containing a button defined with either xpath or css respectively as
XPath:-->: //button[contains(text(), 'Open Door')
CSS:-->: div.device-item.content.view-content > div.detail > div > button.btn.btn-primary.ng-star-inserted
While all is well, it throws
org.openqa.selenium.ElementClickInterceptedException: element click intercepted:
when I am debugging the test one step at a time, it
runs successfully by clicking the button, with out any
problem. But when I am running the test, it fails. I hope it is not a wait issue, as we apply check waiting for
the presence of the button and verify it exists and clickable.
I believe many would advice to use JavaScriptExecutor approach, but our framework has a problem of returning any web element as a custom object called "Element" which is neither Web Element nor sub class of it, but extends Object and implements an interface called IElement, so we can't use JavaScriptExecutor method since it needs Web Element form of the button which we want to click on.
If it works in debug it means the overlay disappear automatically. You can wait for it to vanish
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.invisibilityOfElementLocated(By.cssSelector("[id^='device-card']")));
And in any case you can wait for the button to be clickable
button = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//button[contains(text(), 'Open Door')")));
button.click();
It will look something like:
driver.executeScript("document.querySelector('button.btn').click()")
Just adjust the css
There are a couple of things you need to take care:
XPath //button[contains(text(), 'Open Door'): This xpath can be optimized further in either of the formats:
//button[text()='Open Door']
//button[contains(., 'Open Door')]
CSS: div.device-item.content.view-content > div.detail > div > button.btn.btn-primary.ng-star-inserted looks good however I strongly believe button.btn.btn-primary.ng-star-inserted would also work.
"...Executes successfully in debug mode...": As the browser client gets ample time to settle the webpage dynamics and the desired WebElement gets enough time to get interactive.
"...we amply check waiting for the presence of the button and verify it exists and clickable...": Is a myth as:
presenceOfElementLocated(): Is the expectation for checking that an element is present on the DOM of a page. This does not necessarily mean that the element is visible.
where as:
elementToBeClickable(): Is the expectation for checking an element is visible and enabled such that you can click it.
As your usecase is to invoke click() on the element you need to use the ExpectedConditions as elementToBeClickable().
Example:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("css_element"))).click();
You can find a detailed discussion in What is the most efficient way to wait for a page element (xpath) to show up in Selenium Webdriver?
Finally, when using the Selenium java clients, of coarse there are methods from JavascriptExecutor but that should be the last resort.
Update
As you are still seeing the error ...ElementClickInterceptedException: element click intercepted..., you can add an additional step to induce WebDriverWait for the invisibility and you can use either of the following options:
invisibilityOf(WebElement element):
new WebDriverWait(driver, 20).until(ExpectedConditions.invisibilityOf(overlapping_webelement));
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("css_clickable_element"))).click();
invisibilityOfElementLocated(By locator):
new WebDriverWait(driver, 20).until(ExpectedConditions.invisibilityOfElementLocated(By.cssSelector("css_overlapping_element")));
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("css_clickable_element"))).click();
You can find a relevant detailed discussion in Selenium invisibilityOf(element) method throwing NoSuchElementException + WebDriverWait.ignoring(NoSuchElementException.class) is not working
you can move the mouse to that element and then click on it, even I was facing this issue and this solution solved it
Actions clickOnBtn = new Actions(driver);
clickOnBtn .moveToElement("//button[contains(text(), 'Open Door')").click().build().perform;
this happens even when the element is not visible completely onscreen, in this case you can scroll to that element and then use the above code as shown below
JavascriptExecutor jse2 = (JavascriptExecutor)driver;
jse2.executeScript("arguments[0].scrollIntoView()", ele);
Actions clickOnBtn = new Actions(driver);
clickOnBtn .moveToElement("//button[contains(text(), 'Open Door')").click().build().perform;
click element using javascriptExecutor
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", ele);
This even happens sometimes with the way we selected the element, the xpath can be changed, we can try to use a parent tag rather than the button and try
you can define your own ExpectedCondition with click action:
public class SuccessfulClick implements ExpectedCondition<Boolean> {
private WebElement element;
public SuccessfulClick(WebElement element) { //WebElement element
this.element = element;
}
#Override
public Boolean apply(WebDriver driver) {
try {
element.click();
return true;
} catch (ElementClickInterceptedException | StaleElementReferenceException | NoSuchElementException e) {
return false;
}
}
}
and then use it:
wait10.until(elementToBeClickable(btn));
wait10.until(new SuccessfulClick(btn));

Close window button clicked but window is not closed

In selenium I successfully switch to an iFrame which contains a modal window:
driver.switchTo().frame(driver.findElement(By.xpath("//iframe[#name='intercom-tour-frame']")))
In this iFrame is a close window button which is clicked "successfully" but the window does not close. By successfully I mean the button is found using the xpath and the action is completed without error in my code.
This is what I'm trying:
#FindBy(xpath = ("/html[1]/body[1]/div[1]/div[1]/div[1]/div[1]/div[1]/div[2]/span[1]"))
private WebElement closeTestTourButton;
public newCampaignPage clickCloseTestTourButton(WebDriver driver)
{
delay(5000);
closeTestTourButton.click();
}
I've also tried:
public newCampaignPage clickCloseTestTourButton(WebDriver driver)
{
delay(5000);
Actions builder = new Actions(driver);
builder.moveToElement(closeTestTourButton).build().perform();
waitForElementAndClick(closeTestTourButton, driver);
return this;
}
The test continues but fails as it tries to do an action but this is not possible due to the still open modal window.
Try clicking the button using the javascript, sometimes the events might not trigger with normal click.
public newCampaignPage clickCloseTestTourButton(WebDriver driver)
{
delay(5000);
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", closeTestTourButton);
return this;
}
I would suggest using the WebDriverWait rather delay in your script. Below is the implementation.
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.id(<someid>)));
Possibly you are switching and attempting to click() too early.
To click() on the close window button as the the desired elements are within an <iframe> so you have to:
Induce WebDriverWait for the desired frame to be available and switch to it:
new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//iframe[#name='intercom-tour-frame']")));
Induce WebDriverWait for the desired element to be clickable.
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("/html[1]/body[1]/div[1]/div[1]/div[1]/div[1]/div[1]/div[2]/span[1]"))).click();
But as you are using #FindBy presumably you are using PageFactory in PageObjectModel, so you won't be able to invoke WebDriverWait in conjunction with ExpectedConditions directly and you have to create a method. You can find a relevant detailed discussion in How to wait for invisibility of an element through PageFactory using Selenium and Java
Outro
Here you can find a relevant discussion on Ways to deal with #document under iframe
I don't like to answer my own questions but in this case this was the only solution that worked:
Actions builder = new Actions(driver);
builder.moveToElement(closeTestTourButton).build().perform();
builder.sendKeys(Keys.ENTER).perform();
Granted, this is not the most elegant solution but after two days of trying it was the only one that worked.

Mouse Hover Action-Unable to click hidden Link

I am unable to click hidden link("WatchBanking") after using move-to-element.
WebElement lnkW2yB=dr.findElement(By.xpath("//a[#href='/personal/ways_to_bank/ways-to-bank-landing']"));
Actions act=new Actions(dr);
act.moveToElement(lnkW2yB).build().perform();
WebElement Span=dr.findElement(By.xpath("//span[contains(text(),'Bank with your Watch')]"));
WebDriverWait wait=new WebDriverWait(dr,20);
wait.until(ExpectedConditions.visibilityOf(Span));
act.moveToElement(Span).build().perform();
Thread.sleep(5000L);
WebElement lnk=dr.findElement(By.linkText("WatchBanking"));
wait.until(ExpectedConditions.visibilityOf(lnk));
act.moveToElement(lnk).click(lnk).build().perform();
It Moves to the span("Bank with your Watch") and shows link("WatchBanking").
But its not clicking on WatchBanking due to immediate disappearance.
Please give me any solution on this.
Selenium sometimes behaves like that only.I would go with JavascriptExecutor at times like this.I've repaced Selenium click by Javascript click and it worked perfectly for that site you've mentioned in comment.
Replace the lnk.click() by the following
WebElement lnk = dr.findElement(By.xpath("//a[text()='WatchBanking']"));
wait.until(ExpectedConditions.visibilityOf(lnk));
JavascriptExecutor js = (JavascriptExecutor) dr;
js.executeScript("arguments[0].click();", lnk);

How can i create mouse event for clicking a tab- Selenium WebDriver?

Working with Selenium WebDriver and Java. I have tab name called PR Per Product.
I have many tabs under PR Reports tab.. In that PR tab i used
WebElement menuHoverLink = driver.findElement(By.id("ext-pr"));
actions.moveToElement(menuHoverLink).perform();
But the same format i tried to click PR Per Product tab it is clicking some other tab. The element is targeting through the ID.
<a id="ext-pr-domain" class=" ext-pr-domain" name="ext-pr-domain" href="https://10.4.16.159/extranet_prperdomain/reports/">Per Prod/SW Pack/RDT Group</a>
If i use this method it is exactly clicking the tab. But the problem it is not hovering and clicking.
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", driver.findElement(By.id("Per Prod/SW Pack/RDT Group") ));
Please help me for the issue..
As far as i know, you have to build the action also. Try
actions.moveToElement(menuHoverLink).build().perform();
or if you need to click in order to make it visible:
actions.moveToElement(menuHoverLink).click().build().perform();

Categories

Resources