Home > OS >  I can't click on an element
I can't click on an element

Time:09-28

the page is fully loaded and the element is located, but for some reason it can't be clicked, I can't understand why.

My test:

@Test
public void test(){
    chromeDriver.get("https://alfabank.ru/currency/");
    WebDriverWait wait = new WebDriverWait(chromeDriver, 8);
    wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//button[contains(@class, 'j1I7k')]//p[contains(text(), 'USD')]")));
    WebElement clicking = chromeDriver.findElement(By.xpath("//button[contains(@class, 'j1I7k')]//p[contains(text(), 'USD')]"));
    clicking.click();
}

Full xpatch it doesn't work either:

WebElement clicking = chromeDriver.findElement(By.xpath("/html/body/div[1]/div/div[6]/div/div/div/div/div[1]/div[1]/button[2]"));

Exception:

org.openqa.selenium.ElementClickInterceptedException: element click intercepted: Element is not clickable at point (539, 1199)

CodePudding user response:

I use JS click, don't know why cannot click normally.

wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//button[contains(@class, 'j1I7k')]//p[contains(text(), 'USD')]")));
WebElement clicking = driver.findElement(By.xpath("//button[contains(@class, 'j1I7k')]//p[contains(text(), 'USD')]"));
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true); arguments[0].click()", clicking);

CodePudding user response:

Apply scrollIntoView and then click on the USD using JavascriptExecutor.

JavascriptExecutor js = (JavascriptExecutor) driver;
WebDriverWait wait = new WebDriverWait(driver,30);

driver.get("https://alfabank.ru/currency/");

wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//button[contains(@class, 'j1I7k')]//p[contains(text(), 'USD')]")));
WebElement usdoption = driver.findElement(By.xpath("//button[contains(@class, 'j1I7k')]//p[contains(text(), 'USD')]"));
js.executeScript("arguments[0].scrollIntoView(true);", usdoption);
js.executeScript("arguments[0].click();", usdoption);

CodePudding user response:

None of the existing answer really explain what is their code is about.

Even if you launch the screen in full screen, USD is not in Selenium view port.

Also JS is only recommended when nothing works.

I am doing with Selenium class, actions, please see below :

driver.manage().window().maximize();
WebDriverWait wait = new WebDriverWait(driver, 30);
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
driver.get("https://alfabank.ru/currency/");
boolean b = wait.until(ExpectedConditions.titleIs("Курсы валют — «Альфа-Банк»"));
if (b) {
    new Actions(driver).moveToElement(wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("div[data-test-id='caption']")))).build().perform();
    WebElement clicking = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//button[contains(@class, 'j1I7k')]//p[contains(text(), 'USD')]")));
    ((JavascriptExecutor)driver).executeScript("arguments[0].click();", clicking);
}
  • Related