简体   繁体   中英

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

检查屏幕截图

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  

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

叉


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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM