2017-05-22 87 views
-1

试图关闭Youtube弹出窗口,点击'x'按钮出现在右上角,但我收到以下错误消息:点击'x'关闭youtube弹出窗口不能使用Selenium WebDriver

所致:org.openqa.selenium.ElementNotVisibleException:元素是 当前不可见的,因此可以不与

进行交互试过波纹管代码:

driver.findElement(By.className("close")).click(); 
driver.findElement(By.xpath("//button[@class='close']")).click(); 
driver.findElement(By.cssSelector("button[class='close']")).click(); 
JavascriptExecutor executor = (JavascriptExecutor) driver; 
executor.executeScript("arguments[0].click();", 
driver.findElement(By.className("close"))); 

HTML:

<div id="videoModal" class="modal fade in" aria-hidden="true" aria-labelledby="videoModal" role="dialog" tabindex="-1" style="display: block;"> 
<div class="modal-dialog"> 
<div class="modal-content"> 
<div class="modal-body"> 
<button class="close" aria-hidden="true" data-dismiss="modal" type="button">×</button> 
<div> 
<iframe width="100%" height="350" allowfullscreen="" src="https://www.youtube.com/embed/SQFKxxKMIxc?autoplay=1"/> 
</div> 

截图: https://i.stack.imgur.com/W3IXe.png

public Boolean closeVideoPopup(){ 
    try{ 
     driver.findElement(By.id("video-how-to")).click(); 
     driver.waitForPageLoad(); 
     driver.findElement(By.className("close")).click(); 
     return true; 
    } 
    catch(Exception e) { 
     logger.info("Exception occurred: "+ e.getMessage().toString().substring(0, Integer.parseInt(TestConstants.ERRCHARCOUNT))); 
    } 
    return false; 
} 
+0

你能考虑我们展示上** 'x'按钮**您指的是我们的一些作品吗?谢谢 – DebanjanB

+0

@AakashSingh你还可以添加弹出窗口的打印屏幕,最好是在检查你试图点击的** x **后,用一个开放的浏览器控制台。 – iamdanchiv

+0

@Dev添加了截图链接和我写的代码。 –

回答

0

它可以使用完整的XPath现在

driver.findElement(By.xpath(".//*[@id='videoModal']/div/div/div/button[@class='close']")).click(); 

步骤来做到这一点使用Sikuli:

添加Sikuli罐子在您的项目或在pom.xml中

添加作为一个依赖
<dependency> 
      <groupId>com.sikulix</groupId> 
      <artifactId>sikulixapi</artifactId> 
      <version>1.1.0</version> 
</dependency> 

使用Sikuli IDE获取元素的屏幕截图。 步骤来安装和使用Sikuli IDE: http://www.sikuli.org/downloadrc3.html

写下面的代码:

Screen screen = new Screen(); 
Pattern image = new Pattern(filePath\\xbutton.png"); 
screen.click(image); 

点击Screen类的方法将有助于单击该元素

+0

对于视觉自动化,还有[关东万维网自动化](https://a9t9.com/kantu/web-automation)。它的工作原理与Sikuli相似,但可以自动执行Google Chromium(不是整个桌面)。它适用于使用框架/ iframe的网站。 – Tienkamp

0

这个选择应该工作,但如果你与你适合的标记取代它的效率会更高:

driver.findElement(By.xpath("//*[contains(@class, 'close')]")); 

或者,si我们知道寻求的元素是一个div:

driver.findElement(By.xpath("//div[contains(@class, 'close')]")); 

希望它会帮助你。

+0

试过。但它没有奏效。 –

0

也许某些其他元素与您的弹出窗口重叠。因此,您可以使用显式等待元素以使其可见。

WebDriverWait wait=new WebDriverWait(driver, 90); 
wait.untill(ExpectedConditions.visibilityOf(driver.findElement(By.className("close"))); 

...或找出重叠的元素并等待元素消失。

两种方法你可以尝试。

+0

试过。但它没有奏效。 –

相关问题