如何使用 Selenium Java 在 Facebook 注册页面上单击关闭 icon/element?

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

我是 Selenium 和学习新手。在 Facebook 注册页面上,我想点击 X,如下面的屏幕截图所示,但我无法点击它。这是一个 img 元素。

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

您可以使用 XPath Xpath = //img[@class = '_8idr img'] 点击您案例的图标,示例代码如下所示
注意:- 在驱动程序实例上使用隐式等待,或者您可以使用显式等待此特定元素等待直到可点击。

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  

所需的元素是 <img> 标签,它是 preceding-sibling ]<div> 标签是 <div> 标签的直接祖先,文本为 Sign Up


解决方案

on the element you need to induce for the and you can use either of the following

  • xpath:

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