Search code examples
javaseleniumfacebookxpathwebdriverwait

How to click on Close icon/element on the Facebook sign-up page using Selenium Java?


I am new to Selenium and learning. On the Facebook sign-up page I want to click on the X as shown in the below screenshot, but I am unable to click it. It's an img element.

driver.findElement(By.xpath("//img[@src='https://static.xx.fbcdn.net/rsrc.php/v3/yC/r/Q0G2UVjVQ4_.png']")).click();

check the screenshot


Solution

  • The desired element is an <img> tag which is the preceding-sibling of a <div> tag which is the immediate ancestor of the <div> tag with text as Sign Up

    cross


    Solution

    To click() on the element you need to induce WebDriverWait for the elementToBeClickable() and you can use either of the following locator strategies:

    • xpath:

      new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[text()='Sign Up']//ancestor::div[1]//preceding-sibling::img[1]"))).click();