I am using Selenium Webdriver and java and I need to click on this element:
here is the code:
driver.get(urlHp);
WebDriverWait wait = new WebDriverWait(driver, 10);
By btn = By.cssSelector("#_content-it_it_jcr_content_home-par1_promo_-par_o > div > li> a> div.btn-wrapper > button".trim());
wait.until(ExpectedConditions.visibilityOfElementLocated(btn));
driver.findElement(btn).click();
I can get the element and do not throw any exception but the click does not work.
Please note that the element is below the viewport.
How can I fix it?
Try Javascript executor:
JavascriptExecutor js=(JavascriptExecutor) driver;
js.executeScript("arguments[0].click()", driver.findElement(btn));
May be you can try with Actions class as given below.
driver.get(urlHp);
WebDriverWait wait = new WebDriverWait(driver, 10);
By btn = By.cssSelector("#_content-it_it_jcr_content_home-par1_promo_-par_o > div > li> a> div.btn-wrapper > button".trim());
wait.until(ExpectedConditions.visibilityOfElementLocated(btn));
Actions actions = new Actions(driver);
WebElement btnElement=driver.FindElement(btn);
actions.MoveToElement(btnElement).Click(btnElement).Perform();
Selenium can only click visible elements. By this I mean, that it should work as a user would use your app.
What Ranjith's showed you:
JavascriptExecutor js=(JavascriptExecutor) driver;
js.executeScript("arguments[0].click()", driver.findElement(btn));
This is clicking the button with actual javascript code. For a quickfix this is fine. But remember that this implementation will also click the element if it's size is 1px by 1px. Test will pass, but your app is not usable.
I would rather recommend for selenium to use scrollIntoView method.
https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollIntoView
This way you are closer to the actual user flow.
Related
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));
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.
I cant locate a button on dialog pages, I tried to use cssselectors, xpaths, but simple i cant locate buttons/texts on modal dialogs.
I attached a screenshot from the code.
What can you recommend?
Thank you!
By.xpath(".//button[.='/"Submit/"'])
or
By.xpath(".//button[#class='btn btn-default'])
If it found but click doesnt work try that javascript from other comment
I presume you are able to identify the element.However unable to click on that.
Try use following options.
Use WebDriverWait and elementToBeClickable to click on the element.
WebDriverWait wait = new WebDriverWait(driver, 30);
WebElement elementBtn = wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("div.modal-footer button.btn.btn-default")));
elementBtn.click();
Use Action class to click on the element.
WebDriverWait wait = new WebDriverWait(driver, 30);
WebElement elementBtn = wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("div.modal-footer button.btn.btn-default")));
Actions action=new Actions(driver);
action.moveToElement(elementBtn).click().build().perform();
Java Script Executor to click on the element.
JavascriptExecutor js= (JavascriptExecutor) driver;
js.executeScript("arguments[0].click();", driver.findElement(By.cssSelector("div.modal-footer button.btn.btn-default")));
Note: If above all options doesn't work.Check if there any iframe avaialable.If so, you need to switch to iframe first.like below.
driver.switchTo().frame("framename"); //name of the iframe.
driver.switchTo().frame(0); //you can use index as well.
You could try this:
JavascriptExecutor js= (JavascriptExecutor) driver;
WebElement webElement=driver.findElement(By.cssSelector("div.modal-footer button.btn.btn-default"));
js.executeScript(“arguments[0].click()”, webElement);
Hope it helps.
Try the bellow xpath:
driver.findElement(By.xpath("//div[#class='modal-footer']//button[contains(#class,'btn-default')]")).click();
In facebook navigate to Home and try to click on Add Photos/Videos to bring up the file upload window but it always gives an exception "Element is not clickable at point ..."
I tried the methods given in the first answer of Debugging "Element is not clickable at point" error
but nothing works.
Edit:
Code:
1.
WebElement element= driver.findElement(By.xpath("//span[text()='Add Photos/Video']"));
element.click();
2
WebElement element= driver.findElement(By.xpath("//span[text()='Add Photos/Video']"));
element.sendKeys(Keys.RETURN);
3
Actions actions = new Actions(driver);
actions.moveToElement(element).click().perform();
4.
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("arguments[0].click();", element);
You can use Actions moveToElement
Actions actions = new Actions(driver); // initialize Actions instance
actions.moveToElement(elementToClick).performe(); // scroll the screen to make the button visible
elementToClick.click();
I just tried this and it worked... the File | Open dialog opens.
driver.findElement(By.cssSelector("div._3jk")).click();
Try this:
IJavaScriptExecutor ex = (IJavaScriptExecutor)Driver;
ex.ExecuteScript("arguments[0].click();", elementToClick);
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);