每次打开它时,我的页面上都会出现一个对话框。我想关闭它以便我可以继续我的编程。 对话框就像这个image
我尝试了以下代码:
wait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("div#psyma_layer_background div#psyma_header div#psyma_close_link_container div#psyma_close_link a#psyma_close_button_link")));
wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("div#psyma_layer_background div#psyma_header div#psyma_close_link_container div#psyma_close_link a#psyma_close_button_link")));
wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("div#psyma_layer_background div#psyma_header div#psyma_close_link_container div#psyma_close_link a#psyma_close_button_link"))).click();
我确实进行了元素检查,可见性和可点击性检查,这样我就不会错过这个元素,但它无法弄清楚" X"标志,所以没有关闭对话框!如果可以的话,请帮帮我,真的很难与这个人挣扎
答案 0 :(得分:0)
看起来你很亲密。要关闭页面上显示的对话框,您需要点击标记为 X 的元素<a>
标记。因此,要在所需元素上调用click()
,您需要等待所需的元素可点击,如下所示:
代码块:
System.setProperty("webdriver.chrome.driver", "C:\\Utility\\BrowserDrivers\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://www.landrover.co.uk");
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.linkText("X"))).click();
System.out.println(driver.getTitle());
控制台输出:
Starting ChromeDriver 2.40.565498 (ea082db3280dd6843ebfb08a625e3eb905c4f5ab) on port 15398
Only local connections are allowed.
Jun 18, 2018 2:52:08 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS
Premium 4x4 Vehicles & Luxury SUVs - Land Rover UK
浏览器快照: