Uncaught DOMException: Failed to execute 'remo

2019-08-27 22:25发布

问题:

I have a dialog box appearing on my page every time I open it. I want to close it so that I can proceed with my programming. The dialogue box is like this image

I tried the following code :

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

I did presence of element check, visiblity and clickability check so that I do not miss the element by any chance but it's not able to figure out the "X" sign and so not closing the dialogue box! Please help me out if you can, really struggling with this one

回答1:

It seems you were pretty close. To close the dialog box appearing on your page you need to click on the element marked as X which is a <a> tag. So to invoke click() on the desired element you need to wait for the desired element to be clickable as follows:

  • Code Block:

    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());
    
  • Console Output:

    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
    
  • Browser Snapshot: