点击右上角的'x'按钮,尝试关闭Youtube弹出窗口,但我收到以下错误消息:
引起: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 :(得分:0)
此选择器应该可以工作,但如果用适当的标记替换它会更有效:
driver.findElement(By.xpath("//*[contains(@class, 'close')]"));
或者,因为我们知道所搜索的元素是div:
driver.findElement(By.xpath("//div[contains(@class, 'close')]"));
希望它会对你有所帮助。
答案 1 :(得分:0)
也许其他元素与弹出窗口重叠。因此,您可以使用显式等待元素,以使其可见。
// This is an excerpt of my Entity Paket
@OneToMany(mappedBy = "paket", cascade = CascadeType.ALL, targetEntity = Komponente.class, fetch = FetchType.EAGER)
private List<Komponente> komponenten;
public void setKomponenten(List<Komponente> komponenten) {
this.komponenten = komponenten;
}
public List<Komponente> getKomponenten() {
return komponenten;
}
// Komponente (the owning side)
@ManyToOne(cascade = { CascadeType.PERSIST, CascadeType.MERGE }, fetch = FetchType.EAGER)
private Paket paket;
// Excerpt from the ManagedBean
@ManagedBean
@ViewScoped
public class PaketController implements Serializable {
private List<Komponente> komponenteInSelectedPaketList;
private Komponente selectedKomponente;
private Paket selectedPaket;
public void removeKomponenteFromPaket() {
this.komponenteInSelectedPaketList.remove(this.selectedKomponente);
// Next row manipulates the referenced entity collectin as well
List<Komponente> example = this.selectedPaket.getKomponenten();
// example contains the list without the removed Komponente from the other list (call by reference like behaviour)
}
}
...或找出重叠的元素并等待元素消失。
你可以尝试两种方式。
答案 2 :(得分:0)
现在使用完整的xpath
driver.findElement(By.xpath(".//*[@id='videoModal']/div/div/div/button[@class='close']")).click();
使用Sikuli执行此操作的步骤:
在项目中添加Sikuli jar或在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类的方法将有助于单击元素