0

我是自动化新手并尝试运行我的代码,但是当我登陆页面时,出现的第一个弹出窗口不允许我处理,因为我无法关闭此弹出窗口。

System.setProperty("webdriver.chrome.driver","C:\\Users\\chromedriver_win32\\chromedriver.exe");
    WebDriver driver = new ChromeDriver();
    
    driver.get("https://www.seleniumeasy.com/test/");
    
    driver.findElement(By.xpath("//*[@id='at-cv-lightbox-close']")).click();
4

2 回答 2

1

您可以使用以下id(当我们都知道ID总是比xpath更可取Explicit wait :时,实际上没有一个答案提供解决方案)

ID

at-cv-lightbox-close

代码 :

System.setProperty("webdriver.chrome.driver", "C:\Users\chromedriver_win32\chromedriver.exe"); 
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://www.seleniumeasy.com/test/");
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.elementToBeClickable(By.id("at-cv-lightbox-close"))).click();
System.out.println("Task has been done !");
于 2021-08-02T16:36:25.670 回答
0

您需要在访问该元素之前添加等待。
像这样:

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

wait = WebDriverWait(driver, 20)

wait.until(EC.visibility_of_element_located((By.XPATH, "//*[@id='at-cv-lightbox-close']"))).click()

于 2021-08-02T15:24:02.320 回答