'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 1:[1]

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();
    

Solution 2:[2]

You can use XPath Xpath = //img[@class = '_8idr img'] to click on that icon for your case sample code will look like this
Note :- use implicit Wait on driver instance or you can use explicit Wait for this particular element to wait till clickable.

driver.get("https://www.facebook.com/");
driver.findElement(By.xpath("//a[contains(text(),'Create New Account')]")).click(); 
driver.findElement(By.xpath("//img[@class = '_8idr img']")).click(); // To click on the cross icon in sign Up page  

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 undetected Selenium
Solution 2